|
|
|
Store Common Shell Script Function
|
Original Message
|
Name: reginald
Date: December 28, 2003 at 18:01:02 Pacific
Subject: Store Common Shell Script Function OS: HP-UX 11.0 CPU/Ram: HP L2000
|
Comment: I used to write korn shell script. How can I store common shell script functions in one or more shell script source files and then store the shell script source files in one or more directory(ies); so that I can call the functions from within any shell scripts ?
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: nails
Date: December 29, 2003 at 10:07:56 Pacific
Subject: Store Common Shell Script Function |
Reply: (edit)Reginald: Using the Korn shell, the preferred method is place all your functions in a director, pointed to by shell variable FPATH. i.e: export FPATH=/home/nails/functions. Say, you have a function called downshift: # downshift: this function return the argument as downshifted string # usage: cmmd=$(down_shift $cmmd) function downshift { echo $1 | tr '[A-Z]' '[a-z]' } # end down_shift place it in /home/nails/functions/downshift and now Korn shell scripts automatically load downshift if it's needed. Another method that works with both Korn and Bourne is to place all functions in 1 file and "source" it: . /home/nails/functions/filefunction but this loads all the functions whether they're required of not. Regards, Nails
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: reginald
Date: December 30, 2003 at 18:19:18 Pacific
Subject: Store Common Shell Script Function
|
Reply: (edit)Nails, Thanks for your reply. I know FPATH setting. But I've found the source shell script file name must be the same as the function name. In that case, it seems that I am forced to store only one function in each shell script source file. Is it true ? Do I also need to use typeset ? If yes, how and why ?
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: nails
Date: January 2, 2004 at 20:42:54 Pacific
Subject: Store Common Shell Script Function |
Reply: (edit)Reginald: If you are going to use the FPATH functionality, I'm afraid you must store a single function in a file of the same name. The only exception is if the function calls another function it may alos reside in that file, but you can't call it from the main script. (I never use this). If you autoload a function: autoload downshift The FPATH directories is searched before PATH. autoload is an alias for 'typeset -fu'. Of course, if you choose to source a file, all your functions can be in one file. We've already talked about the drawback of doing this. Regards, Nails
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: reginald
Date: January 5, 2004 at 00:49:49 Pacific
Subject: Store Common Shell Script Function
|
Reply: (edit)What's the drawback of storing more than functions in one single shell script file ?
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: nails
Date: January 5, 2004 at 07:49:16 Pacific
Subject: Store Common Shell Script Function |
Reply: (edit)Reginald: If you load all the functions in a file, and "source" the file (as described in my original post), all the functions are loaded into your environment whether they're required or not. If the file is large, the loading time maybe prohibitive. Program execution maybe affected also. Regards, Nails
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: reginald
Date: January 5, 2004 at 17:54:59 Pacific
Subject: Store Common Shell Script Function
|
Reply: (edit)Nails, What is the purpose of -u option in typeset upon declaring function using typeset. That is, what if I type as follows: typeset -f function_name instead of typeset -fu function_name
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: nails
Date: January 6, 2004 at 11:29:39 Pacific
Subject: Store Common Shell Script Function |
Reply: (edit)Reginald: The -u option of typeset leaves the function undefined until it's needed. Thus, in the event you declare the function, but never use it in the script, this saves shell processing time. Another way of saying why load the function if you don't use it. Regards, Nails
Report Offensive Follow Up For Removal
|
|
Response Number 9
|
Name: reginald
Date: January 6, 2004 at 17:07:26 Pacific
Subject: Store Common Shell Script Function
|
Reply: (edit)But why I've found from a number of HP and non-HP Unix reference manuals that the '-u' option is used to convert all lowercase characters to uppercase characters ? I have never found any clue similar to your explanation. Is it an undocumented secret ?
Report Offensive Follow Up For Removal
|
|
Response Number 10
|
Name: nails
Date: January 7, 2004 at 19:07:32 Pacific
Subject: Store Common Shell Script Function |
Reply: (edit)Reginald: I'm not an HP person, so I can comment on that. You are correct about -u except where it's used with the -f option. A good reference that covers this is "Korn Shell Programming by Example" by Dennis O'Brein and David Pitts. Also, here is an excerpt from my Solaris "man" page for typeset: -f The names refer to function names rather than variable names. No assignments can be made and the only other valid flags are -t, -u and -x. The flag -t turns on execution tracing for this function. The flag -u causes this function to be marked undefined. The FPATH variable will be searched to find the function definition when the function is referenced. Regards, Nails
Report Offensive Follow Up For Removal
|
Use following form to reply to current message:
|
|

|