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