Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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, 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 Aug 21 '10 at 4:09

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

Not programming related and not suitable for StackOverflow. – meagar Aug 20 '10 at 17:04
Should be moved to 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
@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

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

You must log in to answer this question.