Monday, March 21, 2022

I Must Have Too Much Time Using Ksh

 In bash, I want a while loop that compares two values and runs if `echo "-.22 > -.25" | bc` is true.

But how?  That expression returns 1 which is apparently not the boolean true.

This should be simpler than it is:

while [ `echo "$z < 1" |bc` ]

do

echo $z

done

Yes, z does not change but what I get is 

standard_in) 1: syntax error

Yet 

while [ 1 ]; do echo test; done

does what I expect; a continuous loop of test.

4 comments:

  1. Try using parenthesis instead of backticks around it.

    At least in bash, backticks tells the computer to EXECUTE the result of that statement. Parenthesis should RETURN the result of the statement.

    ReplyDelete
  2. I think the answer is: "you need a lot more parentheses". https://stackoverflow.com/questions/8654051/how-can-i-compare-two-floating-point-numbers-in-bash

    ReplyDelete
  3. You may need an extra layer of quotes to protect the greater than from being interpreted as a redirect.

    ReplyDelete
  4. Example:

    z=1
    while (( $(echo "$z < 2" |bc) ))
    do
    echo $z
    z=$(echo "$z + 0.1" | bc)
    done



    ReplyDelete