You need to check the exit status of grep
. And there are a couple of ways to do so.
You either:
call grep
and then examine the special variable $status
, like in
#!/bin/csh
set currentWord=$1
set newWord=$2
set fileName=$3
grep -q $currentWord $fileName
if !($status) then
sed -i.bak -e "s/$currentWord/$newWord/g" $fileName
else
echo "The string is not found."
endif
or, since the actual vale of $status
is not needed here, just use the terser form
#!/bin/csh
set currentWord=$1
set newWord=$2
set fileName=$3
if { grep -q $currentWord $fileName } then
sed -i.bak -e "s/$currentWord/$newWord/g" $fileName
else
echo "The string is not found."
endif
The second one is my favourite.