bash shell 的值是否在范围内
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9636273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is value in range with bash shell
提问by ?mega
In bashshell, how can be value checked if within range by most effective way?
在bashshell 中,如何以最有效的方式检查值是否在范围内?
Example:
例子:
now=`date +%H%M`
if [ $now -ge 2245 ] && [ $now -le 2345 ] ; then
...
fi
...this one is working, but with using nowvariable.
...这个正在工作,但使用now变量。
Other option is:
其他选项是:
if [ $((`date +%H%M`)) -ge 2245 ] && [ $((`date +%H%M`)) -le 2345 ] ; then
...
fi
...without variable, but with execution of datetwice.
...没有变量,但执行date两次。
How to do it with one dateexecution and no variable at all?
如何通过一次date执行而完全没有变量?
回答by ghoti
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 ];.
首先,作为一般规则,我很确定您需要要么使用变量,要么运行该命令两次以对任意数字进行多次比较。没有这样的符号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.
此外,您不需要将反引号命令放在 $((...)) 中。反引号命令的结果是一个字符串,/bin/[它将被 -gt 或 -le 解释为一个数字。
if [ `date '+%H%M'` -gt 2245 -a `date '+%H%M'` -lt 2345 ]; then
That said, as an option for the times in your example, you can try using a smarter datecommand line.
也就是说,作为示例中的时间选项,您可以尝试使用更智能的date命令行。
In FreeBSD:
在 FreeBSD 中:
if [ `date -v-45M '+%H'` -eq 22 ]; then
Or in Linux:
或者在 Linux 中:
if [ `date -d '45 minutes ago' '+%H'` -eq 22 ]; then
回答by kev
You can use Shell Arithmeticto make your code clear.
您可以使用Shell Arithmetic使您的代码清晰。
now=`date +%H%M`
if ((2245<now && now<2345)); then
....
fi
回答by ruakh
I would write:
我会写:
if ( now=$(date +%H%M) ; ! [[ $now < 2245 ]] && ! [[ $now > 2345 ]] ) ; then
...
fi
which is mostly equivalent to your first example, but restricts the $nowvariable to a subshell (...), so at least it doesn't pollute your variable-space or risk overwriting an existing variable.
这与您的第一个示例大致相同,但将$now变量限制为 subshell (...),因此至少它不会污染您的变量空间或冒覆盖现有变量的风险。
It also (thanks to shellter's comment) avoids the problem of $nowbeing interpreted as an octal number when %H%Mis (for example) 0900. (It avoids this problem by using string comparison instead of integer comparison. Another way to avoid this problem would be to prefix all values with a literal 1, adding 10,000 to each of them.)
它还(感谢 shellter 的评论)避免了$now在%H%Mis (例如)时被解释为八进制数的问题0900。(它通过使用字符串比较而不是整数比较来避免此问题。避免此问题的另一种方法是在所有值前加上一个字面量110,000。)
回答by Chanaka Lasantha
#!/bin/bash
while :
do
MAX_TIME="1845"
MIN_TIME="1545"
if ( now=$(date +%H%M) ; ! [[ $now < $MIN_TIME ]] && ! [[ $now > $MAX_TIME ]] ) ;
then
echo "You are in time range and executing the commands ...!"
else
echo "Maximum Time is $MAX_TIME ...!"
# echo "Current Time is $now .....!"
echo "Minimum Time is $MIN_TIME ....!"
fi
sleep 4
done
#nohup sh /root/Date_Comp.sh > /dev/null 2>&1 &

