Script functions provide the ability to store and repeat chunks of code. Functions are declared as:
name () {
... code
return exit status
}
Script functions have access to all variables in the same script
file as the function - script variables are global. However, you can
declare local variables within a function by using the
local
keyword. These local variables exist only so long as the function is
executing.
Functions are called just by giving their name. Parmeters are passed
by just listing their names or values after the function name.
Parameters are passed by value. (It is possible to pass parameters
by reference, but that is beyond the scope of this lab.) Inside the
function these arguments are numbered just like the arguments passed
on the command line to a script. The first argument is
$1
, the scond
$2
, and so on. This includes
$#
to tell you how many arguments were passed. Be careful! It is easy
to mix up the function arguments with the command line arguments.
The
return
statement is optional, but it allows a function to return an
explicit exit code. This exit code is the functional equivalent of
the script exit code, except that the script keeps executing after a
function
return
. If no
return
statment is given, the function's exit code is determined by the
exit code of the last command executed within the function. The
calling script access the return value through the variable
$?
. The script
functest
and its execution illustrates some of these concepts:
#!/bin/bash
some_func() {
echo "Calling some_func"
local local_var="Hi There"
echo "$local_var"
if [ "$#" -ne "3" ]
then
echo "Expects three parameters"
return 1
fi
echo "Parameters are:"
for i in $@
do
echo $i
done
return 0
}
some_func a b
echo "Return value $?"
echo
some_func a b c
echo "Return value $?"
echo
echo "local variable local_var: $local_var"
exit 0
Executing
functest
:
/home/dbrown/CP367> ./functest Calling some_func Hi There Expects three parameters Return value 1 Calling some_func Hi There Parameters are: a b c Return value 0 local variable local_var:
The script
funcwrap
expands upon examples from previous labs. The script defines two
local return values within the function
wrap
, and calls this function from within a
for
loop, passing it arguments from the command-line.
#!/bin/bash
wrap () {
local RETURN_OK=0
local RETURN_BAD=1
echo "file $1"
if [ ! -e "$1" ]
then
echo "$1 does not exist"
local RETURN=$RETURN_BAD
else
newfile=${1}.htm
`sed -f wraphtml.sed $1 > $newfile`
echo "$1 was wrapped as $newfile"
local RETURN=$RETURN_OK
fi
return $RETURN
}
# Pass each command-line argument to the function.
for arg in "$@"
do
wrap $arg
echo "wrap function returned $?"
echo
done
exit 0
The result of calling the script:
/home/dbrown/CP367> ./funcwrap error.txt good.txt stuff.txt file error.txt error.txt was wrapped as error.txt.htm wrap function returned 0 file good.txt good.txt was wrapped as good.txt.htm wrap function returned 0 file stuff.txt stuff.txt does not exist wrap function returned 1
Add the following numbers to a file
nums.txt
and use it for testing for the tasks.
5 3 7 31 1 9 4 12 27
Write a script function
funcsort
that takes a filename as an argument and outputs the contents of
the file in sorted order.
Improve the above script to allow the use of a
-n
argument that sorts the contents of the file in numeric order.
Improve the script again to check the arguments. The script allows
one or two arguments: if there is one argument only, it is a
filename. If there are two arguments, the first argument must be
-n
and the second a filename. Inform the user if they use an invalid
argument. Set appropriate exit codes. Your script must use a
function to check the file validity.
Improve the script once again to check the file argument. Inform the user if the file does not exist, is not a normal file, or cannot be read. Allow the user to enter a third argument that is the output file to write to. Thus your script must be able to correctly handle the following tests:
./funcsort nums.txt./funcsort -n nums.txt./funcsort nums.txt out.txt./funcsort -n nums.txt out.txtand any errors in the input file or the argument count.