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
CodePudding user response:
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
CodePudding user response:
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