Functions


A shell function (tcsh does not have functions) is similar to a shell script in that it stores a series of commands for execution at a later time. However, because the shell stores a function in the computer's main memory (RAM) instead of in a file on the disk, the shell can access it more quickly than the shell can access a script. The shell also preprocesses (parses) a function so that it starts up more quickly than a script. Finally the shell executes a shell function in the same shell that called it. If you define too many functions, the overhead of starting a subshell (as when you run a script) can become unacceptable.

You can declare a shell function in the ~/.bash_profile startup file, in the script that uses it, or directly from the command line. You can remove functions with the unset builtin. The shell does not keep functions once you log out.

Tip: Removing variables and functions

If you have a shell variable and a function with the same name, using unset removes the shell variable. If you then use unset again with the same name, it removes the function.


The syntax that declares a shell function is

[function] function-name () { commands } 


where the word function is optional, function-name is the name you use to call the function, and commands comprise the list of commands the function executes when you call it. The commands can be anything you would include in a shell script, including calls to other functions.

The first brace ({) can appear on the same line as the function name. Aliases and variables are expanded when a function is read, not when it is executed. You can use the break statement (page 543) within a function to terminate its execution.

Shell functions are useful as a shorthand as well as to define special commands. The following function starts a process named process in the background, with the output normally displayed by process being saved in .process.out:

start_process( ) { process > .process.out 2>&1 & } 


The next example shows how to create a simple function that displays the date, a header, and a list of the people who are using the system. This function runs the same commands as the whoson script described on page 265. In this example the function is being entered from the keyboard. The greater-than (>) signs are secondary shell prompts (PS2); do not enter them.

$ function whoson () > { >  date >  echo "Users Currently Logged On" >  who > } 


$ whoson Sun Aug 7 15:44:58 PDT 2005 Users Currently Logged On zach     console  May 5 22:18 zach     ttyp1    May 5 22:20 zach     ttyp2    May 5 22:21 (bravo.example.co) 


Functions in startup files

If you want to have the whoson function always be available without having to enter it each time you log in, put its definition in ~/.bash_profile. Then run .bash_profile, using the . (dot) command to put the changes into effect immediately:

$ cat ~/.bash_profile export TERM=vt100 stty kill '^u' whoson () {     date     echo "Users Currently Logged On"     who } $ . ~/.bash_profile 


You can specify arguments when you call a function. Within the function these arguments are available as positional parameters (page 564). The following example shows the arg1 function entered from the keyboard.

$ arg1 ( ) { > echo "$1" > } $ arg1 first_arg first_arg 


See the function switch () on page 261 for another example of a function. "Functions" on page 561 discusses the use of local and global variables within a function.

Optional

The following function allows you to export variables using tcsh syntax. The env builtin lists all environment variables and their values and verifies that setenv worked correctly:

$ cat .bash_profile ... # setenv - keep tcsh users happy function setenv( ) {     if [ $# -eq 2 ]        then           eval $1=$2           export $1        else           echo "Usage: setenv NAME VALUE" 1>&2     fi }      $ . ~/.bash_profile      $ setenv TCL_LIBRARY /usr/local/lib/tcl      $ env | grep TCL_LIBRARY      TCL_LIBRARY=/usr/local/lib/tcl 


eval

The $# special parameter (page 564) takes on the value of the number of command line arguments. This function uses the eval builtin to force bash to scan the command $1=$2 twice. Because $1=$2 begins with a dollar sign ($), the shell treats the entire string as a single tokena command. With variable substitution performed, the command name becomes TCL_LIBRARY=/usr/local/lib/tcl, which results in an error. Using eval, a second scanning splits the string into the three desired tokens, and the correct assignment occurs. See page 374 for more information on eval.





A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net