Saturday, December 8, 2007
Setting default value for Shell Variable
To set a default value for a BASH variable, the syntax is: (good for setting default value for a BASH command line parameter)
VARIABLE=${1:-DEFAULTVALUE} #set VARIABLE with the value of 1st Arg to the script,
#If 1st arg is not entered, set it to DEFAULTVALUE
The following simple script illustrate this:
tmpdir=/tmp
defvalue=1
DIR=${1:-$tmpdir} # Defaults to /tmp dir.
VALUE=${2:-$defvalue} # Default value is 1.
echo $DIR
echo $VALUE
Now while running the script, specify values for both the arguments.
$ ./defvaue.sh /dev 23
/dev
23
This time don't mention their values.
$ ./defvaue.sh
/tmp
1
so
DIR=${1:-$tmpdir}
VALUE=${2:-$defvalue}
is a replacement for the following:
[ -z $1 ] && DIR="/tmp"
[ -z $2 ] && VALUE=1
Subscribe to:
Post Comments (Atom)
© Jadu Saikia www.UNIXCL.com
No comments:
Post a Comment