Operating System - Linux
1745783 Members
3789 Online
108722 Solutions
New Discussion юеВ

Re: IF statement construct

 
SOLVED
Go to solution
Berd
Trusted Contributor

IF statement construct

I'm currently looking to edit a if statement within script.
The current syntax is

if [[ $(date +%H) -gt 8 ]]

so if it is after 08:00 the next statement runs. I want the if statement to say that if its -gt 8 and -lt 21 , how would I oncorporate that ??

Cheers,
Berd
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: IF statement construct

Your problem is both a syntactical one and a logical one. First, it is possible that the very thing you are testing (time) could change while doing the test so it's wiser to capture it in a variable and then test the variable.

CURRHR=$(date '+%H')
if [[ ${CURRHR} -gt 8 && ${CURRHR} -lt 21 ]]
then

fi
--- or to use the external test command
if [ ${CURRHR} -gt 8 -a ${CURRHR} -lt 21 ]
then

fi



If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: IF statement construct

Hi Berd:

# if [[ $(date +%H) -gt 8 && $(date +%H) -lt 21 ]]

...although it would be better to first capture the hour in a variable to avoid the repetitive call:

# H=$(date +%H)
# if [[ "${H}" -gt 8 && "${H}" -lt 11 ]]

Regards!

...JRF...
Berd
Trusted Contributor

Re: IF statement construct

Cheers guys for that, I will amend script as suggested.

Berd