Diego-S
Diego-S

Reputation: 77

Can I mute the output of the curl command in the bash script below?

I am attempting to check if a connection can be made with the url in the script, not sure if that is the best approach. The curl command's output is all going to stdout. I only want my echo to go to stdout after running the script below. I tried adding -s, but that did not help. I would appreciate some help.

check=$(curl -s -vv -I https://github.com | grep "HTTP/2 200")
if [[ -z "$check" ]]; then
  echo "Cnxn failed"
else
  echo "Cnxn successful"
fi

I would like to see something like this only:

Cnxn failed

or

Cnxn successful

Upvotes: 1

Views: 1754

Answers (2)

Vincent Stans
Vincent Stans

Reputation: 553

you get the return code by $?

curl -s -vv -I https://github.com/ardmy 2>/dev/null | grep -q "HTTP/2 200"
if [[ $? == 0 ]]; then
echo "Cnxn successful $?"
else
echo "Cnxn failed $?"
fi

Upvotes: 1

Aleksandr Pakhomov
Aleksandr Pakhomov

Reputation: 66

Please try this code

check=$(curl -s -vv -I https://github.com 2>/dev/null | grep "HTTP/2 200")
#echo $check
if [[ -z $check ]] ; then
      echo "Cnxn failed"
    else
      echo "Cnxn successful"
fi

Upvotes: 1

Related Questions