Need to go back 1 day using the date command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need to go back 1 day using the date command
# 1  
Old 09-28-2001
Need to go back 1 day using the date command

I am trying to write a shell script to look at log files with dates in the file name.

Now I know how to use the expr command to subtract 1 day from the other, which is simple when the dates are from the 2nd to the 31st of each month.

But the problem I have is when the date turns to the 1st and the last month is 31st or 30th - because 1 -1 = -1 don't it, rather than 30 or 31

How do I write a script to handle this.

P.S I'm using KSH
# 2  
Old 09-28-2001
Please search the forums, this has been brought up a few times today alone... Here are a few I found in a minute of searching:

https://www.unix.com/showthread.php?s...sterday+script

https://www.unix.com/showthread.php?s...date+yesterday < -- That one has a few different ways of doing it...
# 3  
Old 09-28-2001
This script was posted here some time ago, and I modified it slightly.
Code:
#!/usr/bin/ksh
date '+%m %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 
echo "Yesterday was: $MONTH $DAY $YEAR" 
}

This User Gave Thanks to 98_1LE For This Post:
# 4  
Old 12-07-2001
Ok, I understand the script and provided the following result ...
Yeseterday was: 6 12 2001 .

But how does one convert the month (12) into either the long or short name(ie - December/Dec).

I need to do this as Directories are named ... (eg - Nov2001, Dec2001, etc.). I'm trying to create a reporting script to run via cron on the 1st of every month perusing the pervious months sar files for system reports. The scripts purpose is to be run from cron and the command line if required (for reruns for eg.).

Any help appreciated - have already done a search.
Such is the life of a newbie.Smilie

Last edited by Cameron; 12-07-2001 at 09:37 PM..
# 5  
Old 12-12-2001
MySQL Solved ...

Try the following ... Smilie

## Long Name Month Listing.
lngmth="January February March April May June \
July August September October November December"
## Short Name Month Listing.
shtmth="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"

##
## Get Current Month Number (01-12).
xcurmth=`date +%m`
## Get Previous Month Number (01-11).
xprvmth=`expr $xcurmth - 1`
## Get Year Number (??).
xcuryr=`date +%Y`
## Get Previous Year Number (??).
xprvyr=`expr $xcuryr - 1`

xx=`echo $shtmth | cut -f$2 -d" "`
zz=`echo $lngmth | cut -f$2 -d" "`
XX=`echo $shtmth | cut -f$2 -d" "`$xprvyr
ZZ=`echo $lngmth | cut -f$2 -d" "`$xcuryr


If $xprvmth = 0 then you must use $xprvyr and 12 for the month. ...
(eg. `echo $shtmth | cut -f12 -d" "`$xprvyr)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
1 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Using date command, getting previous day

Legends, i need to get previous day using date command. Can you please help. sdosanjh:/home> date +%m%d%y 011514 i tried -d '-1 day' but it is not working (5 Replies)
Discussion started by: sdosanjh
5 Replies

3. AIX

Need to get the next day's date of the user entered date

I need to get the next day's date of the user entered date for example: Enter date (yyyy/mm/yy): 2013/10/08I need to get the next day's date of the user entered date Desired Output: 2013/10/09Though there are ways to achieve this is Linux or Unix environment (date command) ,I need to... (1 Reply)
Discussion started by: rpm120
1 Replies

4. Shell Programming and Scripting

Fetch date of 7 years back from current date in Perl

$beginDate = substr(DateCalc("today", "-7Days"),0,8); This fetches the date 7 days back Can I fetch the date before 7 years from todays date in Perl using same syntax Use code tags, see PM. (3 Replies)
Discussion started by: parthmittal2007
3 Replies

5. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

6. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

7. Shell Programming and Scripting

to find all files created a day back

Hi Guys, My unix is SunOS. I like to find all the files which are created 1 day back. i tried the following command find . -type f -name '*.aud' -mtime +1 This gives me all the files created 48 hours back (2 days) but not one.. Can you let me know where i am going wrong. Thanks,... (8 Replies)
Discussion started by: mac4rfree
8 Replies

8. Filesystems, Disks and Memory

Can I back up all the files I work with each day using tar?

Can I back up all the files I work with each day using tar? (2 Replies)
Discussion started by: jo calamine
2 Replies
Login or Register to Ask a Question