Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting | ||
---|---|---|
Prev | Chapter 23. Functions | Next |
Functions may process arguments passed to them and return an exit status to the script for further processing.
function_name $arg1 $arg2 |
The function refers to the passed arguments by position (as if they were positional parameters), that is, $1, $2, and so forth.
Example 23-2. Function Taking Parameters
#!/bin/bash func2 () { if [ -z "$1" ] # Checks if parameter #1 is zero length. then echo "-Parameter #1 is zero length.-" # Also if no parameter is passed. else echo "-Param #1 is \"$1\".-" fi if [ "$2" ] then echo "-Parameter #2 is \"$2\".-" fi return 0 } echo echo "Nothing passed." func2 # Called with no params echo echo "Zero-length parameter passed." func2 "" # Called with zero-length param echo echo "Null parameter passed." func2 "$uninitialized_param" # Called with uninitialized param echo echo "One parameter passed." func2 first # Called with one param echo echo "Two parameters passed." func2 first second # Called with two params echo echo "\"\" \"second\" passed." func2 "" second # Called with zero-length first parameter echo # and ASCII string as a second one. exit 0 |
The shift command works on arguments passed to functions (see Example 34-6). |
In contrast to certain other programming languages, shell scripts normally pass only value parameters to functions. [1] Variable names (which are actually pointers), if passed as parameters to functions, will be treated as string literals and cannot be dereferenced. Functions interpret their arguments literally. |
Functions return a value, called an exit status. The exit status may be explicitly specified by a return statement, otherwise it is the exit status of the last command in the function (0 if successful, and a non-zero error code if not). This exit status may be used in the script by referencing it as $?. This mechanism effectively permits script functions to have a "return value" similar to C functions.
Terminates a function. A return command [2] optionally takes an integer argument, which is returned to the calling script as the "exit status" of the function, and this exit status is assigned to the variable $?.
Example 23-3. Maximum of two numbers
#!/bin/bash # max.sh: Maximum of two integers. E_PARAM_ERR=-198 # If less than 2 params passed to function. EQUAL=-199 # Return value if both params equal. max2 () # Returns larger of two numbers. { # Note: numbers compared must be less than 257. if [ -z "$2" ] then return $E_PARAM_ERR fi if [ "$1" -eq "$2" ] then return $EQUAL else if [ "$1" -gt "$2" ] then return $1 else return $2 fi fi } max2 33 34 return_val=$? if [ "$return_val" -eq $E_PARAM_ERR ] then echo "Need to pass two parameters to the function." elif [ "$return_val" -eq $EQUAL ] then echo "The two numbers are equal." else echo "The larger of the two numbers is $return_val." fi exit 0 # Exercise (easy): # --------------- # Convert this to an interactive script, #+ that is, have the script ask for input (two numbers). |
For a function to return a string or array, use a dedicated variable.
|
Example 23-4. Converting numbers to Roman numerals
#!/bin/bash # Arabic number to Roman numeral conversion # Range: 0 - 200 # It's crude, but it works. # Extending the range and otherwise improving the script is left as an exercise. # Usage: roman number-to-convert LIMIT=200 E_ARG_ERR=65 E_OUT_OF_RANGE=66 if [ -z "$1" ] then echo "Usage: `basename $0` number-to-convert" exit $E_ARG_ERR fi num=$1 if [ "$num" -gt $LIMIT ] then echo "Out of range!" exit $E_OUT_OF_RANGE fi to_roman () # Must declare function before first call to it. { number=$1 factor=$2 rchar=$3 let "remainder = number - factor" while [ "$remainder" -ge 0 ] do echo -n $rchar let "number -= factor" let "remainder = number - factor" done return $number # Exercise: # -------- # Explain how this function works. # Hint: division by successive subtraction. } to_roman $num 100 C num=$? to_roman $num 90 LXXXX num=$? to_roman $num 50 L num=$? to_roman $num 40 XL num=$? to_roman $num 10 X num=$? to_roman $num 9 IX num=$? to_roman $num 5 V num=$? to_roman $num 4 IV num=$? to_roman $num 1 I echo exit 0 |
See also Example 10-27.
The largest positive integer a function can return is 256. The return command is closely tied to the concept of exit status, which accounts for this particular limitation. Fortunately, there are various workarounds for those situations requiring a large integer return value from a function. Example 23-5. Testing large return values in a function
As we have seen, a function can return a large negative value. This also permits returning large positive integer, using a bit of trickery. An alternate method of accomplishing this is to simply assign the "return value" to a global variable.
Example 23-6. Comparing two large integers
See also Example A-7. Exercise: Using what we have just learned, extend the previous Roman numerals example to accept arbitrarily large input. |
A function is essentially a code block, which means its stdin can be redirected (as in Example 4-1).
Example 23-7. Real name from username
#!/bin/bash # From username, gets "real name" from /etc/passwd. ARGCOUNT=1 # Expect one arg. E_WRONGARGS=65 file=/etc/passwd pattern=$1 if [ $# -ne "$ARGCOUNT" ] then echo "Usage: `basename $0` USERNAME" exit $E_WRONGARGS fi file_excerpt () # Scan file for pattern, the print relevant portion of line. { while read line # while does not necessarily need "[ condition]" do echo "$line" | grep $1 | awk -F":" '{ print $5 }' # Have awk use ":" delimiter. done } <$file # Redirect into function's stdin. file_excerpt $pattern # Yes, this entire script could be reduced to # grep PATTERN /etc/passwd | awk -F":" '{ print $5 }' # or # awk -F: '/PATTERN/ {print $5}' # or # awk -F: '($1 == "username") { print $5 }' # real name from username # However, it might not be as instructive. exit 0 |
There is an alternative, and perhaps less confusing method of redirecting a function's stdin. This involves redirecting the stdin to an embedded bracketed code block within the function.
# Instead of: Function () { ... } < file # Try this: Function () { { ... } < file } # Similarly, Function () # This works. { { echo $* } | tr a b } Function () # This doesn't work. { echo $* } | tr a b # A nested code block is mandatory here. # Thanks, S.C. |
[1] | Indirect variable references (see Example 35-2) provide a clumsy sort of mechanism for passing variable pointers to functions.
| |
[2] | The return command is a Bash builtin. |