1

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 ]]
Cyrus
  • 84,225
  • 14
  • 89
  • 153
theborngeek
  • 121
  • 1
  • 7

2 Answers2

3

By seeing OP's attempt editing/improving OP's code here.

  • I have attached logic of removing control m chars with your logic here.
  • Also when we are using awk we need not to use grep with it so I removed it too, as an improvement. But fair warning haven't tested it should work but.
  • as per Barmar sir's comments removed spaces from variable else it will give wrong results.
  • Changed backtick to $(....) too for saving command's value to a variable.
someEquation()
{
    cluster_state=$(src/redis-cli -h 127.0.0.1 -p 36000 cluster info | awk -F':' '/cluster_state/{gsub(/\r/,"",$2);print $2}')
    if [[ "${cluster_state}" == "fail" ]]; then
        echo "arr contains fail"
    fi
}

someEquation
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • thanks for the awk tip, however during condition, i get it as `[[ fai == \f\a\i\l ]]` instead of fail. also can you guide me to the best awk guide which shows using gsub as well ? – theborngeek Aug 04 '20 at 19:59
  • @theborngeek, i think `\r` may be creating space so may be you getting you fai but can't tell without seeing samples. For learning awk i always recommend this link of SO https://stackoverflow.com/tags/awk/info. Cheers and happy learning – RavinderSingh13 Aug 04 '20 at 20:08
3

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' ]]
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • this fixed my issue, when or in what situations do we usually end up with carriage returns ? Cuz i never encountered them until today as I wrote many other values of some redis cli commands to various variables and used them in many conditions. – theborngeek Aug 04 '20 at 19:53
  • I think here redis-cli is the problem. – Cyrus Aug 04 '20 at 19:58
  • 2
    @theborngeek You could use `[[ "${cluster_state%$'\r'}" == "fail" ]]` -- that'll remove the carriage return *if it exists* before comparing, so it should work whether or not there's a carriage return there. – Gordon Davisson Aug 05 '20 at 00:59