linux - Bash run rsh on multiple servers -
i have server list file this:
xxx servername1 xxx xxx servername2 xxx ...
now want go each server rename file local. bash scripts this:
#!/bin/bash while read line server=`echo $line | awk '{print $2}'` # server name rsh $server mv /a/b/c.txt /a/b/d.txt # rename on server echo "rename file in $server" # print echo done < server.txt
however, goes first 1 "severname1", rename file , print echo. never goes rest of servers , doesn't print echo. don't know why works first one.
does give me help?
don't let rsh
eat rest of input stream -- if does, there's nothing left read while read
next time loop tries start. easiest way redirect stdin /dev/null
:
rsh "$server" mv /a/b/c.txt /a/b/d.txt </dev/null
alternately, can loop on fd other 0 (note using awk
here silly; can tell bash pick out second field each line, in code below):
while read -u 3 _ server _; # server name (with read) rsh "$server" mv /a/b/c.txt /a/b/d.txt # rename on server echo "rename file in $server" # print echo done 3< server.txt
to explain of points might less obvious:
- the
3
file descriptor number; needs match between-u 3
,3< server.txt
. choose useread _ server _ <&3
instead of relying onread -u
, if want code compatible shells other bash. - the
_
s eat fields don't wantread
put content into.read _ server _
puts first field_
, second fieldserver
, , 3rd , later fields_
.
Comments
Post a Comment