Computing.Net > Forums > Unix > checking date in Shell script

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.

checking date in Shell script

Reply to Message Icon

Name: ray
Date: May 16, 2003 at 09:41:24 Pacific
OS: SunOS
CPU/Ram: na
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: May 16, 2003 at 13:23:11 Pacific
Reply:

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 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
}

# 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


0

Response Number 2
Name: nails
Date: May 16, 2003 at 13:24:29 Pacific
Reply:

Sorry, that should be "DON'T" do date related stuff well.

Nails


0

Response Number 3
Name: Ray
Date: May 19, 2003 at 04:01:41 Pacific
Reply:

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 31

If 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


0

Response Number 4
Name: Ray
Date: May 19, 2003 at 05:47:52 Pacific
Reply:

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=$' unexpected

Many thanks


0

Response Number 5
Name: nails
Date: May 19, 2003 at 08:15:07 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Ray
Date: May 19, 2003 at 08:57:38 Pacific
Reply:

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



0

Response Number 7
Name: nails
Date: May 19, 2003 at 09:28:33 Pacific
Reply:

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 7

Are 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


0

Response Number 8
Name: LANkrypt0
Date: May 19, 2003 at 13:31:13 Pacific
Reply:

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!"
else

x=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)
done

case $x in
1)
actday="Sunday"
;;
2)
actday="Monday"
;;
3)
actday="Tuesday"
;;
4)
actday="Wednesday"
;;
5)
actday="Thursday"
;;
6)
actday="Friday"
;;
7)
actday="Saturday"
;;
esac

echo "$1/$2/$3 was/will be a $actday!"
fi


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: checking date in Shell script

loops in shell scripts!!! www.computing.net/answers/unix/loops-in-shell-scripts/5140.html

Using hex-code in shell script www.computing.net/answers/unix/using-hexcode-in-shell-script/4227.html

Array size in shell script? www.computing.net/answers/unix/array-size-in-shell-script/5761.html