This is the part of my code:
sample_1=''
sample_1_is_cancelled=''
sample_2=''
sample_2_is_cancelled=''
sample_3=''
sample_3_is_cancelled=''
sample_4=''
sample_4_is_cancelled=''
sample_5=''
sample_5_is_cancelled=''
while read -r insert
do
eval sample_$i=$(echo $insert| awk -F'|' '{print $1}')
eval sample_$i_is_cancelled=$(echo $insert| awk -F'|' '{print $2}')
i=$(( i + 1 ))
done < $logpath/source.txt
mysql -uroot -p -e" insert into ttable(sample_1, sample_1_is_cancelled, sample_2, sample_2_is_cancelled, sample_3, sample_3_is_cancelled, sample_4, sample_4_is_cancelled, sample_5, sample_5_is_cancelled)
values($sample_1, $sample_1_is_cancelled, $sample_2 $sample_2_is_cancelled, $sample_3, $sample_3_is_cancelled, $sample_4, $sample_4_is_cancelled, $sample_5, $sample_5_is_cancelled);"
There is a maximum of 5 sets of values can be possible. Minimum is one set.
I can echo the variables like below,
eval echo \$sample_$i
eval echo \$sample_${i}_is_cancelled
But I am not able to pass it iside the insert query in the same way. Any suggesstions... Please help.
eval? Why aren't you using an array instead of thatsample_$iindirection? Even given that shell is a terrible language to do data processing in, you're making it even harder than necessary by doing that.printfto construct an SQL statement (printf formatting can be used kind-of-like placeholders, without the automatic quoting) in a variable, say$sql, and then execute that with the mysql CLI -mysql ... -e "$sql"