Skip to content

Instantly share code, notes, and snippets.

@NeilRobbins
Created January 8, 2014 14:04
Show Gist options
  • Save NeilRobbins/8317189 to your computer and use it in GitHub Desktop.
Save NeilRobbins/8317189 to your computer and use it in GitHub Desktop.
Some simple argument handling for use in shell scripts. HT to an answer on StackOverflow.
#! /bin/sh -
#argument defaults
x="foo"
y="bar"
# argument handling
while test $# -gt 0
do
case "$1" in
-s) x=${2}
echo "x set to: " ${2}
;;
-p) y=${2}
echo "y set to: " ${2}
;;
-*) echo "bad option $1"
;;
*) #ignore
;;
esac
shift
done
# inform if defaults will be used
if [ "${x}" = "foo" ]; then
echo "No x supplied. Default x will be used: " ${x}
fi
if [ "${y}" = "bar" ]; then
echo "No y supplied. Default y will be used: " ${y}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment