Wednesday, October 27, 2021

[SOLVED] bash-4.3 How to interpret a string expression in bash as boolean

Issue

I have a Array like this

T[1]="x>1";
T[2]="y<2";
 .
 .

and some assignments like that

x=2;
y=10;
.
.

Finally, I need a logic based on the following terms

logic_value1=${T[1]} # but this is just a string and not Boolean 
logic_value2=${T[2]} #  ....." " "....
.
.

Can anybody Help? (I write in a procedure and therefore eval can not be used, and query Like that:

 select * 
 from table1 
 where  condition1 AND logic_value1

)

but for integer value of logic_value1 this Condition and query didn't work


Solution

In this specific case using arithmetic expansion would suffice.

T[1]="x>1";
T[2]="y<2";
x=2;
y=10;
logic_value[1]=$((${T[1]}))
logic_value[2]=$((${T[2]}))
declare -p logic_value
# outputs: declare -a logic_value=([1]="1" [2]="0")


Answered By - KamilCuk