Difference between WHILE and FOR loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference between WHILE and FOR loops
# 1  
Old 04-23-2015
Difference between WHILE and FOR loops

This might be a simple question, but I can't find the answer out there anywhere. I'm trying to write a loop to SSH to a server for me to then copy/paste some commands in. Once I'm done, I type 'exit' and the loop should then automatically SSH to the next server in line.

The 'dbs_group' file contains 5 hostnames, one per line. Nothing else.

Seems to work fine in a FOR loop, but not in a WHILE loop.

For example (server names replaced with 'xxx'):

Code:
[chris@x] for HOST in $(cat dbs_group); do ssh $HOST; done
Last login: Thu Apr 23 10:08:51 2015 from xxx.com
[chris@xxx1] exit
logout
Connection to xxx1 closed.
Last login: Sat Apr 18 13:56:28 2015 from xxx.com
[chris@xxx2] exit
logout
Connection to xxx2 closed.

etc., which is what I was expecting.

But using a WHILE loop:

Code:
[chris@x] cat dbs_group | while read HOST; do ssh $HOST; done
Pseudo-terminal will not be allocated because stdin is not a terminal.
-bash: line 1: xxx1: command not found
-bash: line 2: xxx2: command not found
-bash: line 3: xxx3: command not found
-bash: line 4: xxx4: command not found
-bash: line 5: xxx5: command not found
[chris@x]

I understand that the error above might be resolved by using 'ssh -t -t' but I'm just trying to figure out why the two different loops respond differently. I thought both of them would process the dbs_group file line per line and assign the variable to HOST.

Thanks,

Chris
# 2  
Old 04-23-2015
Hi,
The '|' use a subshell ( without pseudo-terminal. )
A "while" approach as your "for" loop:
Code:
while read HOST; do ssh $HOST; done <dbs_group

Regards.
# 3  
Old 04-23-2015
Hi disedorgue,

I still get the same when using that method:

Code:
[chris@x] while read HOST; do ssh $HOST; done <dbs_group
Pseudo-terminal will not be allocated because stdin is not a terminal.
-bash: line 1: xxx1: command not found
-bash: line 2: xxx2: command not found
-bash: line 3: xxx3: command not found
-bash: line 4: xxx4: command not found
-bash: line 5: xxx5: command not found
[chris@x]

Cheers,

Chris
# 4  
Old 04-23-2015
Try:
Code:
while read HOST <&3; do ssh $HOST; done 3<dbs_group

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-23-2015
Thanks Scrutinizer, that's done the trick.

So why the difference? Do the two loops process the file differently?

Cheers,

Chris
# 6  
Old 04-23-2015
You're welcome. Yes, the for loop does not redirect stdin, whereas the while read loop does. stdin is needed by the ssh command, so ssh was complaining that stdin could not be used..
The adaptation uses the unused file descriptor 3 rather than stdin (file descriptor 0)..

Last edited by Scrutinizer; 04-23-2015 at 09:30 AM..
These 3 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 04-23-2015
or alternatively:
Code:
while read HOST; do ssh $HOST </dev/null; done <dbs_group

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. UNIX for Dummies Questions & Answers

Need help with for loops

Why wont my for statements work? Im trying to get this script to swich to a user an if you put in a start/stop/or restart paramater to do just that for each user. I commented out the actual start/stop actions to test it just by using echos and not do anything hasty in the environment but it... (0 Replies)
Discussion started by: LilyClaro
0 Replies

3. Homework & Coursework Questions

If and Loops

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: In this script you will take a directory as input from the user and change the end of line sequence from a Unix... (1 Reply)
Discussion started by: Pcarson
1 Replies

4. Programming

what is the main difference between difference between using nonatomic lseek and O_APPEND

I think both write at the end of the file ...... but is there a sharp difference between those 2 instruction ..... thank you this is my 3rd question today forgive me :D (1 Reply)
Discussion started by: fwrlfo
1 Replies

5. Shell Programming and Scripting

Find with in loops and then difference

Hi All, Please find my problem below: I have a file at two different nodes dev and test 1st find> find /u/dev/local/abc -name ab.dat --Since this file can be in several sub directories 2nd find> find /u/test/local/abc -name ab.dat I find my 1st find result and do compare with 2nd... (5 Replies)
Discussion started by: varun_bharadwaj
5 Replies

6. Shell Programming and Scripting

Loops

Hi All, I am very new to Shell scripting. I read basic scripting manual. But i didn't understand the code. Please tell the meaning of the below code: while getopts "F:f:R:r:C:c:" opt 2>/dev/null do case ${opt} in F|f) FREQUENCY_MODE=$OPTARG;; ... (3 Replies)
Discussion started by: pdathu
3 Replies

7. Shell Programming and Scripting

loops

Hi All I have some directories on our server which are containing .csv files. i need to print value of cell "B2" from those csv files. Please advise. I have tried head command as example: head -2 */Book_Collection_Report_1_-_Collection_Requests_trials.csv | sed -n "3p" | awk -F","... (4 Replies)
Discussion started by: yash1978
4 Replies

8. UNIX for Dummies Questions & Answers

loops with tr

Hello, I'm not sure if this is more appropriate for the 'unix for dummies' or the 'unix for experts' forum because I'm new to this forum and this is the second topic I've discussed, but if you could let me know which one was more appropriate for something like this, please do! So in tr (an... (2 Replies)
Discussion started by: juliette salexa
2 Replies

9. UNIX for Dummies Questions & Answers

While Loops

I'm trying to create a loop that will prompt the user for 15 values, not forcing them to enter all 15. If the user enters through one or more of the prompts the null value needs to be converted to 0, otherwise set the parameter = to the value entered: ex. Please enter file no #1: 17920 ... (4 Replies)
Discussion started by: vdc
4 Replies

10. UNIX for Dummies Questions & Answers

While loops

Hi all, I am an amateur k shell scripter and came across something I need clarification on. while ; do various commands done What is the "" condition testing for in the above while loop? THx, Bookoo (5 Replies)
Discussion started by: bookoo
5 Replies
Login or Register to Ask a Question