把awk中的数组赋值给shell中的数组

hanjinping 2011-05-26

方法1,通过eval

#!/bin/bash
ipstr="192.168.1.152,192.168.1.153";
#declare -a arr;
eval $(echo $ipstr | awk '{split($0, filearray, ",");for(i in filearray)print "arr["i"]="filearray[i]}')
#eval 把print的内容,和javascript的eval一样的功能
#echo ${arr[1]}
for i in ${arr[*]}
do
#scp_c="scp $i:/opt/flow_video/2011-05-26.txt"
#echo $scp_c
scp $i:/opt/flow_video/2011-05-26.txt /opt/flow_video/2011-05-26_$i.txt
done

方法2通过临时文件

#!/bin/sh
count=0
str="123#abc#@&?"
`awk 'BEGIN { split("'"$str"'",a,"#")}
     END   { for (i in a) {print a[i];$count++; }}'  /dev/null > 2`
     
while read buf
do
 tArray[$c]=$buf
 c=$(expr $c + 1)
done < 2
echo "array len:" $c
for((i=0;i<$c;i++));
do
  echo ${tArray[$i]}
done

执行输出

arraylen:3

123

abc

@&?

相关推荐