![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Time Between Dates | Sreejith_VK | Shell Programming and Scripting | 3 | 02-19-2008 10:06 PM |
| UNIX Dates | ndoggy020 | UNIX for Dummies Questions & Answers | 9 | 02-14-2008 03:25 PM |
| Select entries between two dates by converting Unix timestamp in Oracle Database. | amitsayshii | UNIX for Advanced & Expert Users | 1 | 08-08-2006 08:00 AM |
| How to compare two dates | bankpro | High Level Programming | 5 | 01-24-2006 01:07 AM |
| Unix dates | ssmiths001 | UNIX for Dummies Questions & Answers | 1 | 11-18-2004 07:45 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need help with Unix dates
I'm very new to Unix, so forgive me for what will probably be a very quick fix. I have the following in a ksh script. Its purpose is to return yesterday's date. The $DAY part is coming back with a '2' but I need it to return as '02'. How can I do that?
date '+%b %d %Y' | { read MONTH DAY YEAR DAY=`expr "$DAY" - 1` case "$DAY" in 0) MONTH=`expr "$MONTH" - 1` case "$MONTH" in 0) MONTH=12 YEAR=`expr "$YEAR" - 1` ;; esac DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1` esac } |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Something like this will strip the leading zero, but will leave dates beginning with 1,2 or 3 alone...
echo `date +%d` | sed 's/[0]\([0-9]\)/\1/' Cheers ZB |
|
#3
|
|||
|
|||
|
Where would I add that to this script?
|
|
#4
|
||||
|
||||
|
You don't want to strip a leading zero anyway. You want to add a leading zero. Try this:
((DAY < 10)) && DAY="0"$DAY Put it after the code that sets DAY. |
|
#5
|
||||
|
||||
|
Re-reading your OP, you want to ADD the leading zero on dates less than 10, correct?
If so add Code:
DAY=`echo $DAY | sed 's/^\(.\)$/0\1/'` ZB |
||||
| Google The UNIX and Linux Forums |