Pass the first date and last date of previous month

Hi All,

   I need to run a job every month at the beginning of the month which is scheduled through autosys, lets say on 03/01/2010. I need to pass the last month's i.e February's first_date = 02/01/2010 and last_date = 02/28/2010 as variables to a stored procedure. Can somebody please pass me a shell script that calculates the dates depending on the month.

Hi, vigdmab:

This works here and should be portable to posix-compliant systems.

$ ./vigdmab.sh 
02/01/2010 02/28/2010

$ cat vigdmab.sh
#!/bin/sh

month_year=$(date +'%m %Y' | awk '!--$1{$1=12;$2--}1')
m=${month_year% *}
y=${month_year##* }
d=$(cal $m $y | paste -s - | awk '{print $NF}')
first_date=$(printf '%02d/01/%s' $m $y)
last_date=$(printf '%02d/%s/%s' $m $d $y)
echo $first_date $last_date

Obviously, change the echo command at the end to whatever you need to execute.

Regards,
Alister

Hi Alister,

       Thanks a ton, the code worked.