Running for loop on remote server

Hi,

I'm having a problem performing for loop on remote server, i know this can be done with one liner but i'm not sure how it works if using logical operator such as for ifs and case or while

for server in sterverA serverB serverC ; do
   ssh -v $server "cd ~/MyDocuments/; bag=`find an*.txt bt*.txt se*.wav`; for file in $bag; do cp $file ${file%}.backup; done"
 done

Somehow it doesn't work - it cannot find $bag
Please help

Thanks in advance

One thing - your find syntax has problems - this should work on any POSIX version of find including linux:

bag=`find . \( -name 'an*.txt' -o -name 'bt*.txt' -o -name 'se*.wav' \)`

You could also lose your for loop with xargs, to make it a shorter command

Not sure, if -o works with -exec.

ssh -v $server find ~/MyDocuments/ -name 'an*.txt' -o -name 'bt*.txt' -o -name 'se*.wav' -exec echo cp {} {}.backup \;

If not, you can use below commands:

for server in sterverA serverB serverC ; do
  ssh -v $server find ~/MyDocuments/ -name 'an*.txt' -exec echo cp {} {}.backup \;
  ssh -v $server find ~/MyDocuments/ -name 'bt*.txt' -exec echo cp {} {}.backup \;
  ssh -v $server find ~/MyDocuments/ -name 'se*.wav' -exec echo cp {} {}.backup \;
done

Are you certain about this syntax it fails:

 find . -name '*.lis' -exec echo cp {} {}.lis \;
cp ./.ssh/t.lis {}.lis
cp ./t.lis {}.lis
cp ./test.lis {}.lis

on Solaris 9.

bag is a variable of the shell running on the remote server, so you need to either escape it or put the whole string in single quotes. In your case, single quotes work better because you have more parts that you wanted to escape.

:b: werd,

Id ditch the bag variable all together and just put that command on the for loop... which eliminates a variable. you will probably need to escape the dollar sign in front of files as well.

hey all,

thanks again for all your replies but it seems that it doesn't work on remote server. I've tried it locally and it works just fine..


for server in serverA serverB serverC ; do
   ssh -v $server "cd ~/MyDocument; find . \( name 'A*.wav' -o  -name 'B*.txt' -o -name 'ab*.wav' \) -exec cp {} {}.back \;"
 done

Thanks for all your help