Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I list all days in a month (in KSH) ?

Status
Not open for further replies.

Tison

Programmer
May 12, 1999
216
CH
Hi guru's<br>
<br>
I am in need of a Korn script that will write out all the days in a month.<br>
ie. If I run it on any month and supply a paramater of month name eg March, it must write out ;<br>
1 March 2000,2 March 2000,3 March 2000....31 March 2000<br>
(cater for 28 and 30 and 31 day months).<br>
<br>
I want to use it to log certain things against the date.<br>
<br>
Thanks<br>
Steve
 
Hmmm... this is definitely possible, and relatively simple to accomplish. However, you say you want to use it to log certain things against the current date. Why not just use numerics such as &quot;dd/mm/yyyy&quot; or &quot;mm/dd/yyyy&quot;? This way, when you come back in 6 months time to extract data from the log file by date, you don't have to add code into your program to convert &quot;24 March 2000&quot; back to numerical format (&quot;24/03/2000&quot;, for eg) when doing any ordering or date comparisons.<br>
<br>
If you positively *have* to use names for the month, you need to use some formatting characters for the &quot;date&quot; command. Full detail are available in the man pages, but to get what you need you can run the following:<br>
<FONT FACE=monospace><br>
date +&quot;%d %B %Y&quot;<br>
</font><br>
As easy as that! :) However, I'd still recommend using a numerical format such as:<br>
<FONT FACE=monospace><br>
date +&quot;%d/%m/%Y&quot;<br>
</font><br>
This will make it much easier to parse at a later date.<br>
<br>
HTH
 
Have a look at the 'cal' command<br>
<br>
&gt;cal 5 2000<br>
May 2000<br>
Sun Mon Tue Wed Thu Fri Sat<br>
1 2 3 4 5 6<br>
7 8 9 10 11 12 13<br>
14 15 16 17 18 19 20<br>
21 22 23 24 25 26 27<br>
28 29 30 31<br>
<br>
You could, using ksh, awk or perl - or something - to chop up the output of the cal command wihout too much trouble.<br>
<br>
Does this help? If you need some assistance chopping it up just shout.<br>
<br>
Mike<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Doh! I suffered from a dose of self inflicted forgetting what the post was all about! Sorry, Steve!<br>
<br>
Mike's right, though, you can use <FONT FACE=monospace>cal</font> to list the days in a month as he suggests.
 
Steve,<br>
<br>
If you need some more help, writing a perl/ksh script whatever, just shout.<br>
<br>
Mike <p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
OK, so its way past when you needed it, but I've only just read the message!

Hopefully the following is useful to someone ;-)

It will print out days in month, and if you dont put a month in it will default to the current month. It's not brilliant, but it might be the foundation on which to build. It will also take in to accoutn leap years.

Cheers.
Dave Vickers.
----------------START OF SCRIPT ------------------


#!/usr/bin/ksh

# File: displayMonth
# Purpose: show dates in month

# Set Variables
MAX_DAYS=31
YEAR=`date +%Y`
MONTH_TO_CHECK=`date +%m`
DAY=1

read MONTH?&quot;Enter month (Jan-Dec): &quot;

case $MONTH in
&quot;Jan&quot; ) MONTH_TO_CHECK=&quot;01&quot;;;
&quot;Feb&quot; ) MONTH_TO_CHECK=&quot;02&quot;;;
&quot;Mar&quot; ) MONTH_TO_CHECK=&quot;03&quot;;;
&quot;Apr&quot; ) MONTH_TO_CHECK=&quot;04&quot;;;
&quot;May&quot; ) MONTH_TO_CHECK=&quot;05&quot;;;
&quot;Jun&quot; ) MONTH_TO_CHECK=&quot;06&quot;;;
&quot;Jul&quot; ) MONTH_TO_CHECK=&quot;07&quot;;;
&quot;Aug&quot; ) MONTH_TO_CHECK=&quot;08&quot;;;
&quot;Sep&quot; ) MONTH_TO_CHECK=&quot;09&quot;;;
&quot;Oct&quot; ) MONTH_TO_CHECK=&quot;10&quot;;;
&quot;Nov&quot; ) MONTH_TO_CHECK=&quot;11&quot;;;
&quot;Dec&quot; ) MONTH_TO_CHECK=&quot;12&quot;;;
* ) MONTH=`date +%b`;;
esac

case $MONTH_TO_CHECK in
02 ) {
if [ $(( $YEAR % 4 )) -eq 0 ]
then
MAX_DAYS=29
else
MAX_DAYS=28
fi
};;
04 ) MAX_DAYS=30;;
06 ) MAX_DAYS=30;;
09 ) MAX_DAYS=30;;
11 ) MAX_DAYS=30;;
esac

until [ $DAY -gt $MAX_DAYS ]
do
print &quot;$DAY $MONTH $YEAR&quot;
DAY=$(( $DAY + 1 ))
done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top