Perl One-liner: countdown on the command line:
This one-line written in Perl will show a coundown on the command line: Try it.
perl -e '$|=1; print "hi\n"; $sec = 180; while ($sec--) { printf "Starting in %3s seconds\r", $sec; sleep 1 }'
Explanation:
- -e tells perl to execute the string following it as perl code
- $| = 1; tells perl to avoid buffering STDOUT, that is to make every print arrive to the screen immediately
- print "hi\n"; there only to be nice
- $sec = 180; sets the timer in seconds.
- The key however is that we print \r carrige return at the each of each print instead of \n newline. Thus each print overwrites the output of the previous one creating the effect of only the counter number changing.
Published on 2021-04-10
If you have any comments or questions, feel free to post them on the source of this page in GitHub. Source on GitHub.
Comment on this post