i need a portion in a audit logging shell script where i have to loop thru multiple variables.
I need some help in accomplishing this. i have 3 variables
var1=1,23,234
var2=a,ab,xyz
var3=0,0,0
the variables will have variables number of values but same length.(3 in this case )
i need to be able to loop thru values in var1 and get the values of all the variables to execute some commands.
while IFS=',' read var1 var2 var2
do
echo "$var1 1 st value $var2 first value $var3 first value"
so for 1 st loop for var1 1 i need to use 1 ,a and 0 in a insert script to some tables. so they need to avaliable in 3 different variables so i can do
insert into table values ($var1, $var2, $var3)
in this case i will get
insert into table (1,'a',0)
insert into table (23,'ab',0)
insert into table 234,'xyz',0)
Howsoever, given the variables as presented in post#1, and bash available, how about
IFS=, read -a A <<<$var1
IFS=, read -a B <<<$var2
IFS=, read -a C <<<$var3
for i in ${!A[@]}; do echo "insert into table (${A[$i]}, ${B[$i]}, ${C[$i]});" ; done
insert into table (1, a, 0);
insert into table (23, ab, 0);
insert into table (234, xyz, 0);
Quotation of the string values left as an exercise for the reader.
sorry i meant the number of variable value in each variable will always be the same. if var1 has 3 comma separated values so will var2 and var 3.
---------- Post updated at 12:47 PM ---------- Previous update was at 12:39 PM ----------
thanks rdrtx1 and RudiC.. rdrtx1 can you please explain how seq 0 2 works . i would like to understand this ,,is it based on the number of values in the csv variable? thanks
So the number of values in the variables may not be the same always. So should I can calculate the length first and use that in the code. This example has 3 for each variable.. but it could be 2 or 4. thanks
---------- Post updated at 01:16 PM ---------- Previous update was at 12:54 PM ----------