Bash while 在两次之间循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16928148/
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
Bash while loop between two times
提问by Iz3k34l
I am trying to write a bash script that will loop if the time is after certain time and before a certain time. Here is what i have got but it gives me an error;
我正在尝试编写一个 bash 脚本,如果时间在特定时间之后和特定时间之前将循环。这是我所得到的,但它给了我一个错误;
#!/bin/bash
while [ date +"%T" -gt '06:00:00' && date +"%T" -lt '21:00:00'];
do
##echo `php mainstatquery.php`
echo "Hello World";
sleep 5;
done
and this is the error
这是错误
./timer.sh: line 3: [: missing `]'
If anyone could point me in the right direction.
如果有人能指出我正确的方向。
采纳答案by David W.
Here's an easy way to understand your whileproblems:
以下是了解您的while问题的简单方法:
The [is an actual Unix command. Do ls /bin/[and you'll see. It's a link to the /bin/testcommand. Do a man testfrom the command line and see the various teststhat can be done.
这[是一个实际的 Unix 命令。做ls /bin/[,你会看到。这是/bin/test命令的链接。man test从命令行执行 a并查看可以执行的各种测试。
What the whiledoes is execute the command you give it, and if it returns a zero exit status, the whilestatement is considered true and the statement will continue. You can do things like this:
它的while作用是执行您给它的命令,如果它返回零退出状态,则该while语句被认为是正确的,并且该语句将继续。你可以做这样的事情:
while sleep 2
do
echo "I slept for two seconds"
done
All the testcommand does is do some sort of testing (file tests, tests of equality, etc.) and return a zero if the statement is true and a non-zero otherwise:
所有的test命令确实是做一些测试(文件测试,平等的测试等),并返回一个零,如果该声明是真实的和非否则为零:
$ test 2 -gt 3
$ echo $?
1 <- That statement is false
$ [ 2 -lt 3 ] #The test command using square brackets
$ echo $?
0 <- That statement is true.
Take a look at the manpage for testand you'll see all valid tests. This:
查看手册页test,您将看到所有有效的测试。这个:
while [ date +"%T" -gt '06:00:00' && date +"%T" -lt '21:00:00']
is the same as
是相同的
while test date +"%T" -gt '06:00:00' && date +"%T" -lt '21:00:00'
Let's go through a few things here:
让我们来看看这里的一些事情:
- date + %T is not a valid operator for the
testcommand.
- date + %T 不是
test命令的有效运算符。
The testcommand can't execute a command internally. What you need to do is to put that command in $(..)and probably use quotes to play it safe. Thus:
该test命令无法在内部执行命令。您需要做的是将该命令放入$(..)并可能使用引号来确保安全。因此:
while test "$(date +"%T")" -gt '06:00:00' && "$(date +"%T")" -lt '21:00:00'
- The
&&is not a valid operator in thetestcommand. What you probably want is-awhich is the andconjunctive for stringing together two tests intest.
- 该
&&不是在一个有效的操作test命令。你可能想要的是-a这是和连接性在两个测试串在一起test。
This would give you:
这会给你:
while test $(date +"%T") -gt '06:00:00' -a "$(date +"%T")" -lt '21:00:00'
There are two separate greater thantest operators for comparisons. One is for strings and one is for integers. The
-gtis the test for integers. Since you're dealing with strings, you need to use>:while test "$(date +"%T")" > '06:00:00' -a "$(date +"%T")" < '21:00:00'
有两个单独的大于测试运算符用于比较。一种用于字符串,一种用于整数。这
-gt是对整数的测试。由于您正在处理字符串,因此您需要使用>:而测试 "$(date +"%T")" > '06:00:00' -a "$(date +"%T")" < '21:00:00'
As an alternative, you could have also used the &&conjunctive instead of -a, but each side of the &&would have to be separate test statements:
作为替代方案,您也可以使用&&连接词代替-a,但 的每一侧&&都必须是单独的测试语句:
while test "$(date +"%T")" > '06:00:00' && test "$(date +"%T")" < '21:00:00'
Now, let's convert the testsyntax back to [...]because it's easier on the eyes
现在,让我们将test语法转换回,[...]因为它看起来更容易
while [ "$(date +"%T")" > '06:00:00' -a "$(date +"%T")" < '21:00:00' ]
OR
或者
while [ "$(date +"%T")" > '06:00:00' ] && [ "$(date +"%T")" < '21:00:00' ]
By the way, the internal [[...]]is better -- especially with the >sign since it can be interpreted by the shell as a file redirect.
顺便说一句,内部[[...]]更好——尤其是带有>符号的,因为它可以被外壳程序解释为文件重定向。
while [[ "$(date +"%T")" > '06:00:00' -a "$(date +"%T")" < '21:00:00' ]]
OR
或者
while [[ "$(date +"%T")" > '06:00:00' ]] && [[ "$(date +"%T")" < '21:00:00' ]]
回答by fedorqui 'SO stop harming'
You cannot compare an entire hour like that.
你不能像那样比较一整个小时。
As you are just checking the hour (6<=x<=21), use date "+%H"to do that:
由于您只是在检查小时 (6<=x<=21),因此请使用以下date "+%H"方法:
while [ $(date +"%H") -gt 06 ] && [ $(date +"%H") -lt 21 ];
do
##echo `php mainstatquery.php`
echo "Hello World";
sleep 5;
done
Also take into consideration that the dateexpression has to be called with $()to make it get the result. And your
还要考虑date到必须调用表达式才能$()获得结果。和你的
while [ date +"%T" -gt '06:00:00' && date +"%T" -lt '21:00:00'];
$( ) ] && [ $( ) ^ need space
Finally, the logical condition is not well written. The best POSIX recommendedway is:
最后,逻辑条件写得不好。最好的POSIX 推荐方法是:
if [ -f $VAR1 ] && [ -f $VAR2 ] && [ -f $VAR3 ]
回答by cforbish
It looks like you only care about the hour so:
看起来你只关心小时,所以:
#!/bin/bash
while [ $(date +"%k") -gt 6 -a $(date +"%k") -lt 21 ];
do
##echo `php mainstatquery.php`
echo "Hello World";
sleep 5;
done
回答by cforbish
The ]that closes a [command must be a separate argument. Put a space before it.
所述]封闭一个[命令必须是一个单独的参数。在它之前放一个空格。

