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=$'failr'
== fail ]]
2
Answers
By seeing OP’s attempt editing/improving OP’s code here.
awk
we need not to usegrep
with it so I removed it too, as an improvement. But fair warning haven’t tested it should work but.$(....)
too for saving command’s value to a variable.If variable
cluster_state
always has a trailing carriage return (hex: 0d), remove last character from variable before comparison:or add a carriage return on the other side: