Last active
August 29, 2015 14:07
-
-
Save te2u/4899b57040229470c5f7 to your computer and use it in GitHub Desktop.
bashでコマンドオプションを解析する(getopts)
This file contains hidden or 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 | |
set -eu | |
help_exit() { | |
echo "Usage:" `basename $0` "[-f] [-b dir] [arg ...]" 1>&2 | |
exit ${1:-0} | |
} | |
FOO= | |
BAR= | |
while getopts fb:h OPT | |
do | |
case $OPT in | |
f) FOO=1 | |
;; | |
b) BAR=$OPTARG | |
;; | |
h) help_exit | |
;; | |
\?) help_exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
echo "FOO:$FOO" | |
echo "BAR:$BAR" | |
echo "ARGS:""$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment