3
#!/bin/bash
if [ `date +%u` -lt 6 && `date +%H` == '19' ] ; then
   echo 'Sorry, you cannot run this program today.'
else
   echo 'yes, you can run today'
fi

The script above is to run a program on weekdays and every 7PM. I have check the spaces and it still return error : date.sh: 2: [: missing ]

2 Answers 2

6

Change it to:

#!/bin/bash
if [ `date +%u` -lt 6  ] && [ `date +%H` == '19' ] ; then
   echo 'Sorry, you cannot run this program today.'
else
   echo 'yes, you can run today'
fi

Note that the [ is just a shorthand for the test command and ] the last argument to it. The && operator is used like in any other command line, for example cd /home && ls

Sign up to request clarification or add additional context in comments.

5 Comments

I've tested the same code and it worked. Are you sure your shell is bash ? which bash version?
sicksand@awalfathi:~$ echo $BASH_VERSION 4.2.25(1)-release
me too. can you post the script you are currently trying to pastebin?
awesome! this is the same as my - working - code. Make sure that there are no hidden chars like windows line feeds in the file
If unsure use fromdos script.sh
5

There are two problems with your code.

The first is a simple typo: date %H should be date +%H.

The second is the cause of the error you're seeing: && isn't the right operator to use. It separates commands, whereas your if condition should really be a single one; bash looks for ] at the end of [ `date +%u` -lt 6, doesn't find it, and errors out. You want to use -a instead.

2 Comments

[code]#!/bin/bash if [ date +%u -lt 6 -a date +%H == '19' ] ; then echo 'Sorry, you cannot run this program today.' else echo 'yes, you can run today' fi [/code] return date.sh: 2: [: 13: unexpected operator
I don't know what you're doing wrong, but both my solution and @hek2mgl's are syntactically correct and work. The 13 it's complaining about is the result of date +%H; I can't imagine why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.