[PLUG] Question about bash scripting -- specifically `set -u'

Travis Spencer travislspencer at gmail.com
Sat Sep 10 19:33:19 UTC 2005


Hey All,

I want to play it safe and tell bash to disallow the use of unbound
variables by calling `set -u' at the top of my script.  This is fine
until I check to see if the user has provided the script's required
arguments.  If they haven't, I get an `unbound variable' error.  For
instance, take this little script:

#! /bin/bash

set -u

if [ -n "$1" ]; then
    echo "OK.  \$1 = $1"
else
    echo "You must provide one argument."
    echo "Usage: ${0##*/} ARG"
    exit 1
fi

If the user provides the one required argument `$1' is bound and the
condition works fine; however, if the user invokes the script without
the required argument, the check for `$1' will cause an error such as
`argtest.sh: line 5: $1: unbound variable'.

When using the nounset option, how are you supposed to check for
arguments that may not have been provided?

TIA!

-- 

Regards,

Travis Spencer



More information about the PLUG mailing list