Hi I am using the below script to calculate the yesterdays date
#! /bin/sh
set -x
YEAR=`date +"%Y"`
NMONTH=`date +"%m"`
DATE=`date +"%d"`
if [ $DATE -eq 01 ];then
case $NMONTH in
01|02|04|06|08|09|11)
DATE=31
if [[ $NMONTH == "01" || $NMONTH == "1" ]]; then
YEAR=`expr $YEAR - 1`
NMONTH=12;LMONTH="December"
else
NMONTH=`expr $NMONTH - 1`
fi;;
03) if [[ `expr $YEAR % 4` -eq 0 ]]; then
DATE=29
else
DATE=28
fi
NMONTH=`expr $NMONTH - 1`;;
*)
DATE=30
NMONTH=`expr $NMONTH - 1` ;;
esac
else
DATE=`expr $DATE - 1`
echo $DATE
fi
if [ ${#NMONTH} -eq 1 ]; then
NMONTH="0$NMONTH"
fi
if [ ${#DATE} -eq 1 ]; then
DATE="0$DATE"
fi
case $NMONTH in # set long month name
01) LMONTH="January";;
02) LMONTH="February";;
03) LMONTH="March";;
04) LMONTH="April";;
05) LMONTH="May";;
06) LMONTH="June";;
07) LMONTH="July";;
08) LMONTH="August";;
09) LMONTH="September";;
10) LMONTH="October";;
11) LMONTH="November";;
12) LMONTH="December";;
esac
echo $DATE-$NMONTH-$YEAR #Output date-month-year
the o/p that i am getting is
+ date +%Y
YEAR=2010
+ date +%m
NMONTH=07
+ date +%d
DATE=14
+ [ 14 -eq 01 ]
+ expr 14 - 1
DATE=13
+ echo 13
13
date bad substitution
[/CODE]
I am using a Sun Solaris 10 system
Pls advise
---------- Post updated at 06:49 AM ---------- Previous update was at 06:05 AM ----------
Hi all
Any idea why I am getting a date bad substitution error
Pls advise
Bumping up posts or double posting is not permitted in these forums.
Please read the rules , which you agreed to when you registered, if you have not already done so.
You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future
Thank You.
The UNIX and Linux Forums.
Hi ultimatix,
Instead of writing a shell script you may use the date command as follows:
date +%d-%m-%Y -d "1 day ago"
you can can replace 1 with any number .
Hi annesh
Thanks for the oneliner, but will account for leap years ( Feb ) and 30 / 31 day months ?
Yes, it will. Do you really have the GNU date on Solaris 10?
If the GNU date is not available, you could use Perl:
perl -le 'print ~~localtime time - 86400'
methyl
July 15, 2010, 10:27am
6
In my shell there is a syntax error. The complex condition can be replaced with a simple test by treating $NMONTH as a number.
# if [[ $NMONTH == "01" || $NMONTH == "1" ]]; then
if [ $NMONTH -eq 1 ]; then
Footnote. In this part of the world the parts of a DATE are DAY-MONTH-YEAR (not DATE-MONTH-YEAR).
hi annesh
it gives o/p as
date +%d-%m-%Y -d "1 day ago"
16-07-2010
ie current date
---------- Post updated at 01:21 AM ---------- Previous update was at 01:18 AM ----------
@ methyk
I still get the same o/p
+ date +%Y
YEAR=2010
+ date +%m
NMONTH=07
+ date +%d
DATE=16
+ [ 16 -eq 01 ]
+ expr 16 - 1
DATE=15
+ echo 15
15
./date.sh: bad substitution
methyl
July 16, 2010, 6:43am
8
I finally managed to reproduce the error message by using an old Bourne Shell.
The problem was obfuscated because your first script was called "date" but your second version was called "date.sh". It is not a good idea to name scripts the same name as unix commands.
What Operating System and version is your computer running?
uname -a
What do you get for the following command from your own account shell prompt:
echo ${SHELL}
Do you have a more modern shell like: ksh, bash or a Posix Shell ?