Computing.Net > Forums > Unix > shell script

shell script

Reply to Message Icon

Original Message
Name: Devaraj (by Fidy)
Date: February 6, 2006 at 08:16:11 Pacific
Subject: shell script
OS: sun solaris
CPU/Ram: 1280mb
Comment:

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


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: February 6, 2006 at 09:15:48 Pacific
Reply: (edit)

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 script

The last two lines of cal are piped to awk and awk prints the last field of the 1st line and quits.


Report Offensive Follow Up For Removal

Response Number 2
Name: Devaraj (by Fidy)
Date: February 6, 2006 at 09:21:14 Pacific
Reply: (edit)

Thanks Nail..
I would like the output to be printed in the format mmddyyyy..Can we achieve this from the awk command? Thanks again

eg.. 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


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: February 6, 2006 at 11:05:37 Pacific
Reply: (edit)

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 month

echo "enter a year"
read year

begdate=$(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")



Report Offensive Follow Up For Removal

Response Number 4
Name: Devaraj (by Fidy)
Date: February 6, 2006 at 11:30:25 Pacific
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 5
Name: nails
Date: February 6, 2006 at 14:47:06 Pacific
Reply: (edit)

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 SP

Here'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_OTF

engine_up=$(onstat - | grep "Up" | wc -l)
if [ $engine_up -eq 0 ]
then
echo "Informix Engine must be Up to execute SPs."
exit
fi

dt1="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.



Report Offensive Follow Up For Removal


Response Number 6
Name: nails
Date: February 6, 2006 at 14:57:56 Pacific
Reply: (edit)

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_OTF

engine_up=$(onstat - | grep "Up" | wc -l)
if [ $engine_up -eq 0 ]
then
echo "Informix Engine must be Up to execute SPs."
exit
fi

dt1="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"


Report Offensive Follow Up For Removal

Response Number 7
Name: Devaraj (by Fidy)
Date: February 6, 2006 at 16:04:27 Pacific
Reply: (edit)

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


Report Offensive Follow Up For Removal

Response Number 8
Name: nails
Date: February 6, 2006 at 22:10:44 Pacific
Reply: (edit)


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

month=$(date '+%m')
day=$(date '+%d')
year=$(date '+%Y')

if [[ $day -eq 1 ]]
then
echo "execute for the 1st"
fi

ld=$(cal $month $year |tail -2|awk ' {
print $NF; exit
} ')

if [[ $ld -eq $day ]]
then
echo "execute for the last day"
fi

dayno=$(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



Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: shell script

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge