Getopt example scripts
Wed, 10/29/2014 - 12:33
Several example scripts illustrating how you can use getopt(1) with bash and tcsh. Note that getopt(1) should also work with other sh-derived and csh-derived shells. All programs are also attached below.
Testing which getopt(1) you are using
These scripts checks whether you are using the enhanced getopt(1) described on this site or an older version.
The version for bash and other sh-compatible shells (getopt-test.bash):
#!/bin/bash
if `getopt -T >/dev/null 2>&1` ; [ $? = 4 ] ; then
echo "Enhanced getopt(1)"
else
echo "Old getopt(1)"
fi
The version for tcsh and other csh-compatible shells (getopt-test.tcsh):
#!/bin/tcsh
getopt -T >&/dev/null
if ( $status == 4) then
echo "Enhanced getopt(1)"
else
echo "Old getopt(1)"
endi
Using getopt(1)
These scripts show how you can safely use getopt(1) for parsing a command-line.
The version for bash and other sh-compatible shells (getopt-parse.bash):
#!/bin/bash
# A small example program for using the new getopt(1) program.
# This program will only work with bash(1)
# An similar program using the tcsh(1) script language can be found
# as parse.tcsh
# Example input and output (from the bash prompt):
# ./parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long "
# Option a
# Option c, no argument
# Option c, argument `more'
# Option b, argument ` very long '
# Remaining arguments:
# --> `par1'
# --> `another arg'
# --> `wow!*\?'
# Note that we use `"$@"' to let each command-line parameter expand to a
# separate word. The quotes around `$@' are essential!
# We need TEMP as the `eval set --' would nuke the return value of getopt.
TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \
-n 'example.bash' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
case "$1" in
-a|--a-long) echo "Option a" ; shift ;;
-b|--b-long) echo "Option b, argument \`$2'" ; shift 2 ;;
-c|--c-long)
# c has an optional argument. As we are in quoted mode,
# an empty parameter will be generated if its optional
# argument is not found.
case "$2" in
"") echo "Option c, no argument"; shift 2 ;;
*) echo "Option c, argument \`$2'" ; shift 2 ;;
esac ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
echo "Remaining arguments:"
for arg do echo '--> '"\`$arg'" ; done
The version for tcsh and other csh-compatible shells (getopt-parse.tcsh):
#!/bin/tcsh
# A small example program for using the new getopt(1) program.
# This program will only work with tcsh(1)
# An similar program using the bash(1) script language can be found
# as parse.bash
# Example input and output (from the tcsh prompt):
# ./parse.tcsh -a par1 'another arg' --c-long 'wow\!*\?' -cmore -b " very long "
# Option a
# Option c, no argument
# Option c, argument `more'
# Option b, argument ` very long '
# Remaining arguments:
# --> `par1'
# --> `another arg'
# --> `wow!*\?'
# Note that we had to escape the exclamation mark in the wow-argument. This
# is _not_ a problem with getopt, but with the tcsh command parsing. If you
# would give the same line from the bash prompt (ie. call ./parse.tcsh),
# you could remove the exclamation mark.
# This is a bit tricky. We use a temp variable, to be able to check the
# return value of getopt (eval nukes it). argv contains the command arguments
# as a list. The ':q` copies that list without doing any substitutions:
# each element of argv becomes a separate argument for getopt. The braces
# are needed because the result is also a list.
set temp=(`getopt -s tcsh -o ab:c:: --long a-long,b-long:,c-long:: -- $argv:q`)
if ($? != 0) then
echo "Terminating..." >/dev/stderr
exit 1
endif
# Now we do the eval part. As the result is a list, we need braces. But they
# must be quoted, because they must be evaluated when the eval is called.
# The 'q` stops doing any silly substitutions.
eval set argv=\($temp:q\)
while (1)
switch($1:q)
case -a:
case --a-long:
echo "Option a" ; shift
breaksw;
case -b:
case --b-long:
echo "Option b, argument "\`$2:q\' ; shift ; shift
breaksw
case -c:
case --c-long:
# c has an optional argument. As we are in quoted mode,
# an empty parameter will be generated if its optional
# argument is not found.
if ($2:q == "") then
echo "Option c, no argument"
else
echo "Option c, argument "\`$2:q\'
endif
shift; shift
breaksw
case --:
shift
break
default:
echo "Internal error!" ; exit 1
endsw
end
echo "Remaining arguments:"
# foreach el ($argv:q) created problems for some tcsh-versions (at least
# 6.02). So we use another shift-loop here:
while ($#argv > 0)
echo '--> '\`$1:q\'
shift
end