Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
How can I get the 1'st date and the last date of any given month without having to use an array..preferrably using awk..
Thanks in advance.
Regards,
Jern

I'm not exactly certain what you want; the first is always the first. This stub using the unix cal command delivers the last date. This example uses Feb of 2006:
cal 2 2006 |tail -2|awk ' {
print $NF; exit
} '
# end scriptThe last two lines of cal are piped to awk and awk prints the last field of the 1st line and quits.

Thanks Nail..
I would like the output to be printed in the format mmddyyyy..Can we achieve this from the awk command? Thanks againeg.. 02282006-format.. I would like to pass this date in a stored procedure(say BETWEEN 02012006 AND 02282006)from a shell script..Do you have any other idea HOW TO PASS THIS FIRST AND LAST DATE FROM THE SCRIPT TO A STORED PROCEDURE? Thanks so much.
Regards,
Jern

In a shell script, how you call a stored procedure depends on the database you're using. You haven't told us what database you're using.
This stub ksh script does build 2 variables for the 1st and last dates of a month:
#!/bin/ksh
echo "enter a month"
read monthecho "enter a year"
read yearbegdate=$(printf "%02d01%s" $month $year)
enddate=$(cal $month $year |tail -2|awk ' {
printf("%02d%02d%s\n", v1, $NF, v2); exit
} ' v1="$month" v2="$year")

Thanks again..Nail
Basically, I wanted to pass the dates as a parameter to an infomix stored procedure.
the procedure will query the DB depending on the date..
for example - precedure ('start date', 'end date')also I would want the dates to be generated for a week..
Therefore this unix script will run every monthend/first day of next month and extract the start date/end date info from the system date.
And it also will run on every saturday/friday night and will extract the date depending on the sys date for the previous week.and pass this dates to the routine as the parameters...
Thanks a lot again..
Regards,
Jern

As it happens, I have experience calling Informix SPs from shell scripts. Here's an example:
Given this SP:
-- This function returns a datetime year to second from a date and a time
-- string. No error checking.
-- the time string is of the type: 12:21:16
-- Author: Nails
CREATE PROCEDURE ret_datetime(date1 DATE, time1 INTERVAL HOUR TO SECOND)
RETURNING DATETIME YEAR TO SECOND;
-- RETURNING CHAR(15);
DEFINE xchar CHAR(30);
DEFINE dt_ret DATETIME YEAR TO SECOND;create temp table b (
xdate1 date,
xtime1 interval hour to second
) with no log;insert into b values (date1, time1);
LET dt_ret = (SELECT EXTEND(xdate1, YEAR TO SECOND) + time1 FROM b);
drop table b;RETURN dt_ret;
END PROCEDURE;
-- End SPHere's a shell script, that sends arguments to the SP and even captures output. You either have to invoke the dbaccess or isql utility. I like to parameterize as much as I can:
!/bin/ksh
# database name
DBNAME="testdb"; export DBNAME# use dbaccess or isql
INTERFACE_CMD="dbaccess"; export INTERFACE_CMD# send to standard output
DBCOMMAND="$INTERFACE_CMD -e $DBNAME"; export DBCOMMAND# send errors and output to files and standard output
DBCOMMAND_OTF="eval $INTERFACE_CMD -e $DBNAME 2>> err.txt |tee /dev/tty >> output.txt";
export DBCOMMAND_OTFengine_up=$(onstat - | grep "Up" | wc -l)
if [ $engine_up -eq 0 ]
then
echo "Informix Engine must be Up to execute SPs."
exit
fidt1="02/06/2006"
time1="10:12:16"
# get the informix datetime using SP.
intermed=$(dbaccess -e ificsdc <<MSG
EXECUTE PROCEDURE ret_datetime("$dt1", "$time1")
MSG)# the last two fields of the expression returned from
# executing ret_datetime procedure are the date time
dt2=$(echo $intermed| awk ' { printf("%s %s", $(NF-1), $NF) } ')echo "datetime is $dt2"
I'll answer your date questions in another post.

It's been awhile. Let me repost the script as I forgot to parameterize the dbaccess call:
#!/bin/ksh
# database name
DBNAME="testdb"; export DBNAME# use dbaccess or isql
INTERFACE_CMD="dbaccess"; export INTERFACE_CMD# send to standard output
DBCOMMAND="$INTERFACE_CMD -e $DBNAME"; export DBCOMMAND# send errors and output to files and standard output
DBCOMMAND_OTF="eval $INTERFACE_CMD -e $DBNAME 2>> err.txt |tee /dev/tty >> output.txt";
export DBCOMMAND_OTFengine_up=$(onstat - | grep "Up" | wc -l)
if [ $engine_up -eq 0 ]
then
echo "Informix Engine must be Up to execute SPs."
exit
fidt1="02/06/2006"
time1="10:12:16"
# get the informix datetime using SP.
intermed=$($DBCOMMAND <<MSG
EXECUTE PROCEDURE ret_datetime("$dt1", "$time1")
MSG)# the last two fields of the expression returned from
# executing ret_datetime procedure are the date time
dt2=$(echo $intermed| tail -1|awk ' { printf("%s %s", $(NF-1), $NF) } ')
echo "datetime is $dt2"

Thank you very much Nail..it is very much appreciated..I will look into and and get back to you for any question...
Regards,
Jern

There are a number of ways to do what you want. The following stub determines the last day of the month, the first of the month and whether a given day is a friday or saturday. Running this code from cron once a day should do what you want. Let me know if you have any questions.
#!/bin/ksh# get_dow:
# 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
#!/bin/ksh# get_dow:
# 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 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
}month=$(date '+%m')
day=$(date '+%d')
year=$(date '+%Y')if [[ $day -eq 1 ]]
then
echo "execute for the 1st"
fild=$(cal $month $year |tail -2|awk ' {
print $NF; exit
} ')if [[ $ld -eq $day ]]
then
echo "execute for the last day"
fidayno=$(get_dow $month $day $year)
echo $dayno# friday
if [[ dayno -eq 5 ]]
then
echo "This is friday"
fi# saturday
if [[ dayno -eq 6 ]]
then
echo "This is saturday"
fi

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

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