
Untitled
By: a guest on
Jun 9th, 2012 | syntax:
None | size: 1.12 KB | hits: 23 | expires: Never
Converting Bash command line options to variable name
if [ ! -n $1 ]; then
echo "Error"
fi
if test -s "${foo+set}"
if [ -z "$1" ]; then
echo "Error"
exit 1
fi
#!/bin/bash
USAGE="$0: [-a] [--alpha] [-b type] [--beta file] [-g|--gamma] args..."
ARGS=`POSIXLY_CORRECT=1 getopt -n "$0" -s bash -o ab:g -l alpha,beta:,gamma -- "$@"`
if [ $? -ne 0 ]
then
echo "$USAGE" >&2
exit 1
fi
eval set -- "$ARGS"
unset ARGS
while true
do
case "$1" in
-a) echo "Option a"; shift;;
--alpha) echo "Option alpha"; shift;;
-b) echo "Option b, arg '$2'"; shift 2;;
--beta) echo "Option beta, arg '$2'"; shift 2;;
-g|--gamma) echo "Option g or gamma"; shift;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
echo Remaining args
for arg in "$@"
do
echo '--> '"`$arg'"
done
exit 0
if [[ -z $1 ]]; then
echo "Error"
fi
if [ ! -n "$1" ]; then
echo "Error"
fi
-z string
True if the length of string is zero.
-n string
True if the length of string is non-zero.
-v varname
True if the shell variable varname is set (has been assigned a value).