The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 05-27-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380
Quote:
Originally Posted by allrise123 View Post
hi guys!!!

i am writing a script in which i take an input from user and find it in a file and replace it.
My input file looks like

Code:
hi
what your name?
allrise

my code looks is

Code:
echo "Enter the name"
read name
FILE="/opt/name.txt"
NEW_FILE="/opt/new_name.txt"
exec 0<$FILE
    while read line
    do
     if [ -n "`echo ${line} | grep 'allrise'`" ]
     then
              echo ${line} | sed 's|allrise|hello $name|g' >>$NEW_FILE

Variables are not expanded inside single quotes; use double quotes:


Code:
echo ${line} | sed "s|allrise|hello $name|g" >>$NEW_FILE

Quote:

Code:
     else
             echo $line >>$NEW_FILE
     fi
     done <$FILE

When i run my script, it ask me for name suppose i gave "john"... but the output comes as shown below

Code:
hi
what your name?
hello $name

so, can anyone explain how i can get proper output which should be.

Code:
hi
what your name?
hello john

Who don't you do:


Code:
sed "s/allrise/hello $name/" "$FILE" > "$NEWFILE"

There's no need for a loop.