#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    40
    Rep Power
    0

    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


    Code:
    echo "Enter Old domain name:"
    read old
    
    echo "Enter New domain name:"
    read new
    
    grep -rl '$old' /var/www/ | xargs sed -i 's/$old/$new/g'
    exec bash
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,446
    Rep Power
    1751
    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:
    Code:
    grep -rl "$old" /var/www/
    and check to see what the command returns. Then add in the xargs, where you may also have issues with apostrophes.
    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

    • sunny1234567890 agrees : Thanks.
    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

IMN logo majestic logo threadwatch logo seochat tools logo