比如比如我有一个变量 “123 456 789”,要求以空格为分隔符把这个变量分隔,
并把分隔后的字段分别赋值给变量,即a=123;b=456;c=789
下面几个方法可以参考:
定义一个数组,然后把分隔出来的字段赋值给数组中的每一个元素
通过eval+赋值的方式
通过多次awk把每个字段赋值
具体直接看测试脚本:
#!/bin/bash
str="123,456,789"
arr=${str//,/ }
for each in ${arr[*]}
do
echo $each
done
echo "++++++++++++++++++++++++++++++"
a="123 456 789"
declare -a arr
index=0
for i in $(echo $a | awk '{print $1,$2}')
do
arr[$index]=$i
let "index+=1"
done
echo ${arr[0]}
echo ${arr[1]}
echo "++++++++++++++++++++++++++++++"
b=""
c=""
eval $(echo $a | awk '{ printf("b=%s;c=%s",$1,$2)}')
echo $b
echo "++++++++++++++++++++++++++++++"
eval $(echo "aa:bb" | awk -F ":" '{print "i1="$1";i2="$2}')
echo "++++++++++++++++++++++++++++++"
apitypes=apis,auth,nginx,apapche
apitypeArray=$apitypes
echo $apitypes
echo $apitypeArray
echo "+++++++++++++++++++++++"
for i in ${apitypeArray//,/ }
do
# call your procedure/other scripts here below
echo "$i"
done
echo "++++++++++++++++++++++++++++++"
for f in ${apitypes//,/ }
do
# call your procedure/other scripts here below
echo "$f"
done
echo "++++++++++++++++++++++++++++++"
eval $(echo $apitypes|awk -F"," '{print "i1="$1";i2="$2}')
echo $i1
echo $i2
本文暂时没有评论,来添加一个吧(●'◡'●)