1

I'm trying to execute the following command:

`sed 's/49%100%/49%!100%/' /etc/nagios/objects/services.cfg`

but I'm getting this error

-bash: ###############################################################################: command not found

If I try to run it without the back ticks, the command will work.

I've tried the following:

`sed 's/49%100%/49%!100%/' /etc/nagios/objects/services.cfg` 2>&1

`sed 's/49%100%/49%!100%/' /etc/nagios/objects/services.cfg`> /dev/null

but it didn't work.

learningbee
  • 333
  • 1
  • 5
  • 11
k189
  • 68
  • 2
  • 12
  • 3
    You are trying to run the output of `sed` as a command - this is expected. Why do you need the back ticks? – codeforester Feb 17 '17 at 00:50
  • @codeforester hi, i'm trying to execute this in a shell script, so i'm adding the back ticks. – k189 Feb 17 '17 at 00:52
  • 2
    You'd need the backticks only if you wanted to do something with the output, like storing it to a variable. – Benjamin W. Feb 17 '17 at 00:53
  • What do you want to do with the output of the command? Looks like you wanted to modify your Nagios config file. – codeforester Feb 17 '17 at 00:53
  • 1
    What you're doing with the back-quotes around the `sed` script is telling the shell to execute the output of the `sed` command as if it was a shell script. It isn't a shell script, so the shell gets unhappy rather quickly. – Jonathan Leffler Feb 17 '17 at 01:06
  • @codeforester i'm just trying to modify the content of the configuration. – k189 Feb 17 '17 at 01:06
  • 1
    Well, your code is trying to *run* the modified configuration as if it were made up of shell commands. – Mark Reed Feb 17 '17 at 01:16
  • (well, if we wanted to get really picky, it's trying to string-split and glob-expand the configuration and run that as *one* shell command; [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050) goes into why trying to depend on this kind of behavior to run arbitrary commands intentionally doesn't end well). – Charles Duffy Feb 17 '17 at 01:42

1 Answers1

6

You don't need back ticks or $() to invoke sed here since your requirement is not to interpret the output of sed as a command.

If your intention is to modify /etc/nagios/objects/services.cfg, you could do this:

sed 's/49%100%/49%!100%/' /etc/nagios/objects/services.cfg > /etc/nagios/objects/services.cfg.new

or, to make an in-place edit (when you are absolutely sure that your sed expression is right):

sed -i 's/49%100%/49%!100%/' /etc/nagios/objects/services.cfg

On BSD systems like macOS, sed -i needs an argument. The command would be:

sed -i '' 's/49%100%/49%!100%/' /etc/nagios/objects/services.cfg

See these posts for more info on back ticks and its more modern form, $():

Community
  • 1
  • 1
codeforester
  • 39,467
  • 16
  • 112
  • 140
  • 1
    `sed -i` is incompatibly different between GNU and BSD implementations -- in the BSD one (including on MacOS X), the argument with a backup filename suffix is mandatory rather than optional (to not have a backup file, one must pass an empty string as a separate argument following `-i`) -- so the code here isn't entirely portable even across systems offering the extension. – Charles Duffy Feb 17 '17 at 01:09
  • 3
    `-i ''`, with the space; `-i''` parses exactly the same as `-i` insofar as the command it's passed to is concerned. – Charles Duffy Feb 17 '17 at 01:11
  • 1
    It's not a bad idea to supply a non-empty argument to `-i` to save the backup, just in case. You can always delete it if the new file looks right. :) – Mark Reed Feb 17 '17 at 01:15
  • 1
    My preferred method is `sed 's/a/b/' "${file}" > "${file}2" && mv "${file}2" "${file}"` (Same issues as `-i`, and you must be sure that `${file}2` doesn't exist... Learned the habit on Solaris 10 which didn't have a `-i` option at all...) – Gert van den Berg Feb 17 '17 at 14:20