I know this sounds silly, but had a rough start in this week and i'm not able to think clearly.
I'm writing this simple piece of code, which is suppose to get little complex later on. However I'm stuck near this simple If condition statement.
I wrote the code in VS on my host Ubuntu, copied it to a file inside a docker container and executed it.
As you see, the left side of the IF condition is taken as a null value for comparision. Where am i going wrong ?
#!/bin/bash
someEquation()
{
cluster_state=`src/redis-cli -h 127.0.0.1 -p 36000 cluster info | grep cluster_state | awk -F':' '{print$2}'`
if [[ " ${cluster_state} " == "fail" ]]; then
echo "arr contains fail"
fi
}
someEquation
Output:
+ someEquation
++ src/redis-cli -h 127.0.0.1 -p 36000 cluster info
++ grep cluster_state
++ awk -F: '{print$2}'
+ cluster_state=$'fail\r'
== \f\a\i\l ]]
If variable cluster_state
always has a trailing carriage return (hex: 0d), remove last character from variable before comparison:
[[ "${cluster_state%?}" == "fail" ]]
or add a carriage return on the other side:
[[ "${cluster_state}" == "fail"$'\r' ]]