How to escape characters over SSH command in a Makefile

0

I am trying to reload nginx in a Docker container over an SSH command... This is what I have in my Makefile:

reload:
ssh me@x.x.x "docker kill -s HUP `$$(docker ps | grep nginx | awk '{print $$1}')`"

But the command isn't working... I get this error:

"docker kill" requires at least 1 argument.
See 'docker kill --help'.

Usage:  docker kill [OPTIONS] CONTAINER [CONTAINER...]

Kill one or more running containers
make: *** [reload] Error 1

AAA

Posted 2018-11-25T08:27:39.993

Reputation: 119

Answers

0

In bash ",`, $(), $(()) are pre-interpreted and substituted.
If you want to escape these characters you can use \ backslash.
Try to debug this command using "set -x" (which you can switch off using "set +x"). It shows what is pre-interpreted.
I prefer the single apostrophe since it is not processed. Though you have to escape each single apostrophe in the string too. Try this:

ssh me@x.x.x 'docker kill -s HUP `$$(docker ps | grep nginx | awk \'{print $$1}\')`'

In this case the command between the '...' will be executed on the other side of the ssh. All parameters will be executed there. If you want to send a parameter from the current shell, you have to use a workaround for that. Eg:

'something_here'"$localvar"'continnue_command'
this way the "$localvar" will be substituted and concatenated.

m720

Posted 2018-11-25T08:27:39.993

Reputation: 19