escaping awk with remote ssh command and bash that is already escaped

1

1

Hello.

ssh ufk@10.0.0.2  "bash -lc 'pm2 list | grep app | awk { print $3} '"

I need to run the command I pasted here. the problem is that i'm already escaping twice... the bash with " and the pm2 with '. how can i escape awk ?

i know that it will work if i escape it with awk '{ print $3 }' but the all command is already escaped twice... so .. what do i do ?

update

i created the following bash command:

PM2_APP_ID=`$REMOTE_SRV_SSH_COMMAND "bash -lc \"pm2 list | grep app | grep -v 'pm2 show' | awk '{ print \\\$4 }'\""`;

here i get the all pm2 list relevant line, it's like awk is never been executed.

ufk

Posted 2015-08-04T14:54:41.963

Reputation: 1 055

1awk can do most cases of grep and you don't really need those done remotely, so ssh u@h "bash -lc 'pm2 list' " | awk '/app/{print $3}' will also work – dave_thompson_085 – 2015-08-04T19:48:38.310

thanks @dave_thompson_085, you helped me resolve the issue – ufk – 2015-08-23T09:13:41.483

Answers

1

You can't backslash-escape single quotes inside single-quoted strings, but you can backslash-escape double quotes inside a double-quoted string. So you could do this:

ssh ufk@10.0.0.2  "bash -lc \"pm2 list | grep app | awk '{ print \\\$3 }'\""

You need to escape the $ in the awk program twice to avoid the $3 from being expanded, first by local shell and then by the remote one.

rici

Posted 2015-08-04T14:54:41.963

Reputation: 3 493

trying to test it.. i still get the all line from 'grep app', it's like awk is not being called at all – ufk – 2015-08-05T06:41:33.007

@ufk: Sorry. Edited the answer to include necessary backslashes – rici – 2015-08-05T07:06:08.603

updated main post. still not working. thanks for help so far – ufk – 2015-08-23T09:10:23.260

1

thanks @dave_thompson_085 for your comment, that helped me resolve the issue.

i'm using the following command:

PM2_APP_ID=`$REMOTE_SRV_SSH_COMMAND "bash -lc 'pm2 list'" | grep app | grep -v 'pm2 show' | awk '{ print $4 }'`;

as you can see here i use pm2 list on remove server, and the rest i'm doing locally. no hassle with escaping things and it works properly.

ufk

Posted 2015-08-04T14:54:41.963

Reputation: 1 055