ssh and executing a for loop

Hi all,

I am trying to run a script which is expected to do:

on the remote machine,
There are two directories /export/home/abc1,/export/home/abc2
i am trying to do,
ssh SERVERNAME "for i in `ls -l /export/home/abc*|awk '{print $9}'`; do cd $i; ls -l; done"

But its not working ,iam getting /export/home/abc*:No such file or directory.
When i run the for loop in the remote system its working fine.

The system is Solaris 8 and running as root.

Please let me know if something wrong

Thanks.

The whole script is enormously overcomplicated.

ssh SERVERNAME 'ls -l /export/home/abc[12]'

The error message indicates that this is not as such the problem, though. Are you sure the path is correct?

Hi ,

Thanks for the reply.

The paths are correct, i dont want to use abc[12] as they might vary from servers, thats why i use generic abc*,
Please advise.

Thanks.

Feel free to change that, does it work with these changes?

Hi,

It worked.

Is it possible to do other operations like tar etc inside these directories?

Thanks,

Anything you can run at the prompt, you can run with ssh, but properly quoting the arguments you want to pass to the remote server can be a challenge if it's a complex script.

can you tell me what's wrong with my command. Do i need to put any escape characters?

Thanks.

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.

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.

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.

Again, the backticks are being evaluated on your local system, and you should not be using them anyhow.

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).

You're amazing. It worked.

Thanks for your help.