Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I need help for a shell script to have a user enter a date into the program.
When the date has been entered in format dd/mm/yyyy, i need the script to identify what day that date is... eg. if its a monday, tuesday, wednesday etc.
If it is a monday i need it to be passed onto a different script (e.g a script called monday) and like wise for the other days. However, if it is a Friday, Saturday or Sunday, then it needs to return an error saying "invalid date", and the option to enter another date.
i have looked up case staements but can not get it to work.
Can someone please help... the full prject contains a lot more... and i might need a little more help further down the line if anyone has a little spare time?
Many thanks
Ray

Hi:
I'll do the hard part for you. The standard unix tools do date related stuff well. (GNU date and awk might be able to. Haven't investigated them. Perl will certainly do this). Anyway, I coded Mike Keith's algorithm for determining a day of the week. Check out his web site listed in the function header for more info:
!/bin/ksh
# this function returns the day of week where
# $1 - month
# $2 - day
# $3 - year
# from Mike Keith's
# http://users.aol.com/s6sj7gt/mikecal.htm
# returns 0=Sun, 1=Mon, etc
get_dow ()
{
# [x] means to truncate downward to the nearest integer
# dayofweek = ( [23m/9] + d + 4 + y + [z/4] - [z/100] + [z/400] - 2 (if m >= 3) ) mo
d 7
typeset -i DOW
typeset -i z
typeset -i multprif [ $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
}# no error checking on the date
echo "enter a date in format MM/DD/YYYY"
read date# set $1,$2,$3 equal to the MM, DD, YYYY
set - $(echo $date|tr '/' ' ' )dow=$(get_dow $1 $2 $3)
case $dow in
1) echo "Mon.";;
2) echo "Tues.";;
3) echo "Wednes.";;
4) echo "Thurs.";;
0|5|6) echo "Fri. Sat. Sun.";;
*) echo "error";;
esac# Regards,
# Nails

Hi Nails...
Thanks for the reply.
Just wondering, is it not possible to utalise the Unix program "Cal". This gives day, date, month, and year....?
The Cal program will take the values in the format "mm yyyy", so the command would be "Cal mm yyyy" or in the exmaples case "Cal $1 $3". The program will output the full calender for that month with the day names written in a standard format as below.
December 1990
S M Tu W Th F S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31If after this output, if we could have the script match the date (dd or $2) against the calender, then we could determin the day of the week, if there is a method in unix to check columns?
I hope this makes sense.
Many thanks.... look forward to hearing your thoughts.
Regards
Ray

Nails,
A follow up, what do you mean by
Sorry, that should be "DON'T" do date related stuff well.Also the code that you have written above returns the following?
menu: syntax error at line 19: `z=$' unexpectedMany thanks

Ray:
First, in my cutting and pasting, I dropped the "#" sign off the shell invoccation:
!/bin/ksh
should be:
#!/bin/ksh
Sorry if it is.
Now, as far as using the "cal" command: Sure it's possbile, but I have no desire to do it.
A guy named Larry Reznick wrote an article "Which Day of the week is it"? back in July, 1993. You can see a copy of it at:
http://www.visi0n.net/sysadmin_mag/
Nails

Hi Nails,
Thanks for getting back to me...
I had already included the # at the start of the script.
However for some strange reason, i am being return the error:
menu: syntax error at line 19: `z=$' unexpected
Line 19 for me is line 22 on the script at written above as i have changed some of the comment tags.
Basically it is getting stuck on z=$(($3-1))
Many thanks...Ray

Ray:
get_dow ()
{
# [x] means to truncate downward to the nearest integer
# dayofweek = ( [23m/9] + d + 4 + y + [z/4] - [z/100] + [z/400] - 2 (if m >= 3) ) mo
d 7Are the comments causing a problem? This forum doesn't recognize the two lines starting with "#" as comments. The "d 7" is on a line all by itself which causes a syntax error.
Other than that, I don't know what else to tell you.
Nails

Just whipped up this lil script, uses cal and some sed. Format is mm dd yyyy.
#!/bin/ksh
if [[ -z $1 || -z $2 || -z $3 ]];then
echo "usage: dayofweek mm dd yyyy"
elif [[ $1 -gt 12 ]];then
echo "There are not that many months in a year!"
elif [[ $2 -gt `cal $1 $3 | tr -d "\n" | tr " " "\n" | tail -1` ]];then
echo "The day you specified exceeds the number of days in that month!"
elsex=0
week=$(cal $1 $3 | tail +3 | sed -e "s/ /XX /g" -e 's/$/ /g' -e 's/^/ /g' | grep " $2 ")until [[ $daycount -eq $2 ]];do
x=$(($x+1))
daycount=$(echo $week | tr " " "\n" | head -$x | tail -1)
donecase $x in
1)
actday="Sunday"
;;
2)
actday="Monday"
;;
3)
actday="Tuesday"
;;
4)
actday="Wednesday"
;;
5)
actday="Thursday"
;;
6)
actday="Friday"
;;
7)
actday="Saturday"
;;
esacecho "$1/$2/$3 was/will be a $actday!"
fi

![]() |
![]() |
![]() |

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