Skip to content

Instantly share code, notes, and snippets.

@adamsdesk
Created August 25, 2016 13:30
Show Gist options
  • Save adamsdesk/fcf806338cf47986414496816c033c2f to your computer and use it in GitHub Desktop.
Save adamsdesk/fcf806338cf47986414496816c033c2f to your computer and use it in GitHub Desktop.
BASH getopts example
#!/bin/bash
if (($# == 0)); then
echo "`basename $0`: You must specify one of the '-hlrv' options."
echo "Try '`basename $0` -h' for more information."
exit 1
fi
while getopts ":hlr" opt; do
case $opt in
l)
echo "-l was triggered!" >&2
;;
r)
echo "-r was triggered!" >&2
;;
h)
echo "-h was triggered!" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment