Computing.Net > Forums > Unix > Script using cal

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Script using cal

Reply to Message Icon

Name: Cnot_Man
Date: October 19, 2005 at 21:31:20 Pacific
OS: UNIX
CPU/Ram: P4/512
Comment:

I was wondering if there was an easy way to use cal with maybe either awk or sed to easily give the user the day of the week for the given day they entered (assuming the date was valid).

Is that possible, or do I have to sit there and do all kinds of calculations to get the correct day?

Thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: October 20, 2005 at 06:09:50 Pacific
Reply:

I found a script I wrote (datestamped Apr 2002), that uses cal and awk to determine DOW.

# !/bin/ksh

mmddyy=`echo $1 | tr "[/]" "[ ]"`
echo $mmddyy | read mo day yr junk
day=${day#0}

cal $mo $yr | awk 'BEGIN {
 day="'$day'"
 split("Su Mo Tu We Th Fr Sa Su",DOW)
 getline
 getline}
{if (col=index($0,day))
    {dow=int((col+2)/3)
     print "DOW=" DOW[dow
     print "DOW=" DOW[dow]
     exit}
}'

exit 0

./dow.sh 03/23/1947
DOW=Su


0

Response Number 2
Name: Jim Boothe
Date: October 20, 2005 at 06:14:02 Pacific
Reply:

I had a little posting error. Here is the proper code:

# !/bin/ksh

mmddyy=`echo $1 | tr "[/]" "[ ]"`
echo $mmddyy | read mo day yr junk
day=${day#0}

cal $mo $yr | awk 'BEGIN {
 day="'$day'"
 split("Su Mo Tu We Th Fr Sa Su",DOW)
 getline
 getline}
{if (col=index($0,day))
    {dow=int((col+2)/3)
     print "DOW=" DOW[dow]
     exit}
}'

exit 0

./datedow.sh 03/23/1947
DOW=Su


0

Response Number 3
Name: Cnot_Man
Date: October 20, 2005 at 14:06:15 Pacific
Reply:

thanks for the post,

quick question though, I am new to UNIX in general, and only know how to use bash. It looks as though that is for ksh. If it is not too much trouble, do you know what it would look like in bash. Thanks again.


0

Response Number 4
Name: Jim Boothe
Date: October 20, 2005 at 14:22:26 Pacific
Reply:

On some linux servers, ksh is an alias for bash, so try it just like it is first. If it does not like the ksh, change ksh to bash and try that.


0

Response Number 5
Name: Cnot_Man
Date: October 20, 2005 at 16:09:23 Pacific
Reply:

I tried it just as is, and with changing it to bash instead of ksh, and whenever it is run, it just goes back to the prompt, no result, nothing displayed, not even an error. Also, I am using sun solaris unix, not linux. Any ideas...?

Thanks.


0

Related Posts

See More



Response Number 6
Name: Cnot_Man
Date: October 20, 2005 at 16:15:35 Pacific
Reply:

If I use sh datedow.sh 03/23/1947, then i get the error:
"datedow.sh: bad substitution"
and like I said, if i use just datedow.sh 03/23/1947 I get nothing.



0

Response Number 7
Name: nails
Date: October 20, 2005 at 23:01:23 Pacific
Reply:

This is a shell implementation of Mike Keith's date algorithm:

#!/bin/bash

# This function returns the day of week where
# 0 = sunday, 1 = monday, ... 6 = saturday
# This algorithm is from Mike Keith's World of Words & Numbers:
# http://users.aol.com/s6sj7gt/mikecal.htm
# arguments: $1 = month, $2 = day, $3 = year in format YYYY
get_dow()
{
# [.] means to truncate downward to the nearest integer
# dayofweek=([23m/9] + d + 4 + y + [z/4] - [z/100] + [z/400] - 2 (if m >= 3)) mod 7
typeset -i DOW
typeset -i z
typeset -i multpr

if [ $1 -lt 3 ]
then # year determination
z=$(($3-1))
else
z=$3
fi
if [ $1 -ge 3 ]
then # set the multiplier
multpr=2
else
multpr=0
fi
DOW=$(( ((23 * $1/9) + $2 + 4 + $3 + ($z/4) - ($z/100) + ($z/400) - $multpr) % 7))

echo $DOW
}

# return a day string
# sunday, monday, .. saturday
get_day_string()
{
case $1 in
0) echo "sunday";;
1) echo "monday";;
2) echo "tuesday";;
3) echo "wednesday";;
4) echo "thursday";;
5) echo "friday";;
6) echo "saturday";;
*) echo "error";;
esac
}

mydate="03/27/1947"
set - $(IFS='/'; echo $mydate)
TY=$3
TM=$1
TD=$2

day_str=$(get_day_string $(get_dow $TM $TD $TY) )
echo $day_str # should echo thursday


0

Response Number 8
Name: Jim Boothe
Date: October 21, 2005 at 06:56:12 Pacific
Reply:

The problem is going to be with the day= command.  I have to get any leading zero removed from the day portion of the date argument so that it will be an exact match against the day in the cal output which does not have leading zeros.

In the code below, I now use an expr command to strip any leading zeros, so I think that should work for you.  For testing only, I left the old command in also (as dayx), and I display values for dayx and day just to confirm what substitutions are being made.  You can remove both of the lines that contain dayx.

mmddyy=`echo $1 | tr "[/]" "[ ]"`
echo $mmddyy | read mo day yr junk
dayx=${day#0}
day=`expr $day : "0*\(.*\)"`
echo "TEST: dayx=$dayx  day=$day"

cal $mo $yr | awk 'BEGIN {
 day="'$day'"
 split("Su Mo Tu We Th Fr Sa Su",DOW)
 getline
 getline}
{if (col=index($0,day))
    {dow=int((col+2)/3)
     print "DOW=" DOW[dow]
     exit}
}'


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Script using cal

checking date in Shell script www.computing.net/answers/unix/checking-date-in-shell-script/5035.html

Unix script using Korn Shell www.computing.net/answers/unix/unix-script-using-korn-shell/7393.html

How 2 find java script used in UNIX www.computing.net/answers/unix/how-2-find-java-script-used-in-unix/8124.html