Reputation: 710
I have to look if a file has the work England
. I have used grep
This is what I have so far
#!/bin/bash
cd ~/Desktop/htmlFiles/
echo "Starting"
if `grep -inE "england" maps.html` ; then
echo "yaaaay"
fi
But the output is:
Starting
./trial.sh: line 4: 22:: command not found
Why doesn't it return yaaaay
?
Upvotes: 1
Views: 8870
Reputation: 1580
Backtick substitution will return the output of the command. In this case, you are outputting the line number with grep
's output, following by the matching line. Hence, there appears to be something on line 22, but the shell tries to execute the command 22:
, which then fails.
The variant you have posted as a followup answer will work, as grep returns 0 on a successful match, but 1 if the match fails.
Upvotes: 1
Reputation: 710
#!/bin/bash
cd ~/Desktop/htmlFiles/
echo "Starting"
if grep -qinE "england" maps.html ; then
echo "yaaaay"
fi
Gave me the required output. Is that a good practise?
Upvotes: 3