我有一个代码:
while getopts ab:cde:f opt
do
case ${opt} in
b|e)
[[ ${OPTARG} = -* ]] && usage "Invalid parameter \"${OPTARG}\" provided for agurment \"- ${opt}!\""
[[ ${#OPTARG} -eq 0 ]] && usage "Argument \"-${opt}\" requires a parameter!${OPTARG}"
;;
esac
case $opt in
a) minusa=$opt;;
b) minusb=$opt
file_b=$OPTARG;;
c) minusc=$opt;;
d) minusd=$opt;;
e) minuse=$opt
file_e=$OPTARG;;
f) minusf=$opt;;
/?) echo Unrecognized parameter
exit 1;;
esac
done
echo "minusa:$minusa","minusb:$minusb","file_b:$file_b","minusc:$minusc","minusd:$minusd","minuse:$minuse","file_e:$file_e","minusf:$minusf"
简单的代码,只是为了理解getopts
命令的行为。当我像这样运行脚本时:
./eg2 -b -f
./eg2: line 7: usage: command not found
minusa:,minusb:b,file_b:-f,minusc:,minusd:,minuse:,file_e:,minusf:
它将 option 的参数-b
作为-f
。而我想打印:
[[ ${OPTARG} = -* ]] && usage "Invalid parameter \"${OPTARG}\" provided for agurment \"-${opt}!\""
我到底在哪里出错了?同样对于选项-b
,-e
如果没有参数,我想打印:
[[ ${#OPTARG} -eq 0 ]] && usage "Argument \"-${opt}\" requires a parameter!${OPTARG}"
请解释。