0

i want an if statement to only run if the condition in the statement is true for at least 3 consecutive seconds, how do i achieve it in linux bash shell script? i haven't done it before, and couldn't find anything specific by googling and would greatly appreciate any help! i don't mean like a loop that executes the condition x amount of times, but a function that checks of the condition is true for at least x consecutive seconds/minutes before it runs a block of code.

thanks in advance

  • Has any of the answers solved your problem? Please accept it (✓) or clarify your needs using editing or comments. You can also upvote (▲) useful answers, even if you have not the earned privilege yet (your votes won’t be counted until you get the privilege though). – Melebius Feb 19 at 7:15
1

This is not possible, you have to stick with the loop, or at least test, sleep 3, test again.

| improve this answer | |
1

Computers (digital ones) work with the discrete time, they don’t understand continuous time. In the discrete time, a condition “remains” true if it’s true in every sample. So you have to run a loop and count successful samples in it.

If you want to make the loop depending on the clock time rather than the repeat count, store the start time and test whether the expected time has elapsed in the loop condition.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.