In bash shell, how can be value checked if within range by most effective way?
Example:
now=`date +%H%M`
if [ $now -ge 2245 ] && [ $now -le 2345 ] ; then
...
fi
…this one is working, but with using now variable.
Other option is:
if [ $((`date +%H%M`)) -ge 2245 ] && [ $((`date +%H%M`)) -le 2345 ] ; then
...
fi
…without variable, but with execution of date twice.
How to do it with one date execution and no variable at all?
First off, as a general rule, I’m pretty sure you need EITHER to use a variable OR run the command twice to do multiple comparisons on arbitrary numbers. There is no such notation as
if [ 1000 -lt $(date '+%H%M') -lt 2000 ];.Also, you don’t need to put your backquoted commands inside $((…)). The result of the backquoted command is a string which
/bin/[will be interpreted by -gt or -le as a number.That said, as an option for the times in your example, you can try using a smarter
datecommand line.In FreeBSD:
Or in Linux: