user8342844
user8342844

Reputation:

SH - Replace text

I want to replace one string with another but I can't. The code is:

updatedb
MCRYPTINI=$(locate mcrypt.ini | grep 'apache2')
MCRYPTSO=$(locate mcrypt.so | grep "/mcrypt.so")

OLD="extension=mcrypt.so"
NEW="extension=$MCRYPTSO"

echo $MCRYPTINI
echo $MCRYPTSO
echo $OLD
echo $NEW
echo "'s/$OLD/$NEW' $MCRYPTINI"

sed -i 's/$OLD/$NEW' $MCRYPTINI

And the result is:

sudo sh testScript.sh
/etc/php5/apache2/conf.d/20-mcrypt.ini
/usr/lib/php5/20121212/mcrypt.so
extension=mcrypt.so
extension=/usr/lib/php5/20121212/mcrypt.so
's/extension=mcrypt.so/extension=/usr/lib/php5/20121212/mcrypt.so' /etc/php5/apache2/conf.d/20-mcrypt.ini
sed: -e expression #1, char 11: unterminated `s' command

For the response I don't need to use 'sed', but it's looks easy and good. I use sh not bash because I want the code can use in all the systems, so I prefer answers that follow that principle

UPDATE

sed -i "s/$OLD/$NEW/" $MCRYPTINI

error:

sed: -e expression #1, char 14: unknown option to `s'

Upvotes: 2

Views: 138

Answers (2)

user8342844
user8342844

Reputation:

The solution could be:

sed -i "s/$OLD/$NEW/" $MCRYPTINI

but $NEW is a path, so I need to change "/" by other character, for example "+"

sed -i "s+$OLD+$NEW+" $MCRYPTINI

Upvotes: 0

Guru
Guru

Reputation: 17004

Add a slash and double quotes:

sed -i  "s/$OLD/$NEW/" file

Upvotes: 1

Related Questions