shell 循环读取文件及字符串转为数组

时间:2023-03-10 00:19:27
shell 循环读取文件及字符串转为数组

文件/etc/hdocker_config内容如下:

30.72.63.94 30.72.63.95 30.72.63.96 30.72.63.97
/tmp/lasclocker.tar
master slave1 slave2 slave3

实现功能:依次读取该文件的每一行,并赋值于数组tmp,并把数组的第0个元素,第2个元素分别转化为新数组slaves_hostIP和slaves_hostsName。

#!/bin/bash
i=
while read line
do
tmp[$i]=$line
let i+=
done < /etc/hdocker_config tarPath=${tmp[]}
slaves_hostIP=(${tmp[]})
slaves_hostsName=(${tmp[]}) echo $tarPath
echo ${slaves_hostIP[@]}
echo ${slaves_hostsName[@]}