I would like to know whats the best way to accomplish this:

I have a file with below data:

chl(abc.123) chltype(RCVR)
chl(xyz.123) chltype(RCVR)
chl(abc.000) chltype(RCVR)
chl(abc.999) chltype(RCVR)
chl(xyz.456) chltype(RCVR)

Now I want to run a script which does this.,

ssh to server 1 and run the following commands for those listed under server 1 , then ssh to server 2 and run the commands for those listed under server2

for example, on server 1

echo chl(abc.123) chltype(RCVR)
  • I have removed the blank lines in between, let me know if the blank lines should be there.. – heemayl Sep 18 '15 at 2:38

You could do something like:

while IFS= read -r line; do
    if [[ ${line} =~ ^server\d+$ ]] ; then
    ssh ${server} "echo ${line}"

Assuming the format of your file doesn't change.

  • I suspect serverN in his example are just placeholders, the server names don't actually begin with server. – Barmar Sep 18 '15 at 20:57
  • @Barmar - I figured that but I was assuming the server names matched a pattern of some sort. I'll change it to word count perhaps – DarkHeart Sep 18 '15 at 23:46
  • It looks more like the server lines have one token, the data lines have more than one. – Barmar Sep 18 '15 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.