LinuxQuestions.org
Review your favorite Linux distribution.
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 03-26-2010, 07:39 AM   #1
asemaany
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Rep: Reputation: 0
"while read line" doesn't work with ssh in the loop body


I am trying to read the names of hosts from a file and do a ssh to see if processes are all running or not.
Here is the outer loop that is causing problem:

#/bin/bash
cat hosts.lst|while read line
do
ssh $line ps ux
done

hosts.lst is a text file that has the host names in each line.
The problem is that the loop just breaks after the first run. Somehow while read line becomes invalid when the body of the loop does the ssh and returns. Any idea why it is happening and how to fix it?

Many thanks for your time and help.
 
Old 03-26-2010, 08:20 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
I'm not sure if this is your problem, but a | pipe makes the processes following it run as a subshell. You can avoid this, and the need for the external cat command, by using a simple file redirection.

Code:
while read line; do
     ssh "$line" ps ux
done <hosts.1st
Also, please use [code][/code] tags around your code, to preserve formatting and to improve readability.

Last edited by David the H.; 03-26-2010 at 08:22 AM. Reason: typo
 
Old 03-26-2010, 08:47 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,513

Rep: Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895
Hopefully the previous post will fix issue, but I noticed you said:

Quote:
see if processes are all running or not
Yet you neither capture the output of what the command displays nor do you test the exit
status of ssh to see if it were successful.

Just an observation.
 
Old 03-26-2010, 09:19 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,136

Rep: Reputation: 230Reputation: 230Reputation: 230
Hi

I had the same problem before. The problem is that when you pipe into the while loop, the while loop reads from stdin. The ssh command takes over stdin so it messes up the pipe. Add the -n option to the ssh command and it will work.
 
1 members found this post helpful.
Old 03-26-2010, 03:19 PM   #5
asemaany
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks Guttorm
That should solve the problem.
 
Old 03-28-2010, 01:14 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,513

Rep: Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895
Please mark problem as SOLVED

Sorry ... was looking at a cached copy .. Please ignore
 
  


Reply

Tags
bash, file, from, read, ssh, while


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
LXer: On-line applications "just work"; why worry about the freedom of the licence? LXer Syndicated Linux News 0 10-02-2008 09:30 AM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
"make" does not work with command line arguements %$hfydt%$ Linux - General 7 04-01-2008 10:59 AM
Any way to get "Alice"; "Call of Duty" series and "Descent 3" to work? JBailey742 Linux - Games 13 06-23-2006 01:34 PM
latex: "Unable to read an entire line" when trying graphics hedpe Linux - Software 1 04-23-2006 10:44 PM


All times are GMT -5. The time now is 09:26 AM.

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