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 ]
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' ]]