我正在尝试编写一个 bash 脚本来检查命令行开关并将下一个参数用作开关参数。
例子:
sh myprogram.sh -s switchArg -p programArg start
在我的脚本中,我试图以这种方式循环它:
if [ "$#" -gt 2 ]
then
{
i=1
for arg in "$@"
do
if [ "$arg" == "-s" ] || [ "$arg" == "-S" ]
then
{
i=$((i++))
myArg=$i # This then gets used later in my script
continue
}
elif [ "$arg" == "-p" ] || [ "$arg" == "-P" ]
then
{
i=$((i++))
myArg2=$i # This then gets used later in my script
continue
}
else
{
echo "illegal option: $arg"
}
done
do stuff
}
fi
无论开关的数量如何,我如何检测开关并使用下一个参数作为开关的参数?
最佳答案
您可以使用“shift”来删除第一个参数..
例如:
arg1=$1; shift
arg2=$1
关于linux - 用于循环命令行开关和参数的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16468840/