< Day Day Up > |
11.12. FunctionsThis section introduces functions so that you can use them interactively or store them in your initialization files. Later, when discussing scripts, functions will be covered in more depth. Functions can be used when an alias is not enough, that is, for passing arguments. Functions are often defined in the user's initialization file, .profile. They are like mini-scripts, but unlike scripts, functions run in the current environment; that is, the shell does not fork a child process to execute a function. All variables are shared with the shell that invoked the function. Often functions are used to improve the modularity of a script. Once defined, they can be used repeatedly and even stored in another directory. Functions must be defined before they are invoked; there are two formats used to define them. One format came from the Bourne shell and the other is new with the Korn shell. Functions can be exported from one invocation of the shell to the next. The typeset function and unset command can be used to list and unset functions. See Table 11.17.
11.12.1 Defining FunctionsThere are two acceptable formats for defining functions: the Bourne shell format (still allowed for upward compatibility) and the new Korn shell format. A function must be defined before it can be used.[5]
FORMAT (Bourne Shell) functionname() { commands ; commands; } (Korn Shell) function functionname { commands; commands; } Example 11.59.1 $ function fun { pwd; ls; date; } 2 $ fun /home/jody/ellie/prac abc abc123 file1.bak none nothing tmp abc1 abc2 file2 nonsense nowhere touch abc122 file1 file2.bak noone one Mon Feb 9 11:15:48 PST 2004 3 $ function greet { print "Hi $1 and $2"; } 4 $ greet tom joe # Here $1 is tom and $2 is joe Hi tom and joe 5 $ set jane nina lizzy 6 $ print $* jane nina lizzy 7 $ greet tom joe Hi tom and joe 8 $ print $1 $2 jane nina EXPLANATION
11.12.2 Functions and AliasesWhen processing the command line, the shell looks for aliases before special built-in commands and for special built-ins before functions. If a function has the same name as a built-in, the built-in will take priority over the function. An alias for a special built-in can be defined, and then the function name can be given the name of the alias to override the order of processing. Example 11.60.(The ENV File) 1 alias cd=_cd 2 function _cd { 3 \cd $1 4 print $(basename $PWD) 5 } (The Command Line) $ cd / / $ cd $HOME/bin bin $ cd .. ellie EXPLANATION
11.12.3 Listing FunctionsTo list functions and their definitions, use the typeset command. Example 11.61.(The Command Line) 1 $ typeset –f function fun { pwd; ls; date; } function greet { print "hi $1 and $2"; } 2 $ typeset +f fun greet EXPLANATION
11.12.4 Unsetting FunctionsWhen a function is unset, it will be removed from the shell's memory. Example 11.62.(The Command Line) 1 $ typeset –f function fun { pwd; ls; date; } function greet { print "hi $1 and $2"; } 2 $ unset -f fun 3 $ typeset –f function greet { print "hi $1 and $2"; } EXPLANATION
|
< Day Day Up > |