LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   while loop ending prematurely in a bash script (https://www.linuxquestions.org/questions/programming-9/while-loop-ending-prematurely-in-a-bash-script-175408/)

meat-head 04-28-2004 02:40 AM

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.

meat-head 04-30-2004 04:26 AM

Bump, to give it another shot.

Hko 05-01-2004 07:44 AM

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.

kanilbabu 05-03-2004 10:54 PM

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

kanilbabu 05-04-2004 04:02 PM

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

meat-head 05-07-2004 05:56 AM

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


Hko 05-07-2004 08:13 AM

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


meat-head 05-08-2004 01:46 AM

I completely misread what you posted before. Thank you very much for help. I really appreciate it.


All times are GMT -5. The time now is 05:15 PM.