Issues with Reading Line by line from a file

I am trying to read a host name one at a time from a file which has a list of hostnames and do rsh and print . its not looping through the entire file. its breaking out after the first entry. If i comment out the rsh then it loops through file

#!/bin/ksh
filename="/tmp/hostnames";
while read -r host
do
    #i=0;
    #print $i
   NumEngines=`rsh $host "ps -ef |grep pattern|wc -l"`
    NumConnections=`rsh $host "netstat -n |grep <IP>|wc -l"`
    AvgConn=`expr $NumConnections / $NumEngines`;
    print $host     $NumEngines     $NumConnections  $AvgConn
done< $filename

Please advise what i am doing wrong
Thanks

Are your remote host configured to authorized connection from your machine ?
(is your hostname added properly in the .rhost file in the account of the remote host you are trying to connect to ?)
if you manually give a try of rsh to these hosts, does it work fine ?

yes manually rshing works and in this case it returns the output for the first host in the list.

What Operating System and version are you using?

host is a standard unix command so at first, you should choose another name for your variable

Try to run it without the -r option of the read command.
You could also make a try with the -n option of the rsh command
and use quote :

print "$host     $NumEngines     $NumConnections  $AvgConn"

x86_64 x86_64 x86_64 GNU/Linux

Replacing host with var did not help either

Hi, Can you try to run it by setting ksh -x option in the interpreter line and paste the output?

+ read -r var
+ printf %-10s, /tmp/filename
+ rsh <hostname> 'ps -ef |grep patternl|wc -l'
+ NumEngines=7
+ rsh <hostname>'netstat -n |grep <IP>|wc -l'
+ NumConnections=14
+ expr 14 / 7
+ AvgConn=2
+ printf %10s%10d%10d%10d, <hostname>7 14 2
+ 1>> /tmp/filename
+ read -r var
+ cat /tmp/filename
+ mailx -s 'Connection Status' email
+ rm /tmp/filename

There are two attempts to read the file. I see two read -r var in the above output. Can you check if the input file is in correct format. Or maybe some junk character is present.

Also check that the rsh works manually on the second remote host of your list

By the way ... according to the output, your script try to send a mail using mailx as well as you use printf instead of the print and i don't see those line of code in the script you have posted initially ...

So i suppose you didn't post the whole script you run or you did some modifications on it.

If you post only part of your script and that the error is somewhere else we could hardly guess where it could come from ...

So maybe you should post again what you exactly run

@ctsgnb - the rsh works on all the hosts -
@116@434 - in fact there is only one read attempt - i think its copy paste error
input file is clean - i can print and iterate through the file if I comment out the rsh commands.
if i do
while read -r var
do
print $var
done<$filename
it just works fine

---------- Post updated at 12:53 PM ---------- Previous update was at 12:27 PM ----------

rsh -n works thanks a lot everyone.

Ok that was one of the suggestion i made in post #5 :slight_smile: