January 7th, 2014, 01:31 AM
Shell Script(simple problem)!!!
I want to find and replace string from files present in one directory.
user will input the string to be searched and to replace .
Here is my program but Not working
echo "Enter Old domain name:"
echo "Enter New domain name:"
grep -rl '$old' /var/www/ | xargs sed -i 's/$old/$new/g'
January 7th, 2014, 03:14 AM
What do you see if you run the grep -rl '$old' /var/www/ in isolation? I'm guessing you'll see a list of ... nothing. The apostrophes is preventing the shell expanding the variable, replace them with quotes:
and check to see what the command returns. Then add in the xargs, where you may also have issues with apostrophes.
grep -rl "$old" /var/www/
Next up is where the results of the edit go. Currently they are thrown away into stdout. Look at the (I think it is) -i option to have the edit happen 'in place'.
And finally, why the exec bash at the end?
Comments on this post
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc