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 04-28-2004, 02:40 AM   #1
meat-head
LQ Newbie
 
Registered: May 2002
Location: Tampa
Distribution: Slackware 9.1
Posts: 21

Rep: Reputation: 15
while loop ending prematurely in a bash script


I have a problem with the script below:

Code:
#!/bin/bash

while read line
do
  echo $line
  ssh $line "df -k | awk '{ print \$6 }'">>output.txt
  awk -F'|' '/FSParam=/ { print $1,";",$2,";",$3,";",$4,";",$5,$6 }'>>output.txt
  $line|cat>>output.txt
done<$1
Basically, I want to run this script against a list of servers. It checks what mounts are on the server and then awks a file with the same name as the server. If I comment the ssh line, the script runs fine and parses all the files giving me what I want from the awk line. When I uncomment the ssh line the script only returns the information for the first server in the list as if it's killing the loop. If I copy the ssh line a few times and replace "$line" with server names it works for all of them but then seemingly kills the loop. Is there some reason, I can't use ssh in a loop?

This is running on Solaris. I appreciate any help.
 
Old 04-30-2004, 04:26 AM   #2
meat-head
LQ Newbie
 
Registered: May 2002
Location: Tampa
Distribution: Slackware 9.1
Posts: 21

Original Poster
Rep: Reputation: 15
Bump, to give it another shot.
 
Old 05-01-2004, 07:44 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
A few things that sound a little strange to me:

1) you are feeding the loop with the first argument on the script's commandline ("done <$1"). So -I guess- this $1 must be a filename. What's in that file?

2) You are reading the file line-by-line. Because of this line: "$line | cat >>output.txt" I conclude there are shell-executable command inside that file. But on the other hand the lines seem to contain "<user>@servername", because you are using them for ssh-ing. These are conflicting meanings for the contents of the file you giva your script as the first argument.

3) This is probably where your problem is:
The awk-line doesn't mention any input source, so it's reading from stdin, which comes from the same file as the loop is reading from! This is wrong, because when your script starts, it reads he first line from the file into the "line" variable with "while read line". Then is executes the awk command, which will read the rest of the file in just one go, leaving nothing to read for the second run of the loop, so the loop will end.
 
Old 05-03-2004, 10:54 PM   #4
kanilbabu
LQ Newbie
 
Registered: May 2004
Posts: 2

Rep: Reputation: 0
Unhappy

Hi ,

i also have the same problem just doing this

while read list
do

whoishe=`ssh ${list} whoami `

echo ${whoishe} :${list} >> otherfile

done < hostnames

its a simple command. whenever i enter the wrong password it is going to the next box in the list but when the correct password is entered it executes properly and just ends the script. doesn't not continue with the list. it is breaking the loop.

But when tried scp it goes through the list.

please advice

thanks
 
Old 05-04-2004, 04:02 PM   #5
kanilbabu
LQ Newbie
 
Registered: May 2004
Posts: 2

Rep: Reputation: 0
hi
changed the way the loop works and works fine
file list in `cat hostname`
do
whoishe=`ssh ${list} whoami `
echo ${whoishe}:${list} >> otherfile
done < hostnames
i don't know is it the way the While loop and the done < hostname works
hope this helps others
 
Old 05-07-2004, 05:56 AM   #6
meat-head
LQ Newbie
 
Registered: May 2002
Location: Tampa
Distribution: Slackware 9.1
Posts: 21

Original Poster
Rep: Reputation: 15
Hko,

The line "$line|cat>>output.txt" shouldn't have been in there when I posted the code. I am using a file for the first parameter that contains a list of server names. This code does not work either. Well it works but doesn't loop.

Code:
while read line
do
   ssh $line "df -k | awk '{ print \$6 }'">>output.txt
done<$1
 
Old 05-07-2004, 08:13 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Yes.
Even though a command is given on ssh's command line, ssh is still reading stdin (which is your server-name-file). So after ssh executed for the first time the file containing the server names will be read until the end-of-file by ssh, leaving nothing to read for the read-line-loop.
To prevent this from happening use the "-n" option for ssh.

Try:
Code:
#!/bin/bash

rm output.txt 2>/dev/null
while read line
do
   echo "Host ${line}:" >>output.txt
   ssh -n $line "df -k | awk '{if (NR!=1) print \$6 }'" >>output.txt
   echo >>output.txt
done <$1
 
Old 05-08-2004, 01:46 AM   #8
meat-head
LQ Newbie
 
Registered: May 2002
Location: Tampa
Distribution: Slackware 9.1
Posts: 21

Original Poster
Rep: Reputation: 15
I completely misread what you posted before. Thank you very much for help. I really appreciate it.
 
  


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
bash script loop bong.mau Programming 6 09-14-2005 07:38 PM
Bash Script and Loop error handling Kedelfor Programming 5 05-22-2005 02:22 PM
bash script for loop drisay Programming 5 12-25-2004 12:32 AM
bash script - for loop question rignes Programming 3 10-05-2004 11:16 PM
never ending loop on make jods Linux - Software 1 04-02-2004 03:02 PM


All times are GMT -5. The time now is 06:31 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