我正在运行bash V4.2,并且正在尝试使用内置命令getopts
解析命令行参数,
但是getopts似乎不能正确地解析它,如果-s
不是第一个参数,它就不会被解析
未解析-s
:
%> ./getopt.sh aaa -s aaa
aaa
这一个被解析了
%> ./getopt.sh -s aaa
s: aaa
aaa
脚本在这里:
#!/bin/bash
while getopts "bs:" opt
do
case $opt in
s)
echo "s: $OPTARG"
;;
*)
echo not supported
;;
esac
shift
done
echo $1
发布于 2013-07-07 16:55:43
与(较旧的) getopt
不同,getopts
不会重新排列参数以将选项放在第一位。因此,在
./getopt.sh arg1 -s opt1
一旦看到非选项arg1
,选项解析就会停止。
https://stackoverflow.com/questions/17510395
复制相似问题