Running for loop on remote server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Running for loop on remote server
# 1  
Old 03-13-2010
Question 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

Code:
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
# 2  
Old 03-13-2010
One thing - your find syntax has problems - this should work on any POSIX version of find including linux:
Code:
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
# 3  
Old 03-13-2010
Not sure, if -o works with -exec.

Code:
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:

Code:
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

# 4  
Old 03-13-2010
Are you certain about this syntax it fails:
Code:
 find . -name '*.lis' -exec echo cp {} {}.lis \;
cp ./.ssh/t.lis {}.lis
cp ./t.lis {}.lis
cp ./test.lis {}.lis

on Solaris 9.
# 5  
Old 03-13-2010
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.

Last edited by binlib; 03-13-2010 at 12:49 PM..
# 6  
Old 03-13-2010
Quote:
Originally Posted by binlib
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.
Smilie 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.
# 7  
Old 03-14-2010
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..

Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check processes running on remote server

Hello Guys, I need some help to find out if processes are running on remote server or not. I could do 'ssh' to do that but due to some security reasons, I need to avoid the ssh & get result from remote server. Could you please suggest some that can be done without ssh or similar sort of... (8 Replies)
Discussion started by: UnknownGuy
8 Replies

2. Shell Programming and Scripting

Ssh - running commands on remote server

Hi, I am trying to run commands on a list of servers that I can ssh to and just want to know if there is a 'cleaner' way of doing this. At the moment, I am doing as below. Is there a way that I can escape the double quote differently? If a use a single quote to enclose the commands that I... (1 Reply)
Discussion started by: newbie_01
1 Replies

3. Shell Programming and Scripting

How to Append the output of a script running in remote server to a file in local server?

Hi guys, So i am in server1 and i have to login to server 2, 3,4 and run some script there(logging script) and output its result. What i am doing is running the script in server2 and outputting it to a file in server 2 and then Scp'ing the file to server1. Similarly i am doing this for other... (5 Replies)
Discussion started by: srkmish
5 Replies

4. Shell Programming and Scripting

Running script on remote server

Hi All, I need to run a ksh script on around 200 servers(consisting of AIX,LInux,HP-UX,Solaris). The script is there in the /tmp directory of all the servers. I want want to execute the script in background on the respective servers and then exit from there. I have written something like below:... (8 Replies)
Discussion started by: proactiveaditya
8 Replies

5. Solaris

Running command on Remote server

Hi, I have username/password for a remote server. I would like to have list of filenames and their size in a particular directory of remote server. Now Problem is - I can not use rsh command as I can not modify rhost file of remote server. Thanks in advance. Sanjay (1 Reply)
Discussion started by: sanjay1979
1 Replies

6. Shell Programming and Scripting

Running a Script in a Remote server

I am trying to write a script that would let me run a command in a remote server using ssh. scriptA: (dcm2nii is a command that only works on the other server) dcm2nii a b c scriptB: (I run this one on the current server) ssh -X otherserver /home/abc/Desktop/scriptA But when I do ... (2 Replies)
Discussion started by: ZeroGPX
2 Replies

7. Shell Programming and Scripting

Running an informix query on remote server

Hi, I have the following query: ------------------------------------------------------------------- set -xv ssh <remote server name> -l <login> export CLIENT_LOCALE=<some value> export INFORMIXDIR=<directory name> TERM=vt100 $INFORMIXDIR/bin/dbaccess sysmaster@dwem_test<<EOF>temp.txt... (2 Replies)
Discussion started by: sureshg_sampat
2 Replies

8. Shell Programming and Scripting

Running a remote Server through perl script

Hello people, I am want to run a server on remote machine through perl scripting using telnet api. Now when I try to do so, the server gets started perfectly, but as soon as I close the telnet connection in the script, the server started on the remote machine suddenly goes down. I also... (0 Replies)
Discussion started by: chandrak
0 Replies

9. Shell Programming and Scripting

running a script on remote server.

I need to run a script on a remote server from my ksh script. The issue I'm having is that I need to logon to the remote server as a different user. (see the following) logged on to server 1 as adsmgr neet to log on to server 2 as odemgr run passwd_util.ksh Thanks in advance. (1 Reply)
Discussion started by: whited05
1 Replies

10. UNIX for Dummies Questions & Answers

Running shell scripts on a remote server

Hi - I am running Mac OS X (Terminal) at my current workstation, and I would like to telnet to my local server to run a shell script. The local server is a Solaris machine (don't know the version or the type of Unix being run on it, sorry.) The problem I have is that when I create a #!/bin/sh... (2 Replies)
Discussion started by: pepintheshort
2 Replies
Login or Register to Ask a Question