【发布时间】:2014-02-19 06:00:08
【问题描述】:
我在 .sh 文件中定义了一个数组,如下所示:
hosts=(NODE IPS)
hosts[0]="1.110,Node-01"
hosts[1]="1.111,Node-02"
hosts[2]="1.112,Node-03"
hosts[3]="1.113,Node-04"
hosts[4]="1.114,Node-05"
hosts[5]="1.115,Node-06"
如何遍历数组,但根据逗号分割每个索引,并将值存储在变量中,如下所示:
for index in ${!hosts[*]}
do
ip = ## some way to split the string, and get the left side of the index
hostname = ## some way to split the string, and get the right side of the index
## do something with the above variables
echo "IP: $ip HOSTNAME: $hostname"
done
如果有帮助,我正在使用 linux
【问题讨论】:
-
试试这个
-->已经在帖子中回答了stackoverflow.com/questions/9479814/…
标签: arrays linux string bash split