CalebK
CalebK

Reputation: 707

Bash script if statement not properly evaluating string/float comparison (bracketing)

I am having the most confusing time understanding how exactly bash evaluates statements. I wrote the following script that works perfectly. Given an input float it is able to do the comparison properly. Here is the script:

read test
if [ $( echo "$test < 0.001" | bc) -eq 1 ]; then
    echo "CONDITION"
else
    echo "HI"
fi

Then I added it to a larger bash script here

result=`eval "${comparison_cmd}"`
parse="$(echo $result | cut -d "(" -f2 | cut -d ")" -f1)"
echo $parse
if [ $( echo "$parse < 0.001" | bc) -eq 1 ]; then

For some background result ends up being a string like "GARBAGE (important number)" so the goal is to take the important number that is within parenthesis and use that for the comparison.

This seems to be evaluating as true no-mater what and I am not exactly sure why bash is not interpreting this statement as expected. I did have to put the if statement in '' to remove a syntax error. NOTE I have removed it above since apparently that statement is always true.

Comparison command is a bash script that needs to be run in order to get the string "GARBAGE (important)"

Upvotes: 1

Views: 131

Answers (1)

user2849202
user2849202

Reputation:

Does this do what you want:

parse="$(echo "$result" | cut -d "(" -f2 | cut -d ")" -f1)"
echo "$parse"
if [ "$(echo "$parse < 0.001" | bc)" -eq 1 ]; then
    echo foo
else
    echo bar
fi

Upvotes: 1

Related Questions