Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...The enviroment is simple, natural and efficient. The members are competent, educated and professionals..."

Geography

Where in the world do Tek-Tips members come from?

Shell Scripts

How to calculate yesterday's date in a Shell script?
Posted: 25 Jul 02 (Edited 25 Jul 02)


# File-name: yesterday.sh
#-----------------------------------------------
# Returns date 1 day ago from the specified date
# Current date is taken if no date is specified
#-----------------------------------------------
# Input:        Default:
#   $1 - dd             Current day
#   $2 - mm             Current month
#   $3 - yyyy           Current year
#-----------------------------------------------

#This is how a function is defined in a
#UNIX shell scripts

get_one_day_before_specified_date()
{
#get the command line input(date month & year)
    day=$1
    month=$2
    year=$3
 
    # if it is the first day of the month
    if [ $day -eq 01 ]
    then
        # if it is the first month of the year
        if [ $month -eq 01 ]
        then
            # make the month as 12
            month=12
 
            # deduct the year by one
            year=`expr $year - 1`
        else
            # deduct the month by one
            month=`expr $month - 1`
        fi
        
        # use cal command, discard blank lines,
        # take last field of last line,  
        # first awk  command is used to get the
        # last useful line of the calendar cmd,
        # second awk command is used to get the  
        # last field of this last useful line,
        # NF is no. of fields,
        # $NF is value of last field

 day=`cal $month $year | awk 'NF != 0{ last = $0 }; END{ print last }' |  awk '{ print $NF }'`
    else
        # deduct the day by one
        day=`expr $day - 1`
    fi

    echo $day $month $year
}

#!/bin/ksh
if [ $# -ne 3 ]
then
    d=`date +%d`
    m=`date +%m`
    y=`date +%Y`
else
    d=$1
    m=$2
    y=$3
fi    

#Cmd line arguments are captured in a shell script
#through $1 $2 $3, ......., $9,${10} (not $10)

# This is how we call unix user-defined functions,
# notice it is not junk123( $1, $2, $3 ) format
get_one_day_before_specified_date $d $m $y

Back to -General UNIX discussion FAQ Index
Back to -General UNIX discussion Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close