LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 12-13-2013, 06:53 AM   #1
keif
Member
 
Registered: Apr 2013
Posts: 107

Rep: Reputation: Disabled
while loop only reading the first line of hostnames in file


Hello all,

I'm trying to ssh to a list of hosts one by one and run a grep command on each host. But my while loop is only reading the first line and quitting.

Code:
#!/bin/bash

while read hostName
do
ssh vision@$STORE "grep LANE_INFO vision.cnf"
done </tmp/test-hosts.txt
I read online that this might be do to ssh reading from the same file as the while loop, but I'm not sure how to get around that. Also I don't know for sure that is the issue here.

Any help is appreciated.

Thanks.
 
Old 12-13-2013, 07:11 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,901

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
you read values into variable hostName but uses STORE in your ssh command.
 
1 members found this post helpful.
Old 12-13-2013, 07:16 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
I'm assuming that the hostName vs STORE is a typo.

Have a look at these 2 solutions:
Code:
#!/bin/bash

# using a while loop
while read hostName
do
  echo $hostName
  ssh -n vision@$STORE 'grep LANE_INFO vision.cnf'
done < /tmp/test-hosts.txt

# or, using a for loop
for hostName in $( cat /tmp/test-hosts.txt )
do
  echo $hostName
  ssh vision@$STORE 'grep LANE_INFO vision.cnf'
done
 
1 members found this post helpful.
Old 12-13-2013, 07:28 AM   #4
keif
Member
 
Registered: Apr 2013
Posts: 107

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
I'm assuming that the hostName vs STORE is a typo.

Have a look at these 2 solutions:
Code:
#!/bin/bash

# using a while loop
while read hostName
do
  echo $hostName
  ssh -n vision@$STORE 'grep LANE_INFO vision.cnf'
done < /tmp/test-hosts.txt

# or, using a for loop
for hostName in $( cat /tmp/test-hosts.txt )
do
  echo $hostName
  ssh vision@$STORE 'grep LANE_INFO vision.cnf'
done
druuna,

Yea, STORE was a typo (I tried to set hostName=STORE in an attempt to divert ssh from reading the txt file, but it didn't work).

Thanks once again for your assistance. The while loop works great. I'll have to read up on ssh -n
 
  


Reply



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
[SOLVED] Reading a multiple file inside loop and store! rameshpaul_cog Linux - Newbie 13 06-20-2012 04:25 AM
[SOLVED] Function goes into infinite loop when reading from a file vikas027 Programming 9 10-08-2011 12:54 AM
How can I read a file line by line and add it to a loop in another file? astroumut Linux - Newbie 7 08-24-2009 04:37 AM
Unexpected result in while do done loop reading file steven.c.banks Linux - General 2 05-01-2008 12:32 PM
File reading line by line and compare Goni Linux - Software 14 09-21-2005 12:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:38 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration