Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have the following command:

ssh $USER@$HOST "ls /ops/pkg/ec/`grep "PKRTS" /ops/pkg/ec | awk '{print $1}'`* > /tmp/tmp_file"

What the above SHOULD do..is, ssh to the host then execute the command. The command should first execute the grep and awk between the back ticks, and generate a file name, then ls using that filename* and write that data to a temp file.

However, it executes the grep and awk before even SSH'ing and the command that goes to the server is simply an "ls /ops/pkg/ec/* > /tmp/tmp_file" since the grep and awk return nothing on the local machine.

Any ideas how to get around this?

share|improve this question

migrated from stackoverflow.com Aug 21 '10 at 4:09

This question came from our site for professional and enthusiast programmers.

1  
Not programming related and not suitable for StackOverflow. –  meagar Aug 20 '10 at 17:04
    
Should be moved to SuperUser.com or a Linux / Unix administration oriented forum. This is not programming related. Please do not answer here. –  mctylr Aug 20 '10 at 17:10
    
unless I misunderstand the system, if the question gets moved to superuser then the answers move too. –  glenn jackman Aug 20 '10 at 18:13
1  
@Nick, if /ops/pkg/ec is a directory on the remote system, why are you grepping it? –  glenn jackman Aug 20 '10 at 18:16

1 Answer 1

It's a quoting problem. Backticks get interpreted inside double quotes, so you have to enclose your command in single quotes. You then have to do some more work related to the awk script quoting:

ssh $USER@$HOST 'ls /ops/pkg/ec/$( awk "/PKRTS/ {print \$1}" /ops/pkg/ec )* > /tmp/tmp_file'

You may have to experiment with the number of backslashes to protect the dollar sign in the awk command.

Note my comment to you above: is /ops/pkg/ec a file or directory? If it's a directory then grep "PKRTS" /ops/pkg/ec is probably not what you want.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.