Link to home
Start Free TrialLog in
Avatar of toooki
toooki

asked on

UNIX shell script question

I have a UNIX script that calls a java program and I want to make changes to it so that the script runs the java program only if certain date/time condition is met. The bash script is called by a cronjob every hour or so.

The original script is:
#!/bin/bash
SCRIPTPATH=`dirname $SCRIPT`
java -jar ${SCRIPTPATH}/MYPROG.jar

I want to update it to (pseudo code):

#!/bin/bash
SCRIPTPATH=`dirname $SCRIPT`
if (current GMT/UTC time day-of-week is Sunday and current GMT/UTC time hour-of-day is 00 or 01 or 02 or 03) then do nothing
else {
java -jar ${SCRIPTPATH}/MYPROG.jar
}

The date function on the server returns the UTC time. Could you please suggest how to write the above? The java program will not be called if the current UTC/GMT time is Sunday morning between midnight and 4am. Otherwise the java program will be called.
SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of toooki
toooki

ASKER

Thanks everyone.

It seems I could use the code:

if [[ $(date -u "+%a") == "Sun" && $(date -u "+%H") -lt 4 ]] ;then
 :
else
 java -jar ${SCRIPTPATH}/MYPROG.jar
fi

Is there any way to avoid the else part of the code above? Is the following same as the above?

if ![[ $(date -u "+%a") == "Sun" && $(date -u "+%H") -lt 4 ]] ;then
java -jar ${SCRIPTPATH}/MYPROG.jar
fi
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of toooki

ASKER

Thanks a lot. I will test and get back to you. Thanks!
FYI, an alternative to the"!" would be to Invert the tests and change the && to ||, so
if [[ $(date -u "+%a") != "Sun" || $(date -u "+%H") -ge 4 ]] ;then
    java -jar ${SCRIPTPATH}/MYPROG.jar
fi

Open in new window