#!/bin/sh
# 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
lengthd=`echo "$day" | wc -c | cut -c1-8`
lengthd=`expr $lengthd - 1`
lengthm=`echo "$month" | wc -c | cut -c1-8`
lengthm=`expr $lengthm - 1`
if [ $lengthd -eq 1 ]
then
day="0"$day
fi
if [ $lengthm -eq 1 ]
then
month="0"$month
fi
echo $year-$month-$day
}
#!/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
# end of script
or you can try this command in other script
bash-2.05$ TZ=y17 date +%c %Y
result : Fri Jul 18 07:24:35 2008
VCURDATE=`TZ=y17 date +%c %Y`
tgl=`echo $VCURDATE | cut -c9-10`
bln=`echo $VCURDATE | cut -c5-7`
thn=`echo $VCURDATE | cut -c21-24`
No comments:
Post a Comment