BASH if conditions

Posted by Daniil on Stack Overflow See other posts from Stack Overflow or by Daniil
Published on 2010-06-14T14:53:15Z Indexed on 2010/06/14 19:32 UTC
Read the original article Hit count: 369

Filed under:
|

Hi, I did ask a question before. The answer made sense, but I could never get it to work. And now I gotta get it working. But I cannot figure out BASH's if statements. What am I doing wrong below:

START_TIME=9
STOP_TIME=17
HOUR=$((`date +"%k"`))
if [[ "$HOUR" -ge "9" ]] && [[ "$HOUR" -le "17" ]] && [[ "$2" != "-force" ]] ; then
    echo "Cannot run this script without -force at this time"
    exit 1
fi

The idea is that I don't want this script to continue executing, unless forced to, during hours of 9am to 5pm. But it will always evaluate the condition to true and thus won't allow me to run the script.

./script.sh [action] (-force)

Thx

Edit: The output of set -x:

$ ./test2.sh restart
+ START_TIME=9
+ STOP_TIME=17
++ date +%k
+ HOUR=11
+ [[ 11 -ge 9 ]]
+ [[ 11 -le 17 ]]
+ [[ '' != \-\f\o\r\c\e ]]
+ echo 'Cannot run this script without -force at this time'
Cannot run this script without -force at this time
+ exit 1

and then with -force

$ ./test2.sh restart -force
+ START_TIME=9
+ STOP_TIME=17
++ date +%k
+ HOUR=11
+ [[ 11 -ge 9 ]]
+ [[ 11 -le 17 ]]
+ [[ '' != \-\f\o\r\c\e ]]
+ echo 'Cannot run this script without -force at this time'
Cannot run this script without -force at this time
+ exit 1

© Stack Overflow or respective owner

Related posts about bash

Related posts about scripting