Bash while loop between two

I am trying to write a bash script that will loop if the time is after a certain time and before a certain time. This is what I got, but it gives me an error;

#!/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 a mistake

 ./timer.sh: line 3: [: missing `]' 

If someone can point me in the right direction.

+4
source share
4 answers

Here is an easy way to understand your while problems:

[ is a real Unix team. Do ls /bin/[ and you will see. This is a link to the /bin/test command. Make a man test from the command line and see the various tests that you can run.

What the while does is execute the command you give it, and if it returns a zero exit status, the while statement is considered true, and the instruction will continue. You can do things like this:

 while sleep 2 do echo "I slept for two seconds" done 

The entire test command does some kind of testing (file tests, equality tests, etc.) and returns zero if the statement is true and not equal to zero:

 $ 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. 

Check the man page for test and you will see all the current tests. It:

 while [ date +"%T" -gt '06:00:00' && date +"%T" -lt '21:00:00'] 

coincides with

 while test date +"%T" -gt '06:00:00' && date +"%T" -lt '21:00:00' 

Let's look at a few things here:

  • date +% T is not a valid statement for the test command.

The test command cannot execute the command on its own. What you need to do is put this command in $(..) and probably use quotes to play it safely. In this way:

 while test "$(date +"%T")" -gt '06:00:00' && "$(date +"%T")" -lt '21:00:00' 
  • && not a valid statement in the test command. You probably need -a , which is also conjunctive for combining two tests into test .

This will give you:

 while test $(date +"%T") -gt '06:00:00' -a "$(date +"%T")" -lt '21:00:00' 
  • For comparison, there are two separate test operators. One for strings, and one for integers. -gt is a test for integers. Since you are dealing with strings, you need to use > :

    while test "$ (date +"% T ")"> '06: 00: 00 '-a "$ (date +"% T ")" <'21: 00: 00'

Alternatively, you could also use the && connection instead of -a , but each && side should have been separate test statements:

 while test "$(date +"%T")" > '06:00:00' && test "$(date +"%T")" < '21:00:00' 

Now convert the test syntax back to [...] because it is simpler before our eyes

 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 [[...]] better - especially with the > sign, since it can be interpreted by the shell as file redirection.

 while [[ "$(date +"%T")" > '06:00:00' -a "$(date +"%T")" < '21:00:00' ]] 

OR

 while [[ "$(date +"%T")" > '06:00:00' ]] && [[ "$(date +"%T")" < '21:00:00' ]] 
+4
source

You cannot compare all this hour.

Since you are just checking the hour (6 <= x <= 21), use date "+%H" for this:

 while [ $(date +"%H") -gt 06 ] && [ $(date +"%H") -lt 21 ]; do ##echo `php mainstatquery.php` echo "Hello World"; sleep 5; done 

Also note that the date expression must be called with $() to get the result. And yours

 while [ date +"%T" -gt '06:00:00' && date +"%T" -lt '21:00:00']; $( ) ] && [ $( ) ^ need space 

Finally, the logical condition is poorly written. Best POSIX recommended :

 if [ -f $VAR1 ] && [ -f $VAR2 ] && [ -f $VAR3 ] 
+3
source

It looks like you only care about the hour like this:

 #!/bin/bash while [ $(date +"%k") -gt 6 -a $(date +"%k") -lt 21 ]; do ##echo `php mainstatquery.php` echo "Hello World"; sleep 5; done 
+1
source

] , which closes the command [ , should be a separate argument. Put a space in front of it.

0
source

Source: https://habr.com/ru/post/1484515/


All Articles