LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-08-2008, 07:21 AM   #1
peter.lovell
LQ Newbie
 
Registered: Oct 2008
Posts: 2

Rep: Reputation: 0
bash: ssh from within while loop doesn't work properly


Hi

Can anybody explain why the following problem occur and maybe how to get around it:


The following bash code

echo -e "Pedro\nPedroAgain" | while read S
do
echo S = $S;
ssh enter_ipaddress_here ls -l
done
echo FINISHED


outputs

S = Pedro
total 192
drwxrwxr-x 7 root root 4096 Oct 3 10:24 tmp
FINISHED


I was expecting the following output:

S = Pedro
total 192
drwxrwxr-x 7 root root 4096 Oct 3 10:24 tmp
S = PedroAgain
total 192
drwxrwxr-x 7 root root 4096 Oct 3 10:24 tmp
FINISHED



A corresponding test 'for'-loop works:

for S in Pedro PedroAgain
do
echo S = $S
ssh enter_ipaddress_here ls -l
done
echo FINISHED



The above codes snippets are just piece of test code to illustrate the problem I have in my real script. I wanna use a while loop and not a for loop since I only got the "while read" loop to work for reading full rows in a file without breaking up a row when there is a white spaces within the row.


I would be grateful for any help.

Cheers
Peter Lovell
 
Old 10-08-2008, 07:33 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,148

Rep: Reputation: 236Reputation: 236Reputation: 236
Hi

I had the same problem a while ago:

http://www.linuxquestions.org/questi...ght=while+loop
 
Old 10-08-2008, 07:44 AM   #3
peter.lovell
LQ Newbie
 
Registered: Oct 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks!!!

I get it, tried it and my script works as it is suppose to again.
Swift reply, thanks a lot.
Peter
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 8 04-05-2011 09:18 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
SSH Bash Loop m1ck Programming 7 01-31-2007 10:03 AM
Do/while loop and if statements not functioning properly w/ array... ohfaney Programming 5 05-02-2006 02:22 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM


All times are GMT -5. The time now is 06:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration