Bash 是否可以调用一个选项并将其';在一个命令中多次使用s参数?
这是我的bash案例Bash 是否可以调用一个选项并将其';在一个命令中多次使用s参数?,bash,getopt,Bash,Getopt,这是我的bash案例测试getopt.sh来理解bash中的getopt OPTS=$(getopt -o d:eh -- "$@") eval set -- "$OPTS" while true; do case "$1" in -d ) if [[ $2 == "a" ]];then echo "i am -d's arg :a" elif [[ $2 == "b" ]
测试getopt.sh
来理解bash中的getopt
OPTS=$(getopt -o d:eh -- "$@")
eval set -- "$OPTS"
while true; do
case "$1" in
-d )
if [[ $2 == "a" ]];then
echo "i am -d's arg :a"
elif [[ $2 == "b" ]];then
echo "i am -d's arg :b"
fi
shift;;
-e )
echo "i am e"
shift;;
-h)
echo "help you"
shift;;
-- )
shift;;
*)
break;;
esac
done
调用选项及其参数,一次调用一个选项及其参数
bash test-get.sh -d a
i am -d's arg :a
其他论点
bash test-get.sh -d b
i am -d's arg :b
我想调用选项,它是一个命令中的所有参数
bash test-get.sh -d a -d b
i am -d's arg :a
有没有办法获得以下预期输出
bash test-get.sh -d a -d b
i am -d's arg :a
i am -d's arg :b
基本问题是,当您处理一个带有参数的选项时,需要将该选项及其参数移动两次,才能从参数列表中删除该选项及其参数。像这样:
OPTS=$(getopt -o d:eh -- "$@")
eval set -- "$OPTS"
while true; do
case "$1" in
-d )
if [[ $2 == "a" ]];then
echo "i am -d's arg :a"
elif [[ $2 == "b" ]];then
echo "i am -d's arg :b"
fi
shift 2;; # <-- The "2" here is the only change
-e )
echo "i am e"
shift;;
-h)
echo "help you"
shift;;
-- )
shift;;
*)
break;;
esac
done
OPTS=$(getopt-od:eh--“$”)
评估集--“$OPTS”
虽然真实;做
案件“$1”
-(d)
如果[$2==“a”];然后
echo“我是d的arg:a”
elif[$2==“b”];然后
回声“我是-d的arg:b”
fi
第二班;#我相信shift;移位代码>也可以工作,当然shift 2代码>更容易+1.