Re:Debounce in Bash
by cryptonector on Monday September 24, 2018 @07:14PM
(#18062301)
When measuring elapsed times, instead of executing date(1) to get the current time, use $SECONDS:
Like so:
Try it like so:
Of course, that's a bad example in that yes(1) will go as fast as you let it and it always outputs the same thing. In the case of yes(1) the better thing to do would be to sleep between reads, but yes(1) is a special case.
[ Parent
| on HN ]
Re:Debounce in Bash
by anothergoogler on Monday September 24, 2018 @07:50PM
(#18062479)
It's worth noting that OP's example works in any Bourne-compatible shell. I don't think `date +%s` is costly enough to warrant losing the portability.
I wouldn't be worried about the performance cost of using `date +%s` personally, I'd be concerned about what happens when the user or an automated process changes the system time.
For example, what will happen in the debounce function if the user sets the system time backwards by a week after a limit has been set?
Re:Debounce in Bash
by anothergoogler on Tuesday September 25, 2018 @01:16AM
(#18063786)
Good point, didn't think of that.
Re:Debounce in Bash
by cryptonector on Tuesday September 25, 2018 @12:16PM
(#18067665)
The title said bash.
Replace "local" with "typeset" and what I wrote is compatible with KSH too -- not Bourne nor POSIX, I know, but still.