Unable to read assign values to two variables in while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to read assign values to two variables in while loop
# 1  
Old 07-16-2013
Unable to read assign values to two variables in while loop

I am trying to read a input file which has two columns separated by space
Input file
Code:
server1 server2
server3 server4
server5 server6

When i execute the below while code it reads line by line and a and b variables are able to successfully fetch the values
Code:
while read a b
do
echo "$a"
echo "$b"
done < inputfile

But when i use a ssh statement . The while code just reads the first line and after that the script exits. Not able to understand this behaviour.
Code:
while read a b
do
ssh -q $a uname -a
done < inputfile

# 2  
Old 07-16-2013
-n option to ssh will avoid this.

Code:
while read a b 
do 
ssh -qn $a uname -a 
done < inputfile

This User Gave Thanks to rajamadhavan For This Post:
# 3  
Old 07-16-2013
To explain in more detail, the ssh application is just trying to read keyboard input from stdin as usual... But in this case, 'standard input' is 'inputfile', and it eats all your lines.
# 4  
Old 07-16-2013
Hi,
Can anyone explain me below commands plz...

Code:
for OUT_TAB in $(eval echo '$JOB_OUTPUT_TABLE_NM'); do


Code:
DELIMITER=`echo "$DELIMITER" | sed 's/E//g'`

Thanks...
# 5  
Old 07-17-2013
Thanks , It worked. But i am wondering , the for loop code for the same seems to be working fine.

Code:
for i in `cat inputfile`
do
ssh -q $i uname -a
done

But when i use the similar code in a while loop with one extra variable it seems to fail!
# 6  
Old 07-17-2013
The while-read loop ends in <filename, which redirects filename into standard input.

The for-loop, which does not have <filename at the end, does not.

The while-loop is much better, but you must bear in mind that standard input is redirected for everything inside it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assign Unknown Values to Variables

i have a program that spits out a certain number of values. i dont know the number of values. they can be 4, 10, 7, 20, no idea. but, i want to be able to assign each of the value returned by this program to a variable. in the latest instance, the program gave the following 6 values: 4... (8 Replies)
Discussion started by: SkySmart
8 Replies

2. Shell Programming and Scripting

A better way to assign values to variables - shell

so i've been used to doing it this way: SVAL=$(echo "7 3 2 38 3" | awk '{print $2}') 4VAL=$(echo "4:21:N:3" | awk -F":" '{print $4}') I know there's a way to do it by putting the value in an array and assigning it that way. but i'm not sure how to do it efficiently. any ideas? i dont... (9 Replies)
Discussion started by: SkySmart
9 Replies

3. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

4. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

5. Shell Programming and Scripting

Read variables names from array and assign the values

Hi, I have requirement to assign values to variables which are created dynamically. Below is the code which i am using to achieve above requirement. #!/bin/ksh oIFS="$IFS"; IFS=',' STR_FAIL_PARENT_IF_FAILS="WF_F_P_IF_FAILS1,WF_F_P_IF_FAILS2,WF_F_P_IF_FAILS3" set -A... (1 Reply)
Discussion started by: tmalik79
1 Replies

6. UNIX for Advanced & Expert Users

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the values... (12 Replies)
Discussion started by: manishab00
12 Replies

7. Fedora

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the... (1 Reply)
Discussion started by: manishab00
1 Replies

8. Shell Programming and Scripting

Assign values to variables of a file

Hi, I have a file like the following... CUST= DIR= NULIST= name=philps_123 How can i add values to each of these unassigned variables using a shell script? say for eg: i have values for CUST as onida, dir as /dir/onida, NULIST as /tmp/onida_files. How can i add these values to... (11 Replies)
Discussion started by: Tuxidow
11 Replies

9. Shell Programming and Scripting

how can i read text file and assign its values to variables using shell

Hello, I have a cat.dat file, i would like shell to read each 3 lines and set this 3 lines to 3 different variables. my cat.dat is: 11 12 +380486461001 12 13 +380486461002 13 14 +380486461003 i want shell to make a loop and assign 1st line to student_id, 2nd line to... (4 Replies)
Discussion started by: rosalinda
4 Replies

10. Shell Programming and Scripting

How do I assign values to variables made in a script?

How do I assign values to variables made in a script? e.g. for ((x=0;x<=5;i+=1)); do Xm$i=$var done (0 Replies)
Discussion started by: gelitini
0 Replies
Login or Register to Ask a Question