Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> RE: Unix shell Question

RE: Unix shell Question

From: Bellows, Bambi <BBellows_at_usg.com>
Date: Thu, 28 Feb 2002 14:31:30 -0800
Message-ID: <F001.0041BF1B.20020228143130@fatcity.com>


Date and string functions are not terribly easy in Unix, and generally involve parsing cal. So, if you want to do sysdate-1, what you would do is this:  

DAY=`date +%d`
MONTH=`date +%m`
YEAR=`date +%C%y`
echo $DAY $MONTH $YEAR
if [ $DAY = 1 ] ;
then

    if [ $MONTH = 1 ] ;
    then

        CAL_MONTH=12
        CAL_YEAR=`expr $YEAR - 1`
    else
        CAL_MONTH=`expr $MONTH - 1`
        CAL_YEAR=$YEAR

    fi
    CAL_DAY=`cal $CAL_MONTH $CAL_YEAR|tail -2|head -1|awk '{print $NF}'` else
    CAL_DAY=`expr $DAY - 1`
    CAL_MONTH=$MONTH
    CAL_YEAR=$YEAR

fi
echo "Yesterday was $CAL_MONTH $CAL_DAY $CAL_YEAR"  

or, you could do this:  

sqlplus << EOF
scott/tiger
spool date
select sysdate-1 from dual;
exit
EOF
grep -v SQL date.lst|tail -2|head -1    

Whichever.  

HTH,
Bambi.  

-----Original Message-----
Sent: Thursday, February 28, 2002 3:38 PM To: Multiple recipients of list ORACLE-L  

Hi,
Need help in Unix shell.
Is there a way to compute the previous date (similar to sysdate - 1 in SQL) using the date expression??
So if my date is 3/1/2002 I will get 2/28/20002.  

TIA
Yuval.

--

Please see the official ORACLE-L FAQ: http://www.orafaq.com
--

Author: Bellows, Bambi
  INET: BBellows_at_usg.com

Fat City Network Services    -- (858) 538-5051  FAX: (858) 538-5051
San Diego, California        -- Public Internet access / Mailing Lists
--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing). Received on Thu Feb 28 2002 - 16:31:30 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US