Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read variable until output is greater than a certain Value in BASH

Tags:

linux

bash

I have a command that has a numeric output, but sometimes, because of an error over which I have no control whatsoever, the output value is below 100. What I need is to read this output, and only continue if the output is greater than 100. The problem is that the "until loop" only reads the variable once. Even though the variable changes over time, the until loop only uses the output from the first variable reading. Is there some way in which I can force it to read the command output many times, until the right value is found?

VARIABLE=$(command)

until [  ${VARIABLE} -gt 100 ]; do
  sleep 0.1
done

echo "$(VARIABLE)"
like image 912
Tony Avatar asked Oct 25 '25 05:10

Tony


1 Answers

No fancy stuff needed, you just need to put the command in the loop and then have the loop condition as the condition you actually want to loop on.

while [[ "$variable" -le 100 ]];do 
    variable=$(command) 
    sleep 0.1 
done 

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!