![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| AIX AIX is IBM's industry-leading UNIX operating system that meets the demands of applications that businesses rely upon in today's marketplace. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Getting yesterday DATE | osymad | Shell Programming and Scripting | 19 | 09-23-2009 02:39 PM |
| yesterday date month/date | skully | Shell Programming and Scripting | 5 | 06-24-2008 05:51 AM |
| yesterday | Tlg13team | Shell Programming and Scripting | 0 | 02-27-2008 04:02 AM |
| How to show yesterday date | wind_n_cloud | Shell Programming and Scripting | 1 | 02-16-2005 10:51 PM |
| get yesterday date in yyyymmdd format | hk_newbie | UNIX for Dummies Questions & Answers | 2 | 12-14-2001 03:32 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
I got sick of trying to script yesterday's date into so many different formats that I finally wrote and stole portions of this script to do it for me. I just pass in any arguments I want and out comes yesterdays date. I named the script "yesterday" and put it in /usr/local/bin on all my boxes:
Code:
FORMAT=${1:-YYYYMMDD}
if [ "$FORMAT" = "-help" ] || [ "$FORMAT" = "-h" ]; then
echo "Usage: Arguments are optional and can be given in any order"
echo " yesterday [YY|yy|YYYY|yyyy|MM|mm|MMM|mmm|DD|dd]"
exit 0
fi
typeset -Z2 YESTERDAY
typeset -Z2 MONTH
YESTERDAY=$((`date +%d` -1))
MONTH=`date +%m`
YEAR=`date +%Y`
if [ "$YESTERDAY" -eq "0" ]; then
MONTH=$((MONTH-1))
if [ $MONTH -eq "0" ]; then
MONTH=12
YEAR=$((YEAR-1))
fi
set `cal $MONTH $YEAR`
shift $(($# - 1))
YESTERDAY=$1
fi
ABBR_MONTH=$(echo $MONTH | sed \
-e "s/01/Jan/" \
-e "s/02/Feb/" \
-e "s/03/Mar/" \
-e "s/04/Apr/" \
-e "s/05/May/" \
-e "s/06/Jun/" \
-e "s/07/Jul/" \
-e "s/08/Aug/" \
-e "s/09/Sep/" \
-e "s/10/Oct/" \
-e "s/11/Nov/" \
-e "s/12/Dec/")
## Print the result
echo "${FORMAT}" | sed \
-e "s/[Mm][Mm][Mm]/${ABBR_MONTH}/" \
-e "s/[Yy][Yy][Yy][Yy]/${YEAR}/" \
-e "s/[Yy][Yy]/$(echo ${YEAR}|cut -c3-4)/" \
-e "s/[Mm][Mm]/${MONTH}/" \
-e "s/[Dd][Dd]/${YESTERDAY}/"
Code:
hostname:/:$ date Tue Mar 31 16:28:15 CDT 2009 hostname:/:$ yesterday yyyymmdd 20090330 hostname:/:$ yesterday yyyy/mm/dd 2009/03/30 hostname:/:$ yesterday MM/DD/YYYY 03/30/2009 hostname:/:$ yesterday MM-DD-YYYY 03-30-2009 hostname:/:$ yesterday "MM dd YYYY" 03 30 2009 hostname:/:$ |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|