PDA

View Full Version : Crazy Time Expr Issue



Nevets_Anderson
19th September 2005, 05:42 PM
Ok this is sort of crazy but anyway... I wanted to build an am / pm time command type thing that
out put the time as a 12 hour clock as opposed to something like the date command that pumps out the 24 hour clock. ( I hate having to think or - 12 ok! I'm lazy) so I wrote this. Issue is that the use of the expr pumps out 0 or 1 depending on the result.

Like this
_____
0
1
time is 5 41 PM
______

where upon I just want this to be the out put

time is 5 41 PM

Anyone know how to suppress this?

Code included for your edification




#!/bin/sh
# hunk gets the date and cuts out the hour only
hunk=`date | cut -d ' ' -f 4 | cut -d : -f 1`
# mins gets the date and cuts out the minutes
mins=`date | cut -d ' ' -f 4 | cut -d : -f 2`

# evaluates wther its AM if true if am spits out hour and minute
hunk2=`expr $hunk - 12`

if expr $hunk \<= '12'
then
echo time is $hunk $mins AM
fi
# evaluates wther its PM if true if am spits out hour and minute
if expr $hunk \>= '12'
then
echo time is $hunk2 $mins PM
fi


Thanks

Nevets

dangelovich
19th September 2005, 07:11 PM
The [] are usually required in an IF statement... The spaces between the [ and the conditions are required too.
I've also added the -eq 1 to determine if the return value is true, rather than leaving it up to the IF to interpret, since both IFs were printing out the time on my machine...



#!/bin/sh
# hunk gets the date and cuts out the hour only
hunk=`date | cut -d ' ' -f 4 | cut -d : -f 1`
# mins gets the date and cuts out the minutes
mins=`date | cut -d ' ' -f 4 | cut -d : -f 2`

# evaluates wther its AM if true if am spits out hour and minute
hunk2=`expr $hunk - 12`

if [ `expr &#036;hunk &#092;<= &#39;12&#39;` -eq 1 ]
then
echo time is &#036;hunk &#036;mins AM
fi
# evaluates wther its PM if true if am spits out hour and minute
if [ `expr &#036;hunk &#092;>= &#39;12&#39;` -eq 1 ]
then
echo time is &#036;hunk2 &#036;mins PM
fi

dangelovich
19th September 2005, 07:15 PM
After spending the last 10 minutes on that...

date +%r

returns

07:19:54 PM

Nevets_Anderson
19th September 2005, 08:59 PM
Dude&#33;

Thank you&#33;


I new there would be a simple way round this &#33;

Thanks again&#33;

Nevets

Squozen
19th September 2005, 10:55 PM
man date

also:

man strftime (mentioned in the date manpage)