Loop with multiple delimited variables

hi,

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)

thanks in advance for some help

Please use code tags as required by forum rules!

What does that mean? It obviously contradicts what your sample looks like...?

try something like:

var1=1,23,234
var2=a,ab,xyz
var3=0,0,0

IFS="," arr1=($var1)
IFS="," arr2=($var2)
IFS="," arr3=($var3)

seq 0 2 | while read i
do
   echo " insert into table (${arr1[$i]}, '${arr2[$i]}', ${arr3[$i]});"
done
1 Like

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

seq --h

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 ----------

thanks I did something like this:

var1=1,23,234,1
var2=a,ab,xyz,z
var3=0,0,0,1

len=`echo $var1 | awk -F '[,]' '{print  NF}'`
echo "$len"

--4

${#A[@]} is array A's element count, ${!A[@]} lists all the indices into array A.

For the quoting of string variables, try

QUS() { FMT="%s"; [[ $1 =~ [^[:digit:]] ]] && FMT="'%s'"; printf $FMT $1; }
for i in ${!A[@]}; do echo "insert into table ($(QUS ${A[$i]}), $(QUS ${B[$i]}), $(QUS ${C[$i]}));" ; done
insert into table (1, 'a', 0);
insert into table (23, 'ab', 0);
insert into table (234, 'xyz', '1a2b');

IFS cannot be set local to an array assignment. So you would need to set it globally and use something like:

oldIFS=$IFS
IFS=,
arr1=($var1) arr2=($var2) arr3=($var3)
IFS=$oldIFS

Why use an (outdated) external program ( seq ) and a pipe when we can use shell syntax

for i in 0 1 2
do

In bash, zsh and ksh93 you can use:

for i in {0..2}
do

or

for (( i=0; i<=2; i++))
do

Not important, but perhaps interesting is that there is no need to use a $ sign for a variable reference used as an index to an array:

echo "insert into table (${arr1}, '${arr2}', ${arr3});"

thank you all for your valuable inputs. i can incorporate this now:

code

#!/bin/sh

var1=1,23,234,1
var2=a,ab,xyz,z
var3=0,0,0,1
var4=0,0,0,1


len=`echo $var1 | awk -F '[,]' '{print  NF-1}'`
echo "$len"

oldIFS=$IFS
IFS=,
arr1=($var1) arr2=($var2) arr3=($var3)  arr4=($var4)
IFS=$oldIFS

for (( i=0; i<=3; i++))
do
  echo " insert into table (${arr1[$i]},${arr2[$i]},${arr3[$i]},${arr4[$i]});"
done

You are welcome. To add, instead of

len=`echo $var1 | awk -F '[,]' '{print  NF-1}'`

one could also use plain shell, without external programs:

len=${#arr1[@]}

after arr1 is set...

--
=> Please use code tags next time for code and data <=