Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [79]

By Root 890 0
the larger program.

Functions must be defined before they can be used. This is done either at the beginning of a script, or by having them in a separate file and sourcing them with the "dot" (.) command. (The . command is discussed later on in Section 7.9.) They are defined as shown in Example 6-4.

Example 6-4. Wait for a user to log in, function version

# wait_for_user --- wait for a user to log in

#

# usage: wait_for_user user [ sleeptime ]

wait_for_user ( ) {

until who | grep "$1" > /dev/null

do

sleep ${2:-30}

done

}

Functions are invoked (executed) the same way a command is: by providing its name and any corresponding arguments. The wait_for_user function can be invoked in one of two ways:

wait_for_user tolstoy Wait for tolstoy, check every 30 seconds

wait_for_user tolstoy 60 Wait for tolstoy, check every 60 seconds

Within a function body, the positional parameters ($1, $2, etc., $#, $*, and $@) refer to the function's arguments. The parent script's arguments are temporarily shadowed, or hidden, by the function's arguments. $0 remains the name of the parent script. When the function finishes, the original command-line arguments are restored.

Within a shell function, the return command serves the same function as exit and works the same way:

answer_the_question ( ) {

...

return 42

}

Note that using exit in the body of a shell function terminates the entire shell script!

* * *

return


Usage

return [ exit-value ]

Purpose

To return an exit value from a shell function to the calling script.

Major options

None.

Behavior

The default exit status used if none is supplied is the exit status of the last command executed. If that is what you want, it is best to do this explicitly in the shell function:

return $?

Caveats

Some shells allow the use of return within a script but outside of a function body to mean the same as exit. This usage isn't recommended, for portability reasons.

* * *

Since the return statement returns an exit value to the caller, you can use functions in if and while statements. For example, instead of using test to compare two strings, you could use the shell's constructs to do so:

# equal --- compare two strings

equal ( ) {

case "$1" in

"$2") return 0 ;; # they match

esac

return 1 # they don't match

}

if equal "$a" "$b" ...

if ! equal "$c" "$d" ...

One item to note here is the use of double quotes in the case pattern list. This forces the value to be treated as a literal string, rather than as a shell pattern. The quotes around $1 don't hurt, but aren't necessary here.

Functions return integer exit status values, just like commands. For functions also, zero means success, nonzero means failure. To return some other value, a function should either set a global shell variable, or print the value, with the parent script capturing it using command substitution (see Section 7.6):

myfunc ( ) {

...

}

...

x=$(myfunc "$@") Call myfunc, save output

Example 5-6 in Section 5.5, showed a nine-stage pipeline to produce a sorted list of SGML/XML tags from an input file. It worked only on the one file named on the command line. We can use a for loop for argument processing, and a shell function to encapsulate the pipeline, in order to easily process multiple files. The modified script is shown in Example 6-5.

Example 6-5. Making an SGML tag list from multiple files

#! /bin/sh -

# Read one or more HTML/SGML/XML files given on the command

# line containing markup like word and output on

# standard output a tab-separated list of

#

# count word tag filename

#

# sorted by ascending word and tag.

#

# Usage:

# taglist xml-files

process( ) {

cat "$1" |

sed -e 's#systemitem *role="url"#URL#g' -e 's#/systemitem#/URL#' |

tr ' ( ){ }[ ]' '\n\n\n\n\n\n\n' |

egrep '>[^<>]+awk -F'[<>]' -v FILE="$1" \

'{ printf("%-31s\t%-15s\t%s\n", $3, $2, FILE) }' |

sort |

uniq -c |

sort -k2 -k3 |

awk '{

print ($2 = = Last) ? ($0 " <----") : $0

Last = $2

}'

}

for f in "$@"

do

process "$f"

done

Functions

Return Main Page Previous Page Next Page

®Online Book Reader