Skip to content

Instantly share code, notes, and snippets.

@gibatronic
Created April 11, 2016 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gibatronic/8b316efaece6429b333ad37d045682a3 to your computer and use it in GitHub Desktop.
Save gibatronic/8b316efaece6429b333ad37d045682a3 to your computer and use it in GitHub Desktop.
bash snippet to parse arguments
#!/usr/bin/env bash
NAME=${NAME:-unknown}
main() {
while [ "$#" != 0 ]; do
case "$1" in
-h|--help) usage ;;
-n|--name) shift; set_name "$1" ;;
*) work $1; exit ;;
esac
shift
done
}
set_name() {
NAME="$1"
}
usage() {
echo
echo ' Usage:'
echo ' options [options] <command>'
echo
echo ' Options:'
echo ' -h --help print usage information'
echo ' -n --name your username'
echo
}
work() {
echo "NAME: "'"'"${NAME}"'"'
echo $1
}
[ "$#" = 0 ] && usage || main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment