Find the answer to your Linux question:
Results 1 to 3 of 3
Code: #!/bin/sh echo Enter directory to replace example [/etc/hosts/]: read dir echo Enter original text: read orginal echo Enter replaced text: read replace if [ "$orginal" = "$replace" ]; then ...
  1. #1
    Just Joined! bsdlover's Avatar
    Join Date
    May 2010
    Posts
    5

    Replace no work

    Code:
    #!/bin/sh
    echo Enter directory to replace example [/etc/hosts/]:
    read dir
    echo Enter original text:
    read orginal
    echo Enter replaced text:
    read replace
    
    if [ "$orginal" = "$replace" ]; then
    clear
    echo Error
    else
      find ${dir} -type f -exec sed -i 's/${orginal}/${replace}/g' {} \;
    fi
    Where is the problem ?

  2. #2
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    Replace the single quotes with double quotes:

    "s/${orginal}/${replace}/g"

  3. #3
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,096
    Two points:
    1) You do realise, that this script is dangerous?
    If ${dir} would be empty or "/" for whatever reason, then you have a good chance of corrupting files.

    2) It doesnt add value.
    2a) If it is intended for a non-technical user, then it lacks input validation and sanitation.
    A lot input validation/sanitation

    2b) For a technical user it is useless, as it adds no logic. (S)he would be better off calling find/sed directly
    You must always face the curtain with a bow.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...