|
dreamsys software |
|
UNIX & Linux Shell Scripting Tutorial
BASH Shell
Functions
When your scripts start to become very large, you may tend to notice that you are repeating code more often in your scripts. You have the
ability to create functions inside of your script to help with code reuse. Writing the same code in multiple sections of your script can lead
to severe maintenance problems. When you fix a bug in a section of code you need to be sure that all sections of code that are repeated will
also have those fixes. A function is a block of code that can be called from other parts of your script. It can have parameters passed to it as
if it were a separate script itself. As an example, we will create a function called logit, which will take two parameters, a level and
a message. The level will be a number between 1 and 3 that will indicate the severity of the log message. The level of messages that you want
to view will be passed in the command line of the script.
#!/bin/sh
# logit function declaration.
logit()
{
MSG_LEVEL=$1
# Shifts the position of the parameters over one place.
shift
if [ "$MSG_LEVEL" -ge 1 ] && [ "$MSG_LEVEL" -le 3 ]; then
if [ "$LEVEL" -eq 1 ] && [ "$MSG_LEVEL" -ge 1 ]; then
echo "Msg Level $MSG_LEVEL: $@"
elif [ "$LEVEL" -eq 2 ] && [ "$MSG_LEVEL" -ge 2 ]; then
echo "Msg Level $MSG_LEVEL: $@"
elif [ "$LEVEL" -eq 3 ] && [ "$MSG_LEVEL" -ge 3 ]; then
echo "Msg Level $MSG_LEVEL: $@"
fi
fi
}
#Load the log level from the command line...
LEVEL=$1
# Call the function a couple of times.
logit 1 Logit Test one
logit 2 Logit Test two
logit 3 Logit Test three
logit 4 Logit Test four
Let's save this file and run it several times with different input levels...
$ test.sh 1 Msg Level 1: Logit Test one Msg Level 2: Logit Test two Msg Level 3: Logit Test three $ test.sh 2 Msg Level 2: Logit Test two Msg Level 3: Logit Test three $ test.sh 3 Msg Level 3: Logit Test three $ test.sh 4 $ In this script we use the unix command shift to shift the position of the $@ pointer to the right. Remember that $@ points to the entire command line. In the case of functions, the command line parameters are localized to the function, so for the first call to logit, the following parameters are passed "1 Logit Test one". Inside the logit function, the $@ variable contains the data "1 Logit Test one". We already read the first parameter ("1") into our MSG_LEVEL variable, so we want to use the other parameters are the text message. access them all together without the first parameter, we call shift, which moves the $@ variable one to the right so that it now contains the text "Logit Test one". Another interesting thing in this script is that for the $LEVEL variable, the scope does not matter. Remember that in a unix script, a varible is just an environment variable and is accessible from anywhere.
You can write recursive functions in a shell script as well. Consider the factoral:
#!/bin/sh
fac()
{
if [ "$1" -gt 1 ]; then
NEXT=`expr $1 - 1`
REC=`fac $NEXT`
PROD=`expr $1 \* $REC`
echo $PROD
else
echo 1
fi
}
echo "Enter a number: "
read NUM
echo "$NUM! = `fac $NUM`"
A recursive function is a function that calls itself. Notice at line 7 in the script, it calls the fac function inside itself. Recursive functions have the possibility of going in an endless loop and crashing if you do not code them right, so only use a recursive function if you really understand what you are trying to do. |
|