Created
August 25, 2016 13:30
-
-
Save adamsdesk/fcf806338cf47986414496816c033c2f to your computer and use it in GitHub Desktop.
BASH getopts example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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