Results 1 to 4 of 4

Thread: functions

  1. #1
    Join Date
    Dec 2003
    Location
    Ogden Utah
    Posts
    34

    Unanswered: functions

    Hello:

    It is not very clear to me how or where I can store a function that will be used in future scripts? Do I store them in a specific directory, profile, etc??
    mvilla

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Depends which shell. You can generally have a number of functions in your .profile which would give you the performance benefit of these being memory resident when you come to use them.

    You could also store them in any old file(s) which you could use as a functions library to source (.) into your scripts when you require them.

    In addition, in ksh, you can use the autoload feature. This uses the FPATH environment variable to search for files matching the function you have attempted to call. If it finds a matching file name, it assumes this hold the fucntion definition and 'autoloads' it. To turn this functionality on, use typeset -fu.

    Damian

  3. #3
    Join Date
    Dec 2003
    Location
    Ogden Utah
    Posts
    34

    functions

    Depends which shell.

    currently using the bourne shell.
    ============================
    You could also store them in any old file(s) which you could use as a functions library to source (.) into your scripts when you require them.

    Yes ..... I would like to follow the suggestion above.

    dir villan
    dir scripts
    dir fctns
    file AB <ftp function>
    file BC <width function>
    file CD <count function>

    Would the above directory structure work?

    Does the shell immediately know it is a function and where to look when the (.) is used within a script

    Do I have to put the above tree structure in the $PATH variable?
    mvilla

  4. #4
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Sourcing a file/script simply executes that script in the current shell as opposed to forking another. This means that any variables or functions declared in the sourced file will be available in the calling script.

    You don't need to do anything with the path (unless you want to) as you can always supply it when you source the file.

    e.g.

    . /some/directory/containing/yourFunctions/functionsFile


    Also, from memory, some implementations of the Bourne shell will require the keyword source rather than the '.' syntax to source a file.

    HTH

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •