
05-27-2009
|
|
Shell programmer, author
|
|
|
Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,380
|
|
Quote:
Originally Posted by allrise123
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.
|