0

I try to pass sed via ssh command as [1] to ssh command, but it does not work. I do ssh to a host, like 'ssh ld3646'. In my console, I input

ssh ld3646 'sed -i 's/USE_KERNEL_NFSD_NUMBER="4"/USE_KERNEL_NFSD_NUMBER="64"/g' /etc/sysconfig/nfs'

file /etc/sysconfig/nfs is not changed at all. It works when I do 'ssh ld3646' and type

sed -i 's/USE_KERNEL_NFSD_NUMBER="4"/USE_KERNEL_NFSD_NUMBER="64"/g' /etc/sysconfig/nfs'

Can someone help on why doesn't it work?

  • Check this out: http://gt-dev.blogspot.com/2015/05/bash-ssh-how-to-invoke-command-remotely.html – g-t Apr 05 '16 at 12:03

2 Answers2

0

Notice how you have two sets of single quotes? That doesn't work. Single quotes don't nest. Luckily, the outer set of quotes isn't necessary.

ssh ld3646 sed -i 's/USE_KERNEL_NFSD_NUMBER="4"/USE_KERNEL_NFSD_NUMBER="64"/g' /etc/sysconfig/nfs
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

You're experiencing quote confusion.

The quote immediately before the sed script EXITS the quoted string that begins with sed.

I provided some strategies for handling nested quotes in this answer, which might be enlightening. In your case, an easy strategy might be simply to change quotes:

ssh ld3646 "sed -i 's/USE_KERNEL_NFSD_NUMBER=\"4\"/USE_KERNEL_NFSD_NUMBER=\"64\"/g' /etc/sysconfig/nfs"

Note that the embedded double quotes are escaped, to avoid precisely this problem.

Another option for visual simplification might be to use variables:

sedscript='s/USE_KERNEL_NFSD_NUMBER="4"/USE_KERNEL_NFSD_NUMBER="64"/g'
ssh ld3646 "sed -i '$sedscript'"

This last one works because single quotes that are embedded in double-quoted strings do not block variable expansion.

Community
  • 1
  • 1
ghoti
  • 45,319
  • 8
  • 65
  • 104