< Day Day Up > |
Functions were introduced to the Bourne shell in AT&T's UNIX System VR2. A function is a name for a command or group of commands. Functions are used to modularize your program and make it more efficient. You may even store functions in another file and load them into your script when you are ready to use them. Here is a review of some of the important rules about using functions.
FORMAT function_name () { commands ; commands; } Example 8.47. dir () { echo "Directories: " ; ls -l nawk '/^d/ {print $NF}' ; } EXPLANATION The name of the function is dir . The empty parentheses are necessary syntax for naming the function but have no other purpose. The commands within the curly braces will be executed when dir is typed. The purpose of the function is to list only the subdirectories below the present working directory. The spaces surrounding the curly braces are required. 8.7.1 Unsetting FunctionsTo remove a function from memory, the unset command is used. FORMAT unset function_name 8.7.2 Function Arguments and the Return ValueBecause the function is executed within the current shell, the variables will be known to both the function and the shell. Any changes made to your environment in the function will also be made to the shell. Arguments can be passed to functions by using positional parameters. The positional parameters are private to the function; that is, arguments to the function will not affect any positional parameters used outside the function. The return command can be used to exit the function and return control to the program at the place where the function was invoked. (Remember, if you use exit anywhere in your script, including within a function, the script terminates.) The return value of a function is really just the value of the exit status of the last command in the script, unless you give a specific argument to the return command. If a value is assigned to the return command, that value is stored in the ? variable and can hold an integer value between 0 and 255. Because the return command is limited to returning only an integer between 0 and 255, you can use command substitution to capture the output of a function. Place the entire function in backquotes and assign the output to a variable just as you would if getting the output of a UNIX command. Example 8.48.(Using the return command) (The Script) #!/bin/sh # Scriptname: do_increment 1 increment () { 2 sum=`expr + 1` 3 return $sum # Return the value of sum to the script. } 4 echo n "The sum is " 5 increment 5 # Call function increment; pass 5 as a parameter. # 5 becomes for the increment function. 6 echo $? # The return value is stored in $? 7 echo $sum # The variable "sum" is known to the function, # and is also known to the main script. (The Output) 4,6 The sum is 6 7 6 EXPLANATION
Example 8.49.(Using command substitution) (The Script) #!/bin/sh # Scriptname: do_square 1 function square { sq=`expr \* ` echo "Number to be squared is ." 2 echo "The result is $sq " } 3 echo "Give me a number to square. " read number 4 value_returned= `square $number` # Command substitution 5 echo $value_returned (The Output) 3 Give me a number to square. 10 5 Number to be squared is 10. The result is 100 EXPLANATION
8.7.3 Functions and the dot CommandStoring FunctionsFunctions are often defined in the .profile file, so that when you log in, they will be defined. Functions cannot be exported, but they can be stored in a file. Then, when you need the function, the dot command is used with the name of the file to activate the definitions of the functions within it. Example 8.50.1 $ cat myfunctions 2 go() { # This file contains two functions cd $HOME/bin/prog PS1='`pwd` > ' ls } 3 greetings() { echo "Hi ! Welcome to my world." ; } 4 $ . myfunctions 5 $ greetings george Hi george! Welcome to my world. EXPLANATION
Example 8.51.(The .dbfunctions file shown below contains functions to be used by the main program.) 1 $ cat .dbfunctions 2 addon () { # Function is named and defined in file .dbfunctions 3 while true do echo "Adding information " echo "Type the full name of employee " read name echo "Type address for employee " read address echo "Type start date for employee (4/10/88) :" read startdate echo $name:$address:$startdate echo n "Is this correct? " read ans case "$ans" in [Yy]*) echo "Adding info..." echo $name:$address:$startdate>>datafile sort u datafile o datafile echo n "Do you want to go back to the main menu? " read ans if [ $ans = Y o $ans = y ] then 4 return # Return to calling program else 5 continue # Go to the top of the loop fi ;; *) echo "Do you want to try again? " read answer case "$answer" in [Yy]*) continue;; *) exit;; esac ;; esac done 6 } # End of function definition ------------------------------------------------------------- (The Script) 7 #!/bin/sh # Scriptname: mainprog # This is the main script that will call the function, addon datafile=$HOME/bourne/datafile 8 . .dbfunctions # The dot command reads the dbfunctions file into memory if [ ! f $datafile ] then echo "`basename $datafile` does not exist" 1>&2 exit 1 fi 9 echo "Select one: " cat <<EOF [1] Add info [2] Delete info [3] Exit EOF read choice case "$choice" in 10 1) addon # Calling the addon function ;; 2) delete # Calling the delete function ;; 3) update ;; 4) echo Bye exit 0 ;; *) echo Bad choice exit 2 ;; esac echo Returned from function call echo The name is $name # Variables set in the function are known in this shell. EXPLANATION
|
< Day Day Up > |