ssh and executing a for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ssh and executing a for loop
# 8  
Old 09-05-2008
Without escaping, the backticks would be evaluated locally; that was the reason you got the error message about the directory. I thought you said you had it working now, though.
# 9  
Old 09-05-2008
Sorry for the confusion.

I tried executing just ls -l on the remote server. Actually the abc will wary from server to server, i have a list of servers which iam reading line by line and ssh to each of them and executing this

ssh X.X.X.X "for i in `ls -l /export/home/abc[12]|awk '{Print $9}'`|do cd $i; tar -cvf new.tar file1 file2; done "
bash: -c: line 1: syntax error near unexpected token `|d'
bash: -c: line 1: `for i in |do cd ; ls -l; done '

Here iam putting abc[12] but ideally i want abc*
The content of file1 and file2 will vary between abc1 and abc2, thats why iam putting these directories in the loop.

Please let me know if this can be done.



Thanks again.
# 10  
Old 09-05-2008
Quote:
can you tell me what's wrong with my command. Do i need to put any escape characters?
double quotes may be causing the commands to be interpreted by shell.
you may try ur code by placing single quotes inplace of double quotes.
# 11  
Old 09-05-2008
Again, the backticks are being evaluated on your local system, and you should not be using them anyhow.

Code:
ssh X.X.X.X 'for i in /export/home/abc*; do cd $i; tar -cvf new.tar file1 file2; done'

The single quotes prevent $i from being replaced with the value of $i locally (presumably would result in an empty string, which isn't very useful).
# 12  
Old 09-05-2008
You're amazing. It worked.

Thanks for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remote script via SSH not executing

I have worked on multiple scenarios to execute remote script via ssh. This problem I am not able to resolve. 2 linux hosts. Server1, Server2 on Server1 I have script called ~/scripts/start_standalone.sh XXXX cd $JBOSS_HOME NODENAME=xyz; IP_ADDR=`hostname`; MGMT_IPADDR=`hostname`;... (3 Replies)
Discussion started by: oraclermanpt
3 Replies

2. Shell Programming and Scripting

Query on executing awk using SSH

Hi All, Im trying to find the count of process running on remote server using SSH. Below command dosen't work. ssh -q user@host "ps -ef | grep "pattern" | grep -v 'grep' | awk '{print $2}'|wc -l" But below command works. ssh -q user@host "ps -ef | grep "pattern" | grep -v... (1 Reply)
Discussion started by: Girish19
1 Replies

3. Shell Programming and Scripting

Help with executing awk and While loop

Hi All, I have a file say, sample.txt Source Name: xxx Department|Revenue 1001|3252 1002|3345 I am using the above file in one of my script. I need to read from Line 3 of the above the file and do some process. My script has a code: awk 'NR > 2' sample.txt | while read Dep; do... (9 Replies)
Discussion started by: machomaddy
9 Replies

4. Shell Programming and Scripting

Executing remote commands via ssh

Hi, I'm tryin to write a script that will collect information about a remote servers, put them into variables and print them to screen. # /usr/bin/bash ls $1 > /dev/null 2>/dev/null if then echo "$1 is file" for server in $(cat $1) do # echo $server ... (5 Replies)
Discussion started by: moshesa
5 Replies

5. UNIX for Dummies Questions & Answers

executing a command using ssh

Hi All, I am trying to execute a command using ssh as below. ssh user123@servername "which ctmcontb" It is gving the error as below no ctmcontb in /usr/bin /usr/sbin /opt/sysadm/bin Not sure from where the PATH is getting picked up. But When I login direclty to the server I am... (5 Replies)
Discussion started by: anilreddy103
5 Replies

6. Shell Programming and Scripting

executing commands over ssh

Hi I am trying to send a command over ssh with a parameter but the shell fails to expand the command properly any ideas what am i doing wrong with this. This is ssh on AIX for i in 71 72 73 74 75 do for server in server1 server2 do somestr="Some String" echo "$server... (3 Replies)
Discussion started by: xiamin
3 Replies

7. Shell Programming and Scripting

Problem with executing script using ssh

I'm writing a script which is meant to ssh into a remote machine, sed a file there, and write the output to a new file on the remote machine. Obviously the easy way to do this is to write and execute the script on the remote machine, but I'm trying to do this all via ssh since I like to keep and... (4 Replies)
Discussion started by: maethlin
4 Replies

8. Shell Programming and Scripting

Script not executing second loop

I have a server that receives backup files from several servers. Each server has its own directory to scp their files into, some of the files are received as .tar files and need to be compressed before being dumped. When the scp of the tar file is complete a file named 'flag' is also sent to... (2 Replies)
Discussion started by: thumper
2 Replies

9. Shell Programming and Scripting

Error while executing glance using ssh

Hi, I'm running glance ussing ssh from a "HP-UX" machine for a Sun machine. Here is a sample code:- x:- HP-UX y:- Sun Extracting a value from glance outuput. ssh x@y "/opt/perf/bin/glance -f -adviser_only -iterations 2 -maxpages 1 > glance.op ; echo `head -1 glance.op | a wk '{print... (0 Replies)
Discussion started by: bishweshwar
0 Replies

10. UNIX for Advanced & Expert Users

executing .profile with ssh

Hi, How do I get all my profile settings when connecting with ssh? (5 Replies)
Discussion started by: rein
5 Replies
Login or Register to Ask a Question