I'm writing a simple bash script and I would like it to accept parameters from the command line in any order.
I've browsed around the web and wrote a simple function with a case statement in a while loop. Right now, the 'any order' part works - but it only picks up the first parameter I set. I'm certainly doing something wrong but scripting is quite new to me and I hadn't been able to figure it out - your help would be greatly appreciated. The flags part of the script is as follows:
#Parameters - source,destination,credentials,bandwidth,timeout,port,help
flags () {
while test $# -gt 0; do
case "$1" in
-s|--source)
shift
if test $# -gt 0; then
export SOURCE=$1
else
echo "No source directory specified!"
exit 1
fi
;;
-d|--destination)
shift
if test $# -gt 0; then
export DESTINATION=$1
fi
;;
-c|--credentials)
shift
if test $# -gt 0; then
export CREDENTIALS=$1
fi
;;
-b|--bandwidth)
shift
if test $# -gt 0; then
export BANDWIDTH=$1
fi
;;
-t|--timeout)
shift
if test $# -gt 0; then
export TIMEOUT=$1
fi
;;
-p|--port)
shift
if test $# -gt 0; then
export PORT=$1
fi
;;
-h|--help)
shift
if test $# -gt 0; then
echo "Help goes here"
fi
;;
-l|--compression-level)
shift
if test $# -gt 0; then
export COMPRESS_LEVEL=$1
fi
;;
*)
break
;;
esac
done
}
flags "$@"
echo "source is $SOURCE, destination is $DESTINATION, credentials are $CREDENTIALS, bandwidth is $BANDWIDTH, timeout is $TIMEOUT, port is $PORT"
Ideally, some of those parameters would be mandatory, and others optional - but that's not a must.
How can I fix this script to accept any of those parameters (both long and short forms, ideally) in any order?