The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


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 !!

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-24-2006
Registered User
 

Join Date: Jan 2006
Posts: 4
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
Reply With Quote
Forum Sponsor
  #2  
Old 01-24-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,248
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
Reply With Quote
  #3  
Old 01-24-2006
Registered User
 

Join Date: Jan 2006
Posts: 4
Using Korn Shell, but it works like a charm.

Thanks for your promt assistance.
Reply With Quote
  #4  
Old 01-24-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
'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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:00 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0