Skip to content

Instantly share code, notes, and snippets.

@brpaz
Created January 24, 2015 13:11
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 brpaz/e4db5057f4d24c9b2500 to your computer and use it in GitHub Desktop.
Save brpaz/e4db5057f4d24c9b2500 to your computer and use it in GitHub Desktop.
Example how to use getops bash function to parse cmd line options. #bash
#!/bin/sh
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
# Initialize our own variables:
output_file=""
verbose=0
while getopts "h?vf:" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
v) verbose=1
;;
f) output_file=$OPTARG
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
echo "verbose=$verbose, output_file='$output_file', Leftovers: $@"
# End of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment