如果您只有一种选择,有时只需检查一下会更简单$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