i wanna ask, just create simple shell script from curl with this condition. but when the curl status is true the else condition is never success, what is my misatake? any suggestion will be appreciate. Thanks
#!/bin/bash
code=$(curl -sX GET "http://10.243.215.143:8088/healthcheck")
if [[ $code != true ]];
then
echo "restart service"
else
echo "service is up"
fi
my expectation is where the curl status all true the script running the else condition.
You are using command substitution, the result can't be boolean. When you use the format
VARNAME=$(command)
$VARNAME will contain the output of command, that is normally a string.
In addition you are receiving output in json format, so you'll have to extract a specific field from the json, I guess "isHealthy".
You should review your script as follow
#!/bin/bash
code=$(curl -sX GET "http://10.243.215.143:8088/healthcheck" | jq ".isHealthy")
if [[ "$code" != "true" ]];
then
echo "restart service"
else
echo "service is up"
fi
On the other hand, if you want to check curl return code just call the curl command, then you'll have curl return code in the special $? bash variable.