You are not logged in.
Hi everybody,
I know that some people here write or use bash scripts, and there's recently been a thread about alarm clock scripts.
https://forums.bunsenlabs.org/viewtopic.php?id=6257
Here's something similar; I've been looking for a way to have a simple, unobtrusive countdown timer, and came across this script (which works in a terminal and counts down 20 seconds at a time);
seconds=20; date1=$((`date +%s` + $seconds));
while [ "$date1" -ge `date +%s` ]; do
echo -ne "$(date -u --date @$(($date1 - `date +%s` )) +%H:%M:%S)\r";
done
I'm using it now and can testify that it works.
Here's a thread with some more and similar ideas in it;
https://superuser.com/questions/611538/ … a-terminal
Last edited by Colonel Panic (2021-03-26 19:11:15)
Offline
Thanks
Offline
... bash ... works in a terminal and counts down 20 seconds at a time ...
seconds=20; date1=$((`date +%s` + $seconds));
while [ "$date1" -ge `date +%s` ]; do
echo -ne "$(date -u --date @$(($date1 - `date +%s` )) +%H:%M:%S)\r";
done
This is actually pretty resource intensive; adding a "sleep 0.1" already reduces the strain significantly.
But why call date twice in a loop over and over when all you want is to count down seconds...
seconds=20
end_seconds=$((SECONDS+seconds))
while ((SECONDS<end_seconds)); do
printf ' %2d\r' "$((end_seconds-SECONDS))"
sleep 0.1
done
see 'man bash' => Shell variables => SECONDS.
Offline
1. An zsh oneliner attempt
c=20; repeat c {(( c-- )); sleep 1; echo $c}
2. Similar, but countdown in single line
c=20; d="$c"; echo -ne "$c\r"; repeat c {(( c-- )); sleep 1; echo -ne "${(l(2)(0))c}\r"} && echo "$d seconds has passe
d."
3. A fun one that draws a little reverse staircase using perc2 script
c=20; set +m; perc=$(( 100.0/c )); perc2 100 & repeat c {(( c-- )); sleep 1 & var="$(perc2 $(( perc * c )))" ; wait; echo "$var" }
Note: set +m disables shell monitoring, so that it will not report subshell returns.
███████████████████████████████████████████████████████████████████████████▎
█████████████████████████████████████████████████████████████████▊
████████████████████████████████████████████████████████▍
███████████████████████████████████████████████
█████████████████████████████████████▋
████████████████████████████▎
██████████████████▊
█████████▍
p.s. Both examples will not achieve millisecond precision. To test:
time ( c=20; set +m; perc=$(( 100.0/c )); perc2 100 & repeat c {(( c-- )); sleep 1 & var="$(perc2 $(( perc * c )))" ; wait; echo "$var" } )
# returns: 20.156 total on this machine.
Last edited by brontosaurusrex (2021-04-11 09:05:09)
Offline
p.s. Both examples will not achieve millisecond precision.
Because you rely on the sleep command to measure time; the other examples above actually measure the time (one with `date`, the other with the bash builtin SECONDS) while counting down.
Offline
Your version takes 19 seconds, so I guess It's superluminal?
edit: The ops version takes 20.238s
I win.
Prettyfied script version (zsh) of my 2nd example. And a progress-bar version (also zsh).
Last edited by brontosaurusrex (2021-04-11 13:08:51)
Offline
Your version takes 19 seconds, so I guess It's superluminal?
edit: The ops version takes 20.238s
I win.
Prettyfied script version (zsh) of my 2nd example. And a progress-bar version (also zsh).
My version takes 19s because it stops 1s early.
But the logic of my argument stands: since you never know how long actual script execution takes, you cannot rely solely on the sleep delays to measure time. It will get less precise the longer it runs.
(My version also uses sleep, but only to keep it from taking 100% CPU, not to measure time.)
But whatever, just continue faffing around with your version. It sure is prettier.
Last edited by ohnonot (2021-04-12 04:45:24)
Offline
Uhmm, ok, still you two are doing low-level stuff in bash script, my 1st script reports 0% cpu usage for example. I'am fine with tie, little reluctant, but ok
Offline
Doing some 'research', this one is neat (and correct and low cpu)
https://github.com/trehn/termdown (pip install termdown)
Similar in go (super correct)
https://github.com/antonmedv/countdown
time countdown 25s -up
countdown 25s -up 0.02s user 0.00s system 0% cpu 25.011 total
Last edited by brontosaurusrex (2021-04-13 09:23:28)
Offline
Good to see this thread's still going although I don't have the knowledge to contribute further. I haven't read all the posts yet but I like the two (termdown and countdown) brontosaurusrex has just mentioned.
Last edited by Colonel Panic (2021-04-17 23:53:24)
Offline