Skip to content

Instantly share code, notes, and snippets.

@yuanqing
Created January 17, 2015 18:09
Show Gist options
  • Save yuanqing/52155a2eecbfbe8d8617 to your computer and use it in GitHub Desktop.
Save yuanqing/52155a2eecbfbe8d8617 to your computer and use it in GitHub Desktop.
#!/bin/sh
error() {
echo "foo: $1"
exit 1
}
while [ $# -gt 0 ]; do
case "$1" in
-v)
# option with value
if [ -z "$2" ]; then
error "option $1 expects a value"
fi
echo "$1: $2"
shift
;;
-o)
# option without value
echo "$1: true"
;;
*)
# non-option argument
if [[ $1 == -* ]] ; then
error "option $1 is invalid"
fi
echo "$1"
;;
esac
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment