我正在尝试编写一个 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
无论开关的数量如何,如何检测开关并使用下一个参数作为开关的参数?