The other answers overlook that when a number starts with 0
, Bash will interprete it in radix 8†. So, e.g., when it's 9am, date '+%H%M'
will return 0900
which is an invalid number in Bash. (not anymore).
A proper and safe solution, using modern Bash:
while :; do
current=$(date '+%H%M') || exit 1 # or whatever error handle
(( current=(10#$current) )) # force bash to consider current in radix 10
(( current > 1110 && current < 1430 )) && run command # || error_handle
sleep 10
done
Could be shortened a bit if you accept a potential 10s delay for the first run:
while sleep 10; do
current=$(date '+%H%M') || exit 1 # or whatever error handle
(( current=(10#$current) )) # force bash to consider current in radix 10
(( current > 1110 && current < 1430 )) && run command # || error_handle
done
Done!
† Look:
$ current=0900
$ if [[ $current -gt 1000 ]]; then echo "does it work?"; fi
bash: [[: 0900: value too great for base (error token is "0900")
$ # oooops
$ (( current=(10#$current) ))
$ echo "$current"
900
$ # good :)
As xsc points out in a comment, it works with the ancient [
builtin... but that's a thing of the past :)
.