If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > functions

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-29-04, 12:30
mvillan mvillan is offline
Registered User
 
Join Date: Dec 2003
Location: Ogden Utah
Posts: 34
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
Reply With Quote
  #2 (permalink)  
Old 03-30-04, 04:11
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
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
Reply With Quote
  #3 (permalink)  
Old 03-30-04, 08:41
mvillan mvillan is offline
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old 03-30-04, 09:01
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On