![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
|
|||
|
|||
|
Using Korn Shell, but it works like a charm.
Thanks for your promt assistance. |
|
#4
|
||||
|
||||
|
'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
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
|
||||
| Google The UNIX and Linux Forums |