【问题标题】:How get next to next argument in linux shell script?如何在 linux shell 脚本中获取下一个参数?
【发布时间】:2014-01-02 12:00:04
【问题描述】:

我写了简单的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)?

问候 杰伊什

【问题讨论】:

  • 问题是什么??
  • 你能详细说明一下你想用上面的脚本做什么吗?
  • 好吧,如果我理解正确,您正在尝试使用 -A 选项绑定 1 和 2,您可以使用 ./test.sh -A 1 -B 2 之类的另一个开关传递 2,然后解析它们。这是一个有用的链接 => stackoverflow.com/questions/16483119/…

标签: linux shell scripting


【解决方案1】:

有多种选择。

(1)你可以使用shift$1

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

(2) 可以遍历 args:

 for i
 do
    # do something with $i
 done

还有其他选择。

【讨论】:

  • 感谢 lgor Chubin。这对我很有帮助。
猜你喜欢
  • 2017-12-08
  • 1970-01-01
  • 2020-07-28
  • 2014-03-14
  • 1970-01-01
  • 2012-06-18
  • 1970-01-01
  • 1970-01-01
  • 2013-11-09
相关资源
最近更新 更多