0

I've been attempting to write a Bash script that automates everything needed to add a new piece of equipment to our MRTG graphs. Part of that requires me to edit a cfg file which I've read can be done with the sed command. The lines pasted below are where the error occurs when running the script giving me a "unexpected EOF while looking for matching `"' " error. town, tower, equipment, and direction are declared above. Any help in narrowing down what the problem might be would be a huge help!

newpattern="WorkDir: /var/www/html/mrtg/$town/$tower/$equipment$direction"
pattern="WorkDir: "
sudo sed -e "s/$pattern/$newpattern/" ~/MRTGconfigs/mrtg-BeatriceBSWT2960.cfg
devdacool
  • 13
  • 5
  • 2
    The unexpected EOF error is due to a mismatched quote earlier in the script. Unfortunately, the shell cannot reliably tell where the actual problem is, so the place it reports the error is where the error became apparent, not necessarily anywhere near where it actually is. Try feeding the entire script into [shellcheck](http://www.shellcheck.net) and see if it can spot the real problem. Note: Jonathan Leffler's answer is also right, but he's pointing out a separate error. – Gordon Davisson Apr 01 '16 at 06:10
  • Thanks for the hint. I didn't know shellcheck existed which is great to know. I've just started dabbling in shell scripting and this will make the learning curve much less painful! – devdacool Apr 04 '16 at 00:59

1 Answers1

2

You need to use something other than slashes in the s/// command because there are slashes galore in the replacement text:

newpattern="WorkDir: /var/www/html/mrtg/$town/$tower/$equipment$direction"
pattern="WorkDir: "
sudo sed -e "s%$pattern%$newpattern%" ~/MRTGconfigs/mrtg-BeatriceBSWT2960.cfg

I used % symbols instead; you can use any other character that appears neither in $pattern nor $newpattern. If need so be, you can use a control character such as Control-A; that works fine too.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thanks for the response Jonathan. The error in question was fixed by Gordon (missing quote) but this also fixed another problem I would've had come up. – devdacool Apr 04 '16 at 01:01
  • @devdacool: I agree with that assessment — I looked for a problem in the code shown, not in the code not shown, but Gordon was correct about the problem you described (rather than the one you showed). Remember [How to debug a shell script?](https://stackoverflow.com/questions/951336/how-to-debug-a-bash-script/951352#951352). You'd see things going awry due to the missing, or extraneous, quote. – Jonathan Leffler Apr 04 '16 at 01:36