1

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)"

7
  • 1
    What is comparison_cmd? For my taste there's far too much eval and unquoted variable expansion going on there. Also, why the double $( ) in the last line? Commented May 15, 2019 at 21:35
  • 3
    Your last condition is always true because you're testing a single string ([ '...' ]), which just checks whether the string is non-empty. Commented May 15, 2019 at 21:36
  • 1
    [ is the name of a command. It's not bracketing syntax. Commented May 15, 2019 at 21:36
  • You need to make your question clearer. For testing purposes, might we assume result could be something like +123.456 or -0.0005? Commented May 15, 2019 at 21:59
  • 1
    BTW, note that in general, commands should never be stored in string variables. Use shell functions, or when you need to build them using conditional logic, arrays. BashFAQ #50 describes the pitfalls of the naive approach, and BashFAQ #48 describes the pitfalls of eval. Commented May 15, 2019 at 22:05

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

5 Comments

Note that a few places in this answer run afoul of BashPitfalls #14.
@Charles Duffy Thanks. I was leaving that in as an exercise to the reader ;-) But surely you're not calling on me to change all echoes into printf's...?
Nawp, just trying to increase the proportion of shellcheck-clean answers. :)
@Charles Duffy Yes, I've seen you in action. There still is the output from the bc test, but anyway. Shellcheck is a relatively new discovery for me. I use it too little, it needs to be brought more to the forefront in my little brain. Thanks for it! [Time passes...] I see you beat me to it. Generous -- double thanks!
It's a matter of taste, but I'd be inclined to use the shell's built-in string editing instead of cut: parse="${result#*(}"; parse="${parse%%)*}" (the first trims the front of the string up to the first "(", the second trims from the first ")" to the end).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.