Last active
October 18, 2017 02:35
-
-
Save ktanaka117/8bfe5d410495819e4f4333729c9a1726 to your computer and use it in GitHub Desktop.
シェルスクリプトで必須オプションが入力されていなければ処理を終了する書き方。
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 | |
function lack_of_necessary_param() { | |
echo "-aオプションは必須オプションです。-aオプションを必ず使用してください。" | |
exit 1 | |
} | |
IS_THERE_NECESSARY_OPT=false | |
# オプションを取得する | |
while getopts a:b: OPT | |
do | |
case $OPT in | |
a ) | |
IS_THERE_NECESSARY_OPT=true | |
echo $OPTARG | |
;; | |
b ) | |
echo $OPTARG | |
;; | |
esac | |
done | |
if [ "${IS_THERE_NECESSARY_OPT}" != true ]; then | |
lack_of_necessary_param | |
fi; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
typo
necessaly
→necessary
https://gist.github.com/ktanaka117/8bfe5d410495819e4f4333729c9a1726#file-necessary_option-sh-L27