0

I'm in trouble doing a sed command inside an ssh call. I need to use single quotes in my sed command. Here is my command line :

ssh root@192.168.0.32 "sed -i '/0 => '192.168.0.32' /a 1 => '$old',' /tmp/test"

Here is the file :

<?php
$CONFIG = array (
  'trusted_domains' =>
  array (
    0 => '192.168.0.32',
  ),
  'dbtype' => 'mysql',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'logtimezone' => 'UTC',
  'installed' => true,
  'redis' =>
  array (
    'host' => '192.168.0.13',
    'port' => 9095,
  ),
  'mail_smtpmode' => 'sendmail',
  'mail_from_address' => 'noreply',
  'knowledgebaseenabled' => false,
  0 => 18,
  'loglevel' => 1,
  'maintenance' => false,
);

In this context, I want to add in the trusted_domains for example 1 => '192.168.0.1', after the first element.

The problem of the command is that he doesn't recognize the single quotes inside the sed command.

Don't forget, the sed command is inside a ssh command

executable
  • 3,365
  • 6
  • 24
  • 52
  • Doesn't work too I tested it. It doesn't find the line but he can add the single quotes – executable Dec 06 '18 at 08:43
  • Possible duplicate of [How to escape single quote in sed?](https://stackoverflow.com/questions/24509214/how-to-escape-single-quote-in-sed) – oguz ismail Dec 06 '18 at 08:44

2 Answers2

3

Let's minimize this problem (and in the process of it realize, that most of your context could be stripped from your question.) First we define a helper function args to understand how the commands are split into words

$ args() { for x in "$@"; do echo "[$x]"; done; }

$ old=0.0.0.0
$ args ssh root@192.168.0.32 "sed -i '/0 => '192.168.0.32' /a 1 => '$old',' /tmp/test"
[ssh]
[root@192.168.0.32]
[sed -i '/0 => '192.168.0.32' /a 1 => '0.0.0.0',' /tmp/test]

Therefore a single argument ("sed -i ...") is passed to ssh. The double quotes are necessary for the variable expansion to take place but we can simplify the problem by inlining the old variable:

$ args sed -i '/0 => '192.168.0.32' /a 1 => '0.0.0.0',' /tmp/test
[sed]
[-i]
[/0 => 192.168.0.32 /a 1 => 0.0.0.0,]
[/tmp/test]

We can simplify this further

$ args '/0 => '192.168.0.32' /a 1 => '0.0.0.0','
[/0 => 192.168.0.32 /a 1 => 0.0.0.0,]

and replacing the complicated argument with a much simpler one

$ args 'foo'1'bar'
[foo1bar]

Notice that the three strings 'foo', 1 and bar are concatenated to the single world foo1bar. And now you can ask a simpler question:

How can I add a single quote to the string 'foo'1'bar'? And the answer is:

$ args 'foo'\'1\''bar'
[foo'1'bar]

Note though that "foo'1'bar" does not work in your case since you already used the double quotes for the outer ssh command.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
2

Could you try the following command (replace the username, hostname accordingly to your needs):

ssh toto@localhost "sed '/0 => \x27192.168.0.32\x27/a 1 => \x27$old\x27,' /tmp/test"

output:

<?php
$CONFIG = array (
  'trusted_domains' =>
  array (
    0 => '192.168.0.32',
1 => '1.1.1.1',
  ),
  'dbtype' => 'mysql',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'logtimezone' => 'UTC',
  'installed' => true,
  'redis' =>
  array (
    'host' => '192.168.0.13',
    'port' => 9095,
  ),
  'mail_smtpmode' => 'sendmail',
  'mail_from_address' => 'noreply',
  'knowledgebaseenabled' => false,
  0 => 18,
  'loglevel' => 1,
  'maintenance' => false,
);

If this works, just add -i options if you are really sure about what you are doing. you can also do the -i.bak to force sed to take a back up of your files

Allan
  • 12,117
  • 3
  • 27
  • 51
  • Then your file is not located in the proper folder in the remote server: try `ssh root@192.168.0.32 "ls /tmp/test"` it will give you the same error normally... than the `sed` command. When you have adapted the path try it again – Allan Dec 06 '18 at 08:55
  • 1
    @RavinderSingh13 Thank you buddy! I have been out for a while but I shall come back slowly to the community!!! Wow you have contributed so much to the community when I was out – Allan Dec 06 '18 at 09:17