if, else help!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if, else help!
# 1  
Old 02-26-2009
Computer if, else help!

Hi,
I've got the following script:

YEAR=`date '+%Y'`
MONTH=`date '+%m'`
DAY=`date '+%d'`
HOUR=`date '+%H'`
MINUTE=`date '+%M'`
LASTHOUR=$((HOUR-1))

What i like to do is:

if $LASTHOUR < 9 then
function1
else
function2

tnx
# 2  
Old 02-26-2009
Hammer & Screwdriver

Code:
> LASTHOUR=8
> if [ $LASTHOUR -lt 9 ]; then echo "less"; else echo "more"; fi
less
> LASTHOUR=10
> if [ $LASTHOUR -lt 9 ]; then echo "less"; else echo "more"; fi
more

# 3  
Old 02-26-2009
tnx body
# 4  
Old 02-26-2009
Quote:
Originally Posted by mehrdad68
Hi,
I've got the following script:
Code:
YEAR=`date '+%Y'`
MONTH=`date '+%m'`
DAY=`date '+%d'`
HOUR=`date '+%H'`
MINUTE=`date '+%M'`


Not only are multiple calls to date unnecessary (and slow), but the script will fail if the time passes a boundary between calls.

Use a single call to date:

Code:
eval "$( date '+ YEAR=%Y MONTH=%m DAY=%d HOUR=%H MINUTE=%M' )"

Quote:
Code:
LASTHOUR=$((HOUR-1))

Code:
LASTHOUR=$(( ${HOUR#0} - 1 ))

Quote:
What i like to do is:

Code:
if $LASTHOUR < 9 then
function1
else 
function2

Code:
if [ ${LASTHOUR#0} -lt 9 ]
then
  function1
else
  function2
fi

# 5  
Old 02-26-2009
MySQL

Quote:
Originally Posted by cfajohnson

Not only are multiple calls to date unnecessary (and slow), but the script will fail if the time passes a boundary between calls.

Use a single call to date:

Code:
eval "$( date '+ YEAR=%Y MONTH=%m DAY=%d HOUR=%H MINUTE=%M' )"

Code:
LASTHOUR=$(( ${HOUR#0} - 1 ))

Code:
if [ ${LASTHOUR#0} -lt 9 ]
then
  function1
else
  function2
fi

Thanks. That was realy usefull information Smilie

Kind Regards
Mehrdad Smilie
Login or Register to Ask a Question

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