shell concat string in for loop,split string to array,no space when initing variable

how to concat

query=" 1!=1"
query_inner=" or (a='1' and b='2')"
query="${query}${query_inner}"  #method1
query+="$query_inner"			#method2

concat string in for loop

method1:OK

a=""
for i in {1..10}; do
    a="${a}${i} "
done

# Remove the trailing space
a="${a% }"

echo "$a"	#1 2 3 4 5 6 7 8 9 10

method2:OK

query="1!=1"
while read text; do
        if [ "$text" != "" ]; then
        arrIN=(${text//,/ })
        query_inner=" or (a='${arrIN[0]}' and b='${arrIN[1]}' and c='${arrIN[2]}')"
        query+="$query_inner"
		#echo "$query"
   fi
done < $filepath/$csvfile
#1!=1 or (a='1' and b='613K' and c='10') or (a='1' and b='126H' and c='10')
echo "$query"  

method3:NG
it seems if use “cat filepath/filepath/filepath/csvfile |while read text; do”,the variable will not be used at outside of it

query="1!=1"
cat $filepath/$csvfile |
while read text; do
    if [ "$text" != "" ]; then
        arrIN=(${text//,/ })
        query_inner=" or (tvrdkbn='${arrIN[0]}' and btshacd='${arrIN[1]}' and mltchnnlshbts='${arrIN[2]}')"
        query+="$query_inner"
		#echo "inner$query"	#ok 1!=1 or (a='1' and b='613K' and c='10') or (a='1' and b='126H' and c='10')
    fi
		#echo "outer$query"	#ok 1!=1 or (a='1' and b='613K' and c='10') or (a='1' and b='126H' and c='10')
done]#ng  "1!=1"
echo "$query"

It is important to notice there should be no space beside “=” when giving value to a variable,like a= 1 or b =“good”,otherwise it will raise strange error,and it offen happens.

a= 1        #ng
b ="good"	#ng
a=1        #ok
b="good"	#ok
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值