Reading multiple variables in a loop


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reading multiple variables in a loop
# 1  
Old 08-27-2015
Reading multiple variables in a loop

Hi,

I managed to read and print variable as shown in the below code.

Code:
table_name=table1,table2,table3

i=0
IFS=","
for i in $table_name
do

        echo $i

done

Is there a way how I can read more than one variable. For example I need to read 2 variables and populate the output as shown below


table_name=table1,table2,table3
col_name=col1,col2,col3

Output:
table1 col1
table2 col2
table3 col3


Thanks
# 2  
Old 08-27-2015
Quote:
Originally Posted by shash
Is there a way how I can read more than one variable. For example I need to read 2 variables and populate the output as shown below
Yes, there is. But in fact there is a better (and MUCH better readable) way to accomplish this if you use arrays - a feature almost every modern shell offers:

Code:
#! /bin/ksh

table[1]="table1" ; col[1]="col1"
table[2]="table2" ; col[2]="col2"
table[3]="table3" ; col[3]="col3"
table[4]="table4" ; col[4]="col4"

counter=1

while [ $counter -le ${#table[@]} ] ; do
     echo "TABLE: ${table[$counter]}     COL: ${col[$counter]}"
     (( counter += 1 ))
done

${#arrayname[@]} is an automatically maintained integer giving the number of elements in the array arrayname.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 08-27-2015
Thanks bakunin. The issue is the variable will be in the following format:

Code:
table_name=table1,table2,table3
col_name=col1,col2,col3

In this case how to use the array please?
# 4  
Old 08-27-2015
You might try sth. along this line (tested in bash):
Code:
OI="$IFS"
IFS=,
table_arr=($table_name)
col_arr=($col_name)
IFS="$OI"

This User Gave Thanks to RudiC For This Post:
# 5  
Old 08-27-2015
Many thanks Rudi & bakunin. Got it working as follows:
Code:
table_name=table1,table2,table3
col_name=col1,col2,col3

OI="$IFS"
IFS=,
table_arr=($table_name)
col_arr=($col_name)
IFS="$OI"

counter=0

while [ $counter -lt ${#table_arr[@]} ] ; do
     echo "TABLE: ${table_arr[$counter]}     COL: ${col_arr[$counter]}"
     (( counter += 1 ))
done

# 6  
Old 08-27-2015
Quote:
Originally Posted by shash
Thanks bakunin. The issue is the variable will be in the following format:
Why? If it is a variable you can define it in any way you want, no?

Or is it some external input you need to use? In this case: how did it get into this variable? Perhaps there is a better way to put it in more suitably formed variables. You might want to present the whole problem, not just an arbitrary part of it.

Quote:
Originally Posted by shash
Code:
table_name=table1,table2,table3
col_name=col1,col2,col3

In this case how to use the array please?
The following is not pretty and less readable than my original idea, but there you go:

Code:
#! /bin/ksh

table_name="table1,table2,table3"
col_name=col1,col2,col3
table=""
col=""

while [ "$table_name" != "" ] ; do
     table="${table_name%%,*}"
     col="${col_name%%,*}"

     echo "TABLE: $table    COL: $col"

     table_name="${table_name##${table}}"
     col_name="${col_name##${col}}"
     table_name="${table_name##,}"
     col_name="${col_name##,}"
done

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 7  
Old 08-27-2015
I 'm reading it from a global variable which I can't change. I need to read those variables as I had mentioned. Many thanks for your help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Displaying multiple variables in for loop

Hi! I've run into a problem where my variables are displayed in the wrong order. Basically I'm supposed to use a file that has information like this username:firstname:lastname:etc:etc. What I'm interested in doing is reformating it into a something more like this: username lastname,... (2 Replies)
Discussion started by: reindeermountai
2 Replies

2. Shell Programming and Scripting

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... (10 Replies)
Discussion started by: rock1
10 Replies

3. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

4. Shell Programming and Scripting

How to use for/while loop with multiple variables?

Hi, I have two variables like below which will always be of the same size a=1:2:3 b=A:B:C I need to use a for/while loop that will have both the variables available. I cannot use an array here and will probably might iterate through the variable as echo $a | tr ':' '\n' and thus iterate... (5 Replies)
Discussion started by: Elizabeth H
5 Replies

5. Shell Programming and Scripting

While loop reading file with multiple conditions

Hi Am trying to print the PIDs of process in a file and trying to grep any PID from that file I set the if condition as $value != "PID" and $value != "-" Assign that number to a variable Am confused since am using while loop to read the line from file and again if condition to check those... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

6. Shell Programming and Scripting

Reading multiple values in while loop

I'm having trouble with a simple piece of code. IFS=, echo "1,2,3,4,5,6,7,8" | while read x y do echo "x=$x" echo "y=$y" done I'm hoping for x=1 y=2 x=3 y=4 . . . but I'm getting x=1 (3 Replies)
Discussion started by: sabbata
3 Replies

7. UNIX for Dummies Questions & Answers

multiple variables in for loop

hi, I want an equivalent for loop for this C code in unix shell script... for(int i,int j;i<5;i++,j++) { } Please reply soon Regards Navjot (1 Reply)
Discussion started by: navjotsingh
1 Replies

8. Shell Programming and Scripting

While loop with Multiple variables

Hi , I am trying to write a script in kshell with while loop ,its like count=1 count_cmp=1 while ; do tail -$count tempfile | head -1 > tempstring ....... done However i get CIF.sh: line 33: ' I have checked thetrailing spaces , not sure what is... (4 Replies)
Discussion started by: amit1_x
4 Replies

9. Shell Programming and Scripting

Reading Multiple Variables From a Single Line in Shell

I'm a Linux newb, I've been running a Debian Linux server for about a year now, and I've written some simple scripts to automate various things, but I still don't know much, and I forget what I learn as fast as I figure it out... Anyway, that really isn't important, I just want you to know that... (14 Replies)
Discussion started by: Drek
14 Replies

10. Shell Programming and Scripting

for loop with multiple variables ?

I have a script which selects two 'sets' of system LVM device files from a tabular file 'mapfile' using awk : LIVELV=`awk '{print($1)}' mapfile` BCVLV=`awk '{print($3)}' mapfile` I wanted to pass these 'sets' into an LVM command 'loop' along the lines of : lvmerge $BCVLV $LIVELV ie.... (3 Replies)
Discussion started by: fosterian
3 Replies
Login or Register to Ask a Question