I have to write a unix shell script using if statement finds a string in a file
ID: 666630 • Letter: I
Question
I have to write a unix shell script using if statement finds a string in a file and replaces it and create a .bak file. but if it doesnt find the string them it doesnt replace it and it doesnt create a .bak file. i wrote it and it works fine, but it keeps frezing on me. someone please help!
here is what i have have written so far:
#!/bin/sh
echo "before if"
if grep $1 "$3"; then
echo "entered if"
mv "$3" "$3.bak"
echo "after mv"
sed s/$1/$2/g "$3.bak" > "$3"
echo "after sed"
else
echo "not found"
fi
echo "end"
Explanation / Answer
#!/bin/sh
echo "before if"
if grep $1 "$3"; then
echo "entered if"
mv "$3" "$3.bak"
echo "after mv"
sed s/$1/$2/g "$3.bak" > "$3"
echo "after sed"
else
echo "not found"
fi
echo "end"