awk 赋值给 shell 变量的方法
- result='week(now(),-1) 49';
- var=`echo $result|awk '{print substr($result,16,3)}'`;
- echo $var;
- SYSFILE_NAME=`echo "$CONTROL_FILE" | awk -F"." '{print $1}' | awk -F"/" '{print $NF}'`;
方法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
方法三:我用的就是这种:直接将结果存入数组
redis_list=$($redis_comm -p 9001 cluster nodes | grep master | awk '{print $2}' | awk -F['@'] '{print $1}')