Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KSH Function

Status
Not open for further replies.

santhas

Technical User
Aug 27, 2003
35
AU
Hi Scripting Gurus,

I got this script from internet. Script is fine when I run as a stand alone. I need to embed it into my script as a function to get yesterday/day before yesterday and so on... But it fails when called as function.
*********************************************************
#! /bin/ksh
# Get yesterday's date in YYYY-MM-DD format.
# With argument N in range 1..28 gets date N days before.
OFFSET=${1:-1}

case $OFFSET in
*[!0-9]* | ???* | 3? | 29) print -u2 "Invalid input" ; exit 1;;
esac

eval `date "+day=%d; month=%m; year=%Y`
typeset -Z2 day month
typeset -Z4 year

# Subtract offset from day, if it goes below one use 'cal'
# to determine the number of days in the previous month.
day=$((day - OFFSET))
if (( day <= 0 )) ;then
month=$((month - 1))

if (( month == 0 )) ;then
year=$((year - 1))
month=12
fi

set -A days `cal $month $year`
xday=${days[$(( ${#days[*]}-1 ))]}
day=$((xday + day))
fi
print $month/$day/$year
***********************************************
Stand alone:
bash-3.00# ./yesterday_date.ksh 2
11/05/2007

Inside function:
yesterday()
{
......Above script lines.....
}
}
yesterday 2

bash-3.00# ./yesterday_date.ksh
./yesterday_date.ksh[14]: day - OFFSET: bad number

Could someone shed some light here?

Thanks in advance
 
As you use bash I assume you have the GNU date date command and thus:
date --date '2 days ago'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Sorry, I am not using Gunu date. It's Solaris.
 
PHV is too modest to mention his excellent faq80-4800 which covers this.

Ceci n'est pas une signature
Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top