Skip to content

Instantly share code, notes, and snippets.

@te2u
Last active August 29, 2015 14:07
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 te2u/4899b57040229470c5f7 to your computer and use it in GitHub Desktop.
Save te2u/4899b57040229470c5f7 to your computer and use it in GitHub Desktop.
bashでコマンドオプションを解析する(getopts)
#!/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