Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help
next up previous contents
Next: Some useful shell and Up: Shell Variables, Aliases and Previous: Aliases   Contents

Functions

Functions are much as in other high level languages. Here's a simple example of a script that creates a function, then calls it.

#takes 3 args; a string, the number of the target word in the string 
#and which chars in that field you want. No error checking done.
function number {
   echo $1 | cut -d' ' -f$2 | cut -c $3 
}

echo -n "Percentage is "
# the next line prints characters 7-8 of word 1 in the supplied string
# by calling the above function
number "%Corr=57   Acc=47.68 Fred" 1 7-8

echo -n "Acc is "
number "%Corr=57   Acc=47.68 Fred"  2 5-9



Tim Love 2010-04-27