Reputation: 305
I have created a shell script to find a string within a text file, then display the results to the screen sorted through grep
.
How could I surround this in an if
statement so that if results are found then echo "Found!" and display the results afterwards else say "not found"?
I have gotten this far already by using various websites but I am stuck with this.
Here is what I have so far:
echo "enter name "
read search
echo "The String searched for "
grep -i $search $fileName
if [$search ????]
then
echo "Found!"
else
echo "Not Found!"
fi
Upvotes: 3
Views: 591
Reputation: 20456
Check if grep
returns a non-zero length string:
if [ -n "$(grep -i "$search" "$fileName")" ]; then
echo "Found!"
else
echo "Not Found!"
fi
Upvotes: 0
Reputation: 123458
Your grep
call can be a part of the if
statement:
if grep -q "$search" "$filename"; then
echo "Found!"
else
echo "Not Found!"
fi
Upvotes: 3