Deben Oldert
Deben Oldert

Reputation: 114

Checking if String is in a command answer

I'm struggling with a problem in linux bash.
I want a script to execute a command

curl -s --head http://myurl/ | head -n 1

and if the result of the command contains 200 it executes another command. Else it is echoing something. What i have now:

CURLCHECK=curl -s --head http://myurl | head -n 1
            if [[ $($CURLCHECK) =~ "200" ]]
            then
            echo "good"
            else
            echo "bad"
            fi

The script prints:

HTTP/1.1 200 OK
bad

I tried many ways but none of them seems to work. Can someone help me?

Upvotes: 0

Views: 82

Answers (4)

Hackaholic
Hackaholic

Reputation: 19753

using wget

 if wget -O /dev/null your_url 2>&1 | grep -F HTTP >/dev/null 2>&1 ;then echo good;else echo bad; fi

Upvotes: 0

anubhava
anubhava

Reputation: 785481

You can use this curl command with -w "%{http_code}" to just get http status code:

[[ $(curl -s -w "%{http_code}" -A "Chrome" -L "http://myurl/" -o /dev/null) == 200 ]] &&
      echo "good" || echo "bad"

Upvotes: 0

user3458
user3458

Reputation:

I would do something like this:

if curl -s --head http://myurl | head -n 1 | grep "200" >/dev/null 2>&1; then
    echo good
else
    echo bad
fi

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247012

You need to actually capture the output from the curl command:

CURLCHECK=$(curl -s --head http://myurl | head -n 1)

I'm surprised you're not getting a "-s: command not found" error

Upvotes: 0

Related Questions