Korn Shell functions are an easy way to break larger Korn Shell scripts into more manageable chunks. You have the option of using the function to return a true/false value as well as normal number or string output. The basic format of a function in the Korn Shell is like this:
function test_function {
typeset local_variables
body_of_function
}
It’s always a good idea to declare your local variables in a function using the ‘typeset’ command so they don’t inherit values from calling functions or scripts. The command-line parameters become localized in a function so ‘$1’ isn’t the same as the ‘$1’ that was passed to the originating script. If you want to pass parameters to a function, call it with the values like this: “test_function parameter1 parameter2”. If you want to pass on the original values passed to the script on the command line, pass them to the function like this: “test_function $1” or to pass all of the parameters, like this: “test_function $@”. Please note use of the ‘shift’ statement will clear out the command line switches so this will not work if the command line switches have been parsed and removed from the environment.
The returning of true/false values from functions is counter intuitive when you are more familiar with other languages. In Korn Shell, a value of zero passed back from a function equals ‘true’ and a value of non-zero equals ‘false’. So, here’s how this plays out:
function test_name {
typeset FIRST=”$1”
typeset LAST=”$2”
if [[ “$FIRST” = “$LAST” ]]
then
return 0
else
return 1
fi
}
read FIRST?”Enter first name: “
read LAST?”Enter last name: “
if test_name “$FIRST” “$LAST”
then
print “First name equals last”
else
print “First name does not equal last”
fi
Now, when this script is executed, it asks for a first and last name and indicates if they are the same. This is not very useful as a script but it illustrates the true/false return quite nicely. It should be noted that functions return the value of the last statement executed. The ‘return’ statement exits the function as well as providing the appropriate feedback. Also, note that double-quotes were used to supply the parameters to the function. This ensures that the entire variable is passed as “$1” or “$2” instead of interpreting spaces as a separator in the call.
Here’s an example of a function returning a string value instead of Boolean:
function full_name {
typeset FIRST=”$1”
typeset LAST=”$2”
print “$FIRST $LAST”
}
read FIRST?”Enter first name: “
read LAST?”Enter last name: “
print “Full Name=$(full_name “$FIRST” “$LAST”)”
This brings up another interesting point. You can call a function using the old-style of the grave symbols:
print “Full Name=`full_name “$FIRST” “$LAST”`”
Or, you can use the non-deprecated form presented in the previous example.
One other concern is how to successfully call Boolean functions in a ‘if’ call. Here’s a simple example:
if [[ test_name “henry” “henry” && “$ME” = “henry” ]]
then
print “hi henry”
fi
This won’t work! The Boolean and the non-boolean portions need to be separated. So, here’s the correct way to combine the two:
if test_name “henry” “henry” && [[“$ME” = “henry” ]]
then
print “hi henry”
fi
Hopefully this blog entry has encouraged the use of functions in shell scripting. Thanks.