Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Script error: [: missing ]

Tags:

bash

scripting

#!/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 ]

like image 216
Shafiq Mustapa Avatar asked Dec 03 '25 16:12

Shafiq Mustapa


1 Answers

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

like image 197
hek2mgl Avatar answered Dec 06 '25 07:12

hek2mgl