Issue
Having issues comparing 2 response codes in shell. Running curl and need to validate if response is between 200 and 400. Also, there is a chance of response to be "000" when a server is down.
#!/bin/sh
response1="200" #curl first url
response2="000" #curl second url
if (( $response1 -ge 400 || $response1 -lt 200 || $response2 -ge 400 || $response2 -lt 200 )) ; then
echo "Something went wrong, response code is not in success range"
exit 1
else
echo "Success"
exit 0
fi
((: 200 -ge 400 || 200 -lt 200 || 000 -ge 400 || 000 -lt 200 : syntax error in expression (error token is "400 || 200 -lt 200 || 000 -ge 400 || 000 -lt 200 ")
If I change the brackets to the [[...]] it always returns true. If I change -lt to < and -ge to >= getting the following error:
((: 200 = 400 || 200 < 200 || 000 = 400 || 000 < 200 : attempted assignment to non-variable (error token is "= 400 || 200 < 200 || 000 = 400 || 000 < 200 ")
Solution
Wrong operator. Correct way of writing is:
if (( response1 > 400 || response1 < 200 || response2 > 400 || response2 < 200 )) ; then
No need to explicitly dereference with $
, als long as it is ensured that your variables contain just integer numbers.
Answered By - user1934428