Reputation
309
Next privilege 500 Rep.
Access review queues
Badges
4 15
Impact
~34k people reached

Jul
10
accepted How to influence a bash while loop with a timed global variable?
Jul
9
revised How to influence a bash while loop with a timed global variable?
added 4 characters in body
Jul
9
revised How to influence a bash while loop with a timed global variable?
deleted 1 character in body
Jul
9
revised How to influence a bash while loop with a timed global variable?
grammar
Jul
9
comment Security implications of forgetting to quote a variable in bash/POSIX shells
This should make one wonder if we are really not better off writing Python scripts instead?
Jul
9
revised How to influence a bash while loop with a timed global variable?
added 1 character in body
Jul
9
revised How to influence a bash while loop with a timed global variable?
To have a fair comparison with other answers, I expanded the code to a working example.
Jul
9
comment How to influence a bash while loop with a timed global variable?
Yes, I have. In my real-world example the heartbeat is every 1800s (half an hour) and the random process is an iteration of about 10 times 3 minutes. I created a more breve example because I did not want respondents to lose their time with this.
Jul
9
suggested approved edit on How to influence a bash while loop with a timed global variable?
Jul
9
comment How to influence a bash while loop with a timed global variable?
Nice code, but it does not take care of schedule slippage (see above comment by Scott).
Jul
9
revised How to influence a bash while loop with a timed global variable?
added 57 characters in body
Jul
9
revised How to influence a bash while loop with a timed global variable?
deleted 6 characters in body
Jul
8
answered How to influence a bash while loop with a timed global variable?
Jul
8
comment How to influence a bash while loop with a timed global variable?
I saved your script under the name test and performed a chmod +x on it. Here is the error I get when trying to run it: ./test: 5: ./test: function: not found
Jul
8
comment How to influence a bash while loop with a timed global variable?
The correct way to initialise is lastTime=$(date --date='5seconds ago' +%s). This then also allows to have lastTime=$((currentTime - elapsedTime +5)), which coincides with the original attempt.
Jul
8
comment How to influence a bash while loop with a timed global variable?
At the beginning of the script elapsedTime equals currentTime if lastTime does not exist. Replacing echo 'Something different' by echo $elapsedTime returned 1436384016 over here. This is the number of seconds since January 1, 1970.
Jul
8
comment How to influence a bash while loop with a timed global variable?
lastTime=-5 is not necessary because elapsedTime will be the number of seconds since 1970-01-01 00:00:00 UTC and therefore greater than 5. Furthermore, -gt should read -ge.
Jul
8
revised How to influence a bash while loop with a timed global variable?
removed a double `then`
Jul
8
suggested approved edit on How to influence a bash while loop with a timed global variable?
Jul
8
revised How to influence a bash while loop with a timed global variable?
added 57 characters in body