1830935 Members
2240 Online
110017 Solutions
New Discussion

script

 
GK_5
Regular Advisor

script

Hi! I do not know what wrong with this simple script.

while read line
do
remsh $line model
done < hostfile

I want to read the hostname from hostfile and run the command for each host. It runs for first host and comes out of loop.
If I replace command "remsh $line model" with "echo $line" it displays all the hosts.

IT is great!
8 REPLIES 8
Paul Murray_4
Frequent Advisor

Re: script

Hi Ganesh,

The problems you receive are related to the way remsh works. Try the following:-

while read line
do
remsh $line -n "model"
done < hostfile


Rgds,
Paul.
Hey, nobody knows EVERYthing !!!
Paul Sperry
Honored Contributor

Re: script

for x in 'more hostfile'
do
remsh $x model
done
Caesar_3
Esteemed Contributor

Re: script

Hello!

Try this in csh:

foreach host (`cat hostfile`)
remsh $host model
end

Caesar
Massimo Bianchi
Honored Contributor

Re: script

Hi,
apart from others hint,
how is the file "hostfile" structure ?


Is it like

node1
node2
node2

or different ?

I think that your situation was

node1 node2 node3

this way the command read get only the first node, node1.

If you want to do the loop for all of them,
or you use a $( cat hostfile ) or change its structure.



the read command works in a slight different manner than you think.


read var1 var2 var3

reads a line of input, and put first field in var1, second in var2 and ALL other input in var3.


HTH,
Massimo
twang
Honored Contributor

Re: script

#!/bin/sh

HOSTLST=/tmp/hosts.lst

for host in $(cat $HOSTLST)
do
< run your commands here >
done
exit 0
RolandH
Honored Contributor

Re: script

In addition what Massimo said:
"... read var1 var2 var3

reads a line of input, and put first field in var1, second in var2 and ALL other input in var3....

that sounds that read can only process three variables. But this is not correct. Here what the manpage from read says to this:

read reads a single line from standard input. The line is split into fields as when processed by the shell (refer to shells in SEE ALSO); the first field is assigned to the first variable var, the second field to the second variable var, and so forth. If there are more fields than there are specified var operands, the remaining fields and their intervening separators are assigned to the last var. If there are more vars than fields, the remaining vars are set to empty strings.

Hey Massimo, I think you mean it like this :-}}


Roland
Sometimes you lose and sometimes the others win
Steve Steel
Honored Contributor

Re: script

Hi

You can have comments and blanks in /etc/hosts

grep -v "#" /etc/hosts|strings|while read ip name
do
remsh $name model
done

Or if your file contains only hosts then
cat hostfile|while read line
do
remsh $line model
done

Your solution reads all in 1 line with <

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Massimo Bianchi
Honored Contributor

Re: script

Hi,
Roland, you are perfectly right, i wanted to give an example, but forgot to mention it :)

Thanks for the remark.

Massimo