0

So, I have this if/else condition which checks for a certain string in curl output. If the output has either "yahoo" or "google", then I want it to hit the if block, otherwise hit the else block. I used -q to search for yahoo. But I am not sure how to add two conditions [that is one string as "yahoo" and the other string as "google" in the same curl query]

if curl https://yahoo.com | grep -q "via yahoo"
    echo "via yahoo or hotmail"
else
    echo "not via yahoo or gmail"
fi
3
  • 1
    See the accepted answer to Match output of a CURL in an IF-ELSE condition in BASH.
    – pjh
    Commented Oct 25, 2024 at 0:04
  • 1
    Regarding 'one string as "yahoo" and the other string as "google" in the same curl query' especially - it's not clear if you're asking how to curl 2 domains at once or grep for either of 2 strings in the output of a curl command or identify which of multiple curl domain commands produced output or something else. Please edit your question to clarify what it is you're trying to do.
    – Ed Morton
    Commented Oct 25, 2024 at 13:33
  • 1
    When I run curl https://yahoo.com I just get the output redirect and when I run curl https://google.com I get output including The document has moved, neither of which outputs via ... so I'm thinking you don't really want to parse the curl output in the way you show. Please provide sample input and expected output that would clarify your needs and we can copy/paste test with.
    – Ed Morton
    Commented Oct 25, 2024 at 13:39

1 Answer 1

2

Extending OP's current code to match on multiple strings:

if curl https://yahoo.com | grep -Eq 'via (google|yahoo)'
then
    echo "via google or yahoo"
else
    echo "via neither"
fi

NOTE: grep -Eq 'via google|via yahoo' would also work

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.