*BSD News Article 34826


Return to BSD News archive

Xref: sserve comp.unix.bsd:14672 comp.unix.misc:13605 comp.unix.programmer:19464 comp.unix.questions:54037
Newsgroups: comp.unix.bsd,comp.unix.misc,comp.unix.programmer,comp.unix.questions
Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!bunyip.cc.uq.oz.au!munnari.oz.au!news.Hawaii.Edu!ames!hookup!swrinde!howland.reston.ans.net!darwin.sura.net!news.Vanderbilt.Edu!drl
From: drl@vuse.vanderbilt.edu (David R. Linn)
Subject: Re: Q:  How can I get yesterday's date?
Message-ID: <1994Aug16.144600.17237@news.vanderbilt.edu>
Sender: news@news.vanderbilt.edu
Nntp-Posting-Host: jester.vuse.vanderbilt.edu
Organization: Vanderbilt University School of Engineering, Nashville, TN, USA
References: <Pine.3.89.9408160216.A15886-0100000@worf.uwsp.edu> <32q00b$9vn@daphne.ecmwf.co.uk>
Date: Tue, 16 Aug 1994 14:46:00 GMT
Lines: 116

In article <32q00b$9vn@daphne.ecmwf.co.uk>,
Mike Connally <cd5@fulla.ecmwf.co.uk> wrote:
>In article <Pine.3.89.9408160216.A15886-0100000@worf.uwsp.edu>,
> pzuge@worf.uwsp.edu (Peter Zuge) writes:
>|> I am using BSD/386 unix:
>|> 
>|> I need to create a script that will run at a certain time everyday 
>|> "getting" ( via ftp ) a file with yesterday's date.
>
>No help to you, I'm afraid, but for designers of future
>shells, this is yet another thing which should be addressed.
>For example, this is how easy it is to do in the NOS/VE
>command language, SCL:
...
>  Mike Connally, Systems Consultant | internet:  Mike.Connally@cdl.cdc.com 

It's not that hard in a UNIX command langauge either:

#!/bin/sh
# yesterday: a program to find yesterday as a string or mail
# file name
#
# this program will not work correctly on 2000-03-01 - 400yr
#   correction will not fire because the YR is already modulo 100
#
TO_DAY=`date '+%d'`
TO_MTH=`date '+%m'`
TO_YR=`date '+%y'`


YEST_DAY=`expr $TO_DAY - 1`
YEST_MTH=`expr $TO_MTH - 0`
YEST_YR=`expr $TO_YR - 0`

if [ $YEST_DAY -eq 0 ] ; then

    case $TO_MTH in

	0[124689]|11)
	    YEST_DAY=31
	    ;;

	0[57]|1[02])
	    YEST_DAY=30
	    ;;

	03)	# I hate February
	    if [ `expr $TO_YR % 4` -eq 0 ] ; then
		if [ `expr $TO_YR % 100` -eq 0 ] ; then
# this stuff will never be exercised - the YR is modulo 100 already
		    if [ `expr $TO_YR % 400` -eq 0 ] ; then
			YEST_DAY=29
		    else
			YEST_DAY=28
		    fi
		else
			YEST_DAY=29
		fi
	    else
		YEST_DAY=28
	    fi
	    ;;

	*)	#this should not happen
	    echo bad TO_DAY value
	    exit 1
	    ;;
    esac

    YEST_MTH=`expr $YEST_MTH - 1`
    if [ $YEST_MTH -eq 0 ] ; then
	YEST_MTH=12
	if [ $YEST_YR -eq 0 ] ; then
	    YEST_YR=99
	else
	    YEST_YR=`expr $YEST_YR - 1`
	fi
    fi
fi

if [ $YEST_DAY -lt 10 ] ; then
    YEST_DAY="0$YEST_DAY"
fi

if [ $YEST_MTH -lt 10 ] ; then
    YEST_MTH="0$YEST_MTH"
fi

if [ $YEST_YR -lt 10 ] ; then
    YEST_YR="0$YEST_YR"
fi

if [ $# -gt 0 ] ; then
    case $1 in
	unread-list-mail)
# this is terribly dependent on my current mail setup
	    echo ${HOME}/.email/${YEST_YR}${YEST_MTH}01/zzUnread/${YEST_DAY}
	    ;;

	*)
	    echo $0: unknown argument - $1
	    exit 2
	    ;;
    esac
else
    echo ${YEST_YR}${YEST_MTH}${YEST_DAY}
fi
exit 0




-- 
David R. Linn, System Manager/Postmaster| INET: drl@vuse.vanderbilt.edu
Disclaimer: I speak only for myself	| Phone: [+1] 615-343-6164
     dee are ell at vee you ess ee dot van der bilt dot ee dee you