< Day Day Up > |
12.8. FunctionsKorn shell functions are similar to those used in the Bourne shell, and are used to modularize your program. A function is a collection of one or more commands that can be executed simply by entering the function's name, similar to a built-in command. Here is a review of some of the important rules about using functions.
12.8.1 Defining FunctionsA function must be defined before it can be invoked. Korn shell functions are defined with the keyword function preceding the function name. The curly braces must have a space on the inside of each brace. (Please see "Functions" on page 372 for the older-style Bourne shell function definition; these are still compatible in Korn shell scripts.) FORMAT function function_name { commands; commands; } Example 12.53.function usage { print "Usage $0 [-y] [-g] " ; exit 1; } EXPLANATION The function name is usage. It is used to print a diagnostic message and exit the script if the script does not receive the proper arguments, either –y or –g. 12.8.2 Listing and Unsetting FunctionsTo list local function definitions, type typeset –f. To list exported function definitions, type typeset –fx. To unset a function, type unset –f function_name. See the typeset command, Table 12.11 on page 714.
12.8.3 Local Variables and the Return ValueThe typeset command can be used to create local variables. These variables will be known only in the function where they are created. Once out of the function, the local variables are undefined. The return value of a function is really just the value of the exit status of the last command in the script unless a specific return command is used. If a value is assigned to the return command, that value is stored in the ? variable. It can hold an integer value between 0 and 255. Because the return command is limited to returning only integer values, you can use command substitution to return the output of a function and assign the output to a variable, just as you would if getting the output of a UNIX command. Example 12.54.(The Script) #!/bin/ksh # Scriptname: do_increment # Using the return Command) (The Output) $ do_increment 5 The sum is 6 6 EXPLANATION
Example 12.55.(Using Command Substitution) (The Script) # Scriptname: do_square #!/bin/ksh 1 function square { (( sq = $1 * $1 )) print "Number to be squared is $1." 2 print "The result is $sq " } 3 read number?"Give me a number to square. " 4 value_returned=$(square $number) 5 print $value_returned (The Output) $ do_square 5 Number to be squared is 10. The result is 100 EXPLANATION
12.8.4 Exported FunctionsFunction definitions are not inherited by subshells unless you define them in the ENV file with the typeset command, for example, typeset –fx function_names. You can export functions with typeset –fx from the current Korn shell to a script, or from one script to another, but not from one invocation of ksh to the next (e.g., a separate invocation means that if you type ksh at the prompt, a brand new shell is started up). Exported function definitions will not be inherited by the new shell. Example 12.56.(The First Script) $ cat calling_script #!/bin/ksh 1 function sayit { print "How are ya $1?" ; } 2 typeset –fx sayit # Export sayit to other scripts 3 sayit Tommy 4 print "Going to other script" 5 other_script # Call other_script print "Back in calling script" **************************************************************** (The Second Script) $ cat other_script # NOTE: This script cannot be invoked with #!/bin/ksh 6 print "In other script " 7 sayit Dan 8 print "Returning to calling script" (The Output) $ calling_script 3 How are ya Tommy? 4 Going to other script 6 In other script 7 How are ya Dan? 8 Returning to calling script Back in calling script EXPLANATION
12.8.5 Function Options and the typeset CommandThe typeset command is used to display function attributes. See Table 12.11. Autoloaded FunctionsAn autoloaded function is not loaded into your program until you reference it. The autoloaded function can be defined in a file somewhere else and the definition will not appear in your script, allowing you to keep the script small and compact. To use autoload, you need to set the FPATH variable in your ENV file. The FPATH variable contains a search path for directories containing function files. The files in this directory have the same names as the functions defined within them. The autoload alias for typeset –fu specifies that the function names that have not yet been defined are to be autoloaded functions. After the autoload command is executed with the function as its argument, you must invoke the function to execute the commands contained in it. The primary advantage of autoloading functions is better performance, because the Korn shell does not have to read the function definition if it has never been referenced.[4]
Example 12.57.(The Command Line) 1 $ mkdir functionlibrary 2 $ cd functionlibrary 3 $ vi foobar (In Editor) 4 function foobar { pwd; ls; whoami; } # function has the same name as the file. (In .profile File) 5 export FPATH=$HOME/functionlibrary # This path is searched for functions. (In Your Script) 6 autoload foobar 7 foobar EXPLANATION
A number of functions can be stored in one file; for example, calculation functions may be contained in a file called math. Because the function must have the same name as the file in which it is stored, you may create hard links to the function file. Each function name will be a link to the file in which the function is defined. For example, if a function in the math file is called square, use the UNIX/Linux ln command to give the math file another name, square. Now the math file and square file can be referenced, and in either case you are referencing the file by the corresponding function name. Now the square function can be autoloaded by its own name. Example 12.58.(The Command Line) 1 $ ln math square add divide 2 $ ls -i 12256 add 12256 math 12256 square 12256 divide 3 $ autoload square; square EXPLANATION
|
< Day Day Up > |