The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 02-25-2008
sirrtuan sirrtuan is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 18
Help, I need to get the last date of previous month

Hi, I'm new with Unix, I'm trying to get a last day of previous month with this format: %b %d %Y (example: Feb 25 2008).
Here is what I have so far.

#!/bin/ksh
cur_month=`date +%m`
cur_year=`date +%Y`
prev_month=$(($cur_month-1))

# Check to see if this is January
if [ $prev_month -lt 1 ]
then
prev_year=$(($cur_year-1))
prev_month=12
LastDayOfMonth=`/bin/cal $prev_month $prev_year | grep -v "^$" | tail -1 | awk '{print $NF}'`
else
LastDayOfMonth=`/bin/cal $prev_month $cur_year | grep -v "^$" | tail -1 | awk '{print $NF}'`
fi
==============================================
I can get the last date of the previous month but I don't know how to put it back into the format that I wanted. Please help.

Thank you.