C
C
CloudZero2020-02-11 15:29:26
linux
CloudZero, 2020-02-11 15:29:26

How to redirect curl to a variable and compare with a string?

Is there a script something like

status=$(curl -k -Is https://example.com" | head -1)

if [ "$status" = "HTTP/2 200" ]
then
  echo -e "Server status OK"
elif [ "$status" = "HTTP/2 401" ]
then
  echo -e "Server have problem with auth"
else
  echo -e "Server status ERROR"
fi

But for some reason, when comparing, the $status variable and the string "HTTP/2 200" are not equal, although if you do echo $status, it returns just such a string.
How can I get $status = "HTTP/2 200"?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xotkot, 2020-02-11
@CloudZero

can be easier

status=$(curl -o /dev/null -Iksw "%{http_code}" https://example.com)

and compare the code itself with what you need

G
Germanjon, 2020-02-11
@Germanjon

The first thing that caught my eye was that the comparison should use two equal signs
if [ "$status" == "HTTP/2 200" ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question