你真的需要一个数组吗
庆典
$ipAddress="10.78.90.137;10.78.90.149"
$IFS=";"
$set -- $ipAddress
$echo $1
10.78.90.137
$echo $2
10.78.90.149
$unset IFS
$echo $@ #this is "array"
如果你想放入数组
$a=( $@ )
$echo ${a[0]}
10.78.90.137
$echo ${a[1]}
10.78.90.149
@OP,关于您的方法:将您的IFS设置为空格
$IFS=" "
$n=( $(grep -i ipaddress file | cut -d'=' -f2 | tr ';' ' ' | sed 's/"//g' ) )
$echo ${n[1]}
10.78.90.149
$echo ${n[0]}
10.78.90.137
$unset IFS
此外,没有必要使用这么多工具.你可以使用awk,或者只是使用bash shell
#!/bin/bash
declare -a arr
while IFS="=" read -r caption addresses
do
case "$caption" in
ipAddress*)
addresses=${addresses//[\"]/}
arr=( ${arr[@]} ${addresses//;/ } )
esac
done < "file"
echo ${arr[@]}
产量
$more file
foo
bar
ipAddress="10.78.91.138;10.78.90.150;10.77.1.101"
foo1
ipAddress="10.78.90.137;10.78.90.149"
bar1
$./shell.sh
10.78.91.138 10.78.90.150 10.77.1.101 10.78.90.137 10.78.90.149
呆子
$n=( $(gawk -F"=" '/ipAddress/{gsub(/\"/,"",$2);gsub(/;/," ",$2) ;printf $2" "}' file) )
$echo ${n[@]}
10.78.91.138 10.78.90.150 10.77.1.101 10.78.90.137 10.78.90.149