Problem - script that parses $date data...

This is probably archaic, but I'm new to unix and this is my first shell script. I'm writing this script to use in another script. All I am trying to do is make the parts of the output from date usable in my other script. I was wondering if you could stand looking at this and see if you notice why I can't get my last line to execute.

#! /bin/sh
wday=`date | head -c +3`
echo "$wday"
cyear=`date | tail -c -5`
echo "$cyear"
ahour=`date | head -c +13 | tail -c +12 -`
if [$ahour - 12 -ge 0]
then
thour=`expr $ahour - 12`
x=`PM`
else
thour=`12 - $ahour`
x=`AM`
fi

if [$wday == Mon]
then
wday= 'Monday'
else
if [$wday == Tue]
then
wday= 'Tuesday'
else
if [$wday == Wed]
then
wday= 'Wednesday'
else
if [$wday == Thu]
then
wday= 'Thursday'
else
if [$wday == Fri]
then
wday= 'Friday'
else
if [$wday == Sat]
then
wday= 'Saturday'
else
if [$wday == Sun]
then
wday= 'Sunday'
fi
echo "$wday"
echo "The time is $thour $x."
exit 0

this date command will display the desired o/p

date +'%A %I:%M:%S %p'

Saturday 01:53:49 PM

Thank you, that works very well. But I can't just grab the day of the week out as a variable. I also can't grab the year. I am trying to use my script to make the data available. Some of the commands (as the one you made so simple) are just there for troubleshooting.

Can you help me make my script work?

you can grab them in any variable you want...

date +'%A %I:%M:%S %p %Y'|read wday thour x cyear
echo $wday $thour $x $cyear

Hmm... I tried that, and when I put in $echo $wday, I get 'Sat'. For the other variables I get nothing...?

I then broke up the first bit you gave me, date +'%A' | read wday and this still gives me just Sat. How do I get it to display Saturday?

---------- Post updated at 03:59 AM ---------- Previous update was at 03:59 AM ----------

Hmm... I tried that, and when I put in $echo $wday, I get 'Sat'. For the other variables I get nothing...?

I then broke up the first bit you gave me, date +'%A' | read wday and this still gives me just Sat. How do I get it to display Saturday?

Hi.

The solution from vidyadhar85 works fine in KSH.

A bash solution, using a here string:

read wday thour x cyear <<< $(date +'%A %I:%M:%S %p %Y')

This also works in newer Korn shells.

As for %A, from the date man page:

Thanks, that works perfectly. Care to explain how that statement works? Can you use that sort of statement to parse any data or just the date statement?

From the bash man page:

The ksh man page puts it slightly differently:

You can use it with most things when you have to pass something to the standard input of, for example your read statement, or another program or a script, etc.

A solution for any Bourne-type shell:

read wday thour x cyear <<.
`date +'%A %I:%M:%S %p %Y'`
.