gpt4 book ai didi

arrays - 如何根据 bash 中的定界符拆分字符串?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:38:27 26 4
gpt4 key购买 nike

<分区>

我在 .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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com