| Vimal |
Posted: Feb 22, 2005 12:47:53 PM |
Total Post: 14
Joined: Jan, 2003
|
Hi All,
I wanted to display previous day date in shell script.. How can i do that..
I tried date + %m%d -1%y
But does not work..
Please help..
Thanks,
Vimal |
|
|
Rohit |
| Posted: Feb 23, 2005 10:33:17 AM | |
|
Total Post: 2
Joined: Feb, 2005
|
You can do this...
d=`date +%d`
m=`date +%m`
y=`date +%y`
pdate=`expr $d -1`
ndate=$m$pdate$y
echo $ndate
|
|
|
|
|
Vimal |
| Posted: Feb 24, 2005 01:25:23 PM | |
|
Total Post: 14
Joined: Jan, 2003
|
Thanks..but how it will handle date like 1 feb, 2005. Month starting ?
|
|
|
|
|
Rajesh |
| Posted: Mar 01, 2005 04:10:09 PM | |
|
Total Post: 5
Joined: Jan, 2003
|
You can use following script.
#!/bin/bash
# Today's date formatted:
TODAY=`date +'%m-%d-%y'`
# Get individual elements
MONTH=`echo $TODAY | cut -d'-' -f1`
DAY=`echo $TODAY | cut -d'-' -f2`
YEAR=`echo $TODAY | cut -d'-' -f3`
if [[ `expr $DAY + 0` -eq 1 ]]; then
if [[ $MONTH -eq 1 ]]; then
MONTH=12
YEAR=`expr $YEAR - 1`
else
MONTH=`expr $MONTH - 1`
fi
cal $MONTH $YEAR | grep 31 1>/dev/null 2>&1
if [[ $? -eq 0 ]]; then
DAY=31
else
DAY=30
fi
else
DAY=`expr $DAY + 0`
DAY=`expr $DAY - 1`
fi
if [[ `echo $MONTH | wc -c` -eq 2 ]]; then
MONTH=0$MONTH
fi
if [[ `echo $DAY | wc -c` -eq 2 ]]; then
DAY=0$DAY
fi
# Previous day in the same format, without hyphens
NEW_DATE=$MONTH$DAY$YEAR
echo $NEW_DATE
|
|
|
|
|
| Time Zone: EDT |
Send this thread to your friend |