如何在linux shell脚本中找到下一个参数?

时间:2014-01-02 12:00:04

标签: linux shell scripting

我编写了简单的shell脚本test.sh,如下所示:

while getopts ":A:" OPTION
do 
   case $OPTION in
   A)
      echo $OPTARG
   ?)
      echo "no option"

  esac
done

按如下步骤执行脚本

$ ./test.sh -A 1 2

现在如果得到$ OPTARG的参数1,但我如何访问第二个参数(在这种情况下为2)?

此致 Jayesh

1 个答案:

答案 0 :(得分:3)

有几种选择。

(1)您可以使用shift并使用$1

 while -n "$1"
 do  
  # do something with $1
  shift
 done

(2)你可以遍历args:

 for i
 do
    # do something with $i
 done

还有其他选择。