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
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