Here's the script.
#!/bin/bash
set -e
echo ""
declare -i value=00
unset optics_status
optics_status=$(curl -I xx.xx.xx.xx:4512 | grep 'X-Trace' | awk '{print $NF}' | rev |cut -c 1-3 | rev)
if [ $optics_status != $value ]; then
echo "result is different means I am in if loop"
echo "stats is $optics_status"
else
echo "result is equal means I am in else loop"
echo "stats is $optics_status"
fi
xx.xx.xx.xx is a server IP and if I run the curl command separately this is the output I get.
user@machine-lap:~/Documents$ curl -I xx.xx.xx.xx:4512 | grep 'X-Trace' | awk '{print $NF}' | rev |cut -c 1-3 | rev
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 8359 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
00
user@machine-lap:~/Documents$
As you see I've set value to 00 and the output of optics_status is also 00. So according to the if loop, it should echo i am in else loop but it just echos the i am in if loop and I can't understand why.
To put it in simpler words, its echoing the if statement instead of else.
bashdoes not retain zero padding. The string"$value"would be"0".basharithmetic on non-zero numbers with a leading zero digit it will treat them as octal. Generally it's better to apply padding at the last possible moment - in the output statement, for example.