如何解决读入数组中的很多参数
我有一个hnr3 []数组,带有66个“数据”。
所有开头都是0。
我需要通过参数{batch.sh arg1 arg2 arg3
,例如batch.sh /tmp/ 19 33 55
来指示脚本hnr3 [19],hnr3 [33]和hnr3 [55]设置为1。 >
我的问题是,我不知道hnr3参数是从$ 2,$ 3还是$ 4开始,也不知道会有多少个。
反正有什么办法使它起作用,又如何? (我是数组部分的新手)
已添加:
怎样在参数中添加一个“关键字”,例如batch.sh /tmp/ -data 19,33,55
,然后我们将$ *作为字符串读取,并以某种方式读取 -data
解决方法
如果数字前的参数是可选的,则可以使用getopts处理它们:
#!/bin/bash
manual=0
reset=0
tmpdir=""
while getopts mrt: c; do
case $c in
m) manual=1;;
r) reset=1;;
t) tmpdir="$OPTARG";;
# You can add others optional arguments here
esac
done
shift $((OPTIND - 1))
for arg; do
hnr3[arg]=1
done
declare -p manual
declare -p reset
declare -p tmpdir
declare -p hnr3
调用方法:
./test.sh -m -t /tmp 19 33 55
# results:
declare -- manual="1"
declare -- reset="0"
declare -- tmpdir="/tmp"
declare -a hnr3=([19]="1" [33]="1" [55]="1")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。