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 use read _ server _ <&3 instead of relying on read -u, if want code compatible shells other bash.
  • the _s eat fields don't want read put content into. read _ server _ puts first field _, second field server, , 3rd , later fields _.

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -