Previous Table of Contents Next


Each return statement causes the function to exit and return execution control to the shell or function that called it initially. All remaining code in the function is skipped.


NOTE:  
If the return command is used at the first level of execution (not inside a function), the shell will exit as if you used an exit statement.




NOTE:  
If you have set the exit trap to 0 (trap 'command' 0), it is executed before the function is exited. The Bourne shell function does not execute the trap command when exiting a function.



DIAGNOSTICS AND BUGS

The most common problem with shell scripts that fail to execute properly is syntax problems. And most syntax problems relate to missing quotes. You will receive strange error messages if you forget to match quotes correctly.

RELATED COMMANDS

Refer to the ksh command described in Module 71.

RETURN CODES

You control what exit status is returned by using the exit command or the return command inside functions. If you do not specify a return code, the code from the last command is used.

APPLICATIONS

A shell script is commonly used to perform tasks that require the combination of multiple UNIX commands. For example, disk backup scripts are often used to perform daily backups. Instead of writing an entire program in a programming language like C, you would use a shell script. The script is much easier to program and update.

TYPICAL OPERATION

In this activity you create a shell named phone. Use the break command to break out of a while loop if a certain time is reached. Make the shell accept an argument for the name of someone. Begin at the shell prompt.

1.  Check for an existing shell with the same name. Use the whence (ksh), type (sh), or which (BSD) commands. Type whence phone and press Return. If the phone command is not located continue to the next step. If a command with this name was located, you will need to select a new name for the shell before continuing.
2.  Type cd ~/bin to change directories to your bin directory.
3.  Type vi phone and press Return.
4.  Type i to enter insert mode.
5.  Type the following lines and press Return after each line.
     cd                   # cd to your home directory
     PERSON=$1            # set person to argument one.
     OIFS=$IFS            # SAVE the Input Field Separator
     IFS=":"              # reset IFS to a:
     cat db/phone | \     # pipe the phone file thru a while loop
     while read LINE      # read one line at a time
      do
       set $LINE          # set the positional parameters to the input line
       If [ "$1" = "$PERSON" ]
       then
             NAME=${NAME:=$1}
             NUMB=${NUMB:=$7}
             echo "Name   : $NAME"
             echo "Phone #: $NUMB"
       fi
      done
      IFS=$OIFS           # set IFS back to original value

C Shell
There is no easy way to do this exact process in the csh. In fact a better way to do this same function is to use nawk or use the following script.
grep "$1" db/phone | cut -d:-f1,7 | sed 's/:/<tab/g'
This will work for any shell. Although on BSD systems that do not support cut you will have to replace it with an nawk equivalent.

6.  Save the shell to disk by pressing Escape and typing ZZ.
7.  Change the permissions so the shell can be executed. Type chmod 755 phone and press Return.
8.  Now you are ready to execute the shell. Type phone anyone and press Return. Notice the Phone # is empty. You will need to update your db/phone database to contain valid names and phone numbers. Currently, your database does not have a "phone" field. Simply add a colon at the end of each line and place a phone number in the new field.
9.  Turn to Module 73 to continue the learning sequence.


Previous Table of Contents Next