3 ms·
When writing these things, I prefer using the “sleep” command as the while condition. This way, the loop is easier to break out of. I.e. when I press Ctrl-C,
by teddyh 5y ago
When writing these things, I prefer using the “sleep” command as the while condition. This way, the loop is easier to break out of. I.e. when I press Ctrl-C, the sleep command returns a non-zero exit code, and the while loop terminates gracefully:
while sleep 60; do clear; curl -s wttr.in; done
Otherwise, I very often get a loop which stubbornly refuses to abort, and I have to hammer both Ctrl-C and Ctrl-\ to make it stop. The downside is, of course, that I have to wait 60 seconds (in this case) the first time. When this is a problem, I just unroll the loop slightly and add an extra invocation to the beginning:
clear; curl -s wttr.in; while sleep 60; do clear; curl -s wttr.in; done
This could be generalized into a terminal watch function:
termwatch(){ delay="$1"; shift; clear; "$@"; while sleep "$delay"; do clear; "$@"; done; }
termwatch 60 curl -s wttr.in
- Kim_Bruning 5y agoThanks for your help igor_chubin and teddyh! According to The Fine Manual: watch --color (-c for short) , should render color correctly, eg. watch --color ls --color but watch --color curl -s v2.wttr.in doesn't quite play ball. In the end I went with something like your solution. I didn't need to put the sleep 60 as the first operation though. #!/bin/bash while true; do clear curl -s v2.wttr.in sleep 100 done ^C breaks out of this loop just fine on my system.
- teddyh 5y ago> ^C breaks out of this loop just fine on my system. It might be different when run as a script (with a separate shell process) to when run as an explicit loop in an interactive shell, or a shell function.