AND OR with if


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers AND OR with if
# 1  
Old 01-24-2006
AND OR with if

Hi,

I am try to set certain time parameters.
The if statement only seems to see the part before || or &&.
Please assist.
The code below.

if test `date +%a` != "Sat" || `date +%a` != "Sun"
then
day_type="weekday"
else
day_type="weekend"
fi

if test `date +%H%M` -gt 600 && `date +%H%M` -lt 2000
then
time="workhours"
else
time="afterhours"
fi

Thanks
# 2  
Old 01-24-2006
You don't say which shell you are using, but from the syntax I would guess you are using the original bourne shell.

You should use && for the first condition and you need an additional "test" after &&.
Code:
if test `date +%a` != "Sat" && test `date +%a` != "Sun"
then
    day_type="weekday"
else 
    day_type="weekend"
fi

if test `date +%H%M` -gt 600 && test `date +%H%M` -lt 2000
then
    time="workhours"
else
    time="afterhours"
fi

# 3  
Old 01-24-2006
Using Korn Shell, but it works like a charm.

Thanks for your promt assistance.
# 4  
Old 01-24-2006
'if' stattements can be modified in the following forms :

Code:
# First form

if test `date +%a` != "Sat" -a `date +%a` != "Sun"
then
    day_type="weekday"
else 
    day_type="weekend"
fi

# Second form

if [ `date +%H%M` -gt 600 -a  `date +%H%M` -lt 2000 ]
then
    time="workhours"
else
    time="afterhours"
fi

Another version :

Code:
week_day=`date +%u`
if [ $week_day -ge 6 ]
then
    day_type="weekday"
else 
    day_type="weekend"
fi

day_time=`date +%H%M`
if [ $day_time -gt 600 -a  $day_time -lt 2000 ]
then
    time="workhours"
else
    time="afterhours"
fi

 
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question