【问题标题】:Shell script to loop command line switches and arguments用于循环命令行开关和参数的 Shell 脚本
【发布时间】:2013-05-04 08:18:12
【问题描述】:

我正在尝试编写一个 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

如何检测开关并使用下一个参数作为开关的参数而不管开关的数量是多少?

【问题讨论】:

标签: linux bash shell


【解决方案1】:

您可以使用“shift”来删除第一个参数..
例如:

arg1=$1; shift
arg2=$1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 2012-11-17
    • 2015-08-20
    • 2010-09-07
    相关资源
    最近更新 更多