Skip to content

Instantly share code, notes, and snippets.

@marcioj
Last active December 1, 2015 15:22
Show Gist options
  • Save marcioj/2a06e06a9aa4cc579b70 to your computer and use it in GitHub Desktop.
Save marcioj/2a06e06a9aa4cc579b70 to your computer and use it in GitHub Desktop.
Parsing CLI options in bash
# taken from http://www.unix.com/302182212-post4.html
while true
do
case $# in 0) break ;; esac
case $1 in
-d|--dir) shift; dir=$1; shift ;;
-u|--user) shift; user=$1; shift ;;
-a|--all) shift; all=true ;;
-|--) shift; break;;
-h|--help) cat <<______EOF >&2; exit 0 ;;
Syntax: $0 [ -d directory | --dir directory ] [ -u user | --user user ] | --help
______EOF
-*) cat <<______EOF >&2; exit 2 ;;
Invalid option $1. Try $0 --help to see available options.
______EOF
*) break ;;
esac
done
set -e
# other stuffs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment