オプションが 1 つしかない場合は、チェックする方が簡単な場合があります$1
。
# put download command here
if (( $? != 0 )) && [[ $1 != -c ]]; then
echo -e "can't reach the url, \n aborting"
exit
fi
# put stuff to do if continuing here
他のオプションを受け入れる場合は、引数が含まれている可能性があるため、getopts
を使用する必要があります。
#!/bin/bash
usage () { echo "Here is how to use this program"; }
cont=false
# g and m require arguments, c and h do not, the initial colon is for silent error handling
options=':cg:hm:' # additional option characters go here
while getopts $options option
do
case $option in
c ) cont=true;;
g ) echo "The argument for -g is $OPTARG"; g_option=$OPTARG;; #placeholder example
h ) usage; exit;;
m ) echo "The argument for -m is $OPTARG"; m_option=$OPTARG;; #placeholder example
# more option processing can go here
\? ) echo "Unknown option: -$OPTARG"
: ) echo "Missing option argument for -$OPTARG";;
* ) echo "Unimplimented option: -$OPTARG";;
esac
done
shift $(($OPTIND - 1))
# put download command here
if (( $? != 0 )) && ! $cont; then
echo -e "can't reach the url, \n aborting"
exit
fi
# put stuff to do if continuing here