在bash脚本中访问第9个之后的命令行参数 [英] Accessing command line arguments beyond the 9th in bash script

查看:50
本文介绍了在bash脚本中访问第9个之后的命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用bash读取命令行参数,但是在第10列上有读取困难.这是我的示例脚本:

I am trying to read command line arguments in bash but I have problems to read on the 10th column. Here is my sample script:

#-------------------------------------------------------
#!/bin/bash
an=$2  mn=$4  dy=$6  der=$8  new=$10  sec=(${12} ${13})
echo $an $mn  $dy   $der  $new  $sec 
#--------------------------------------------------------

我已经运行了上面的脚本"test.sh",

I have run the above script "test.sh" as

./test.sh -yr cat  -mn Jan  -dy  tuesday  -der tt  -new car -sec 001 001

输出为:

cat Jan tuesday tt -yr0 001

但是对于变量$ new($ 10),答案应该是 car ,但是我得到的是 -yr0

But for variable $new ($10) the answer should have been car but I get -yr0

知道为什么吗?

推荐答案

此为解决方法:

#!/bin/bash
an=$2  mn=$4  dy=$6  der=$8  new=${10}  sec="${12} ${13}"
echo $an $mn  $dy   $der  $new  $sec 

  1. 使用$ {10}代替$ 10(即附加了0的"$ 1")
  2. sec ="..."而不是使用括号,除非您打算使用数组

这篇关于在bash脚本中访问第9个之后的命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆