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.
Try using parenthesis instead of backticks around it.
ReplyDeleteAt least in bash, backticks tells the computer to EXECUTE the result of that statement. Parenthesis should RETURN the result of the statement.
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
ReplyDeleteYou may need an extra layer of quotes to protect the greater than from being interpreted as a redirect.
ReplyDeleteExample:
ReplyDeletez=1
while (( $(echo "$z < 2" |bc) ))
do
echo $z
z=$(echo "$z + 0.1" | bc)
done