Skip to content

Instantly share code, notes, and snippets.

@yuanqing
Created January 17, 2015 18:33
Show Gist options
  • Save yuanqing/7bcc36bccd0f9886c9dc to your computer and use it in GitHub Desktop.
Save yuanqing/7bcc36bccd0f9886c9dc to your computer and use it in GitHub Desktop.
function error() {
echo "foo: $1"
exit 1
}
# loop over arguments
while [ $# -gt 0 ]; do
case "$1" in
# flag with value
-c | -r | -i | -o)
if [ -z "$2" ]; then
error "option $1 expects a value"
fi
echo "$1: $2"
shift
;;
# flag without value
-o)
echo "$1: true"
;;
# value without flag
*)
# exit if the value starts with '-'
if [[ "$1" == -* ]] ; then
error "option $1 is invalid"
fi
# concatenate the current value to $vals
vals="$vals $1"
;;
esac
shift
done
# set arguments to $vals
set -- $vals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment