I have the following
if [[ curl https://www.example.com | grep -q "Match 1 Found" = 0 ]]
then
echo "Match 1 Found"
elif [[ curl https://www.example.com | grep -q "Match 2 Found" = 0 ]]
then
echo "Match 2 Found"
else
echo "No Match Found"
PS : I am equating to zero since shell returns 0 if it is True
[[ ]]
is used to evaluate conditional expressions, but you don't need that when testing a command. The exit status of a command is tested directly by if
.
So just write:
if curl https://www.example.com | grep -q "Match 1 Found"
If you're doing the same curl
in both if
and elif
, you probably should save the output in a variable rather than going to the website twice.
results=$(curl https://www.example.com)
case "$result" in
*"Match 1 Found"*) echo "Match 1 Found" ;;
*"Match 2 Found"*) echo "Match 2 Found" ;;
*) echo "No Match Found" ;;
esac