0

I have a problem replacing a command inside of a script, the offending line in the script looks like this:

mail -s "$(hostname) on $(date)"

It should be replaced with a line like this:

nail -r "[email protected]" -s "Subject" -S smtp=255.255.255.255

But I can't get sed to do a replacement :) I wrote a small script for that purpose:

#!/bin/bash

old="mail -s \"\$(hostname) on \$(date)"
new="nail -r \"[email protected]\" -s \"Subject\" -S smtp=255.255.255.255"

sed -i 's|$old|$new|' script.sh

Does anyone have any advice?

3 Answers 3

3
sed -i "s|$old|$new|" script.sh

Note the double quotes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for pointing out this stupid mistake :) I've been using sed to replace and append double quotes where needed and got tired of escaping them all the time so i just started to put in single quotes and forgot about it :)
1

Sed by default does not do in Place editing. If you are using gnu Sed try providing the in place flag -ikbak

1 Comment

The OP has the -i option in place. The only difference is that yours causes a backup to be created with the (unusual?) suffix "kbak".
0

Better use ed!

http://bash-hackers.org/wiki/doku.php?id=howto:edit-ed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.