Getting Proper Date Format in SH Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting Proper Date Format in SH Script
# 1  
Old 02-01-2011
Getting Proper Date Format in SH Script

There's a small SH script I'm trying to write where it will get the current month and find a log file that is based on the date.
Example: Today is February, so the log file is going to be 201102.log (2011 + 02)
An additional thing is that if today is the 1st of a month, it will also find the log file of the previous month.
In the example's case, the previous month's log file is 201101.log.

However, when I run my script, the previous month's log file that the script tries to look for is 20111.log, which obviously ends up failing.
What's the extra bit of code I need to make it look for 201101.log?
The script works fine if the previous month is Oct, Nov, or Dec as this has already been tested.

Code:
DATE_CMD=date

# SET DEBUG to 1 to printout debug data
DEBUG=0
# SET DEBUG_NO_ACTION to 1 to prevent file transfer and update actions
DEBUG_NO_ACTION=0


if [ ${DEBUG_NO_ACTION} == 1 ]; then
   echo WARNING: DEBUG_NO_ACTION set to 1, no file xfer or update will occur
fi

CURDAY=`eval ${DATE_CMD} '+%d'`
if [ ${DEBUG} == 1 ]; then
  echo CURDAY: $CURDAY
fi
if [ "$CURDAY" == "01" ]; then
    CURYEAR=`eval ${DATE_CMD} '+%Y'`
    CURMONTH=$((`eval ${DATE_CMD} '+%m'` - 1))
    
    if  [ "$CURMONTH" == "0" ]; then
        CURYEAR=$((`eval ${DATE_CMD} '+%Y'` - 1))
        CURMONTH=12
    fi
    
    FILENAME=$CURYEAR$CURMONTH.log
        if [ ${DEBUG} == 1 ]; then
          echo PREVIOUS MONTH FILENAME: $FILENAME
        fi
fi

# 2  
Old 02-01-2011
Use printf to format the value:
Code:
CURMONTH=$(printf "%02d" $(($(eval date '+%m') - 1)))

And change the next line to:
Code:
    if  [ "$CURMONTH" == "00" ]; then


Last edited by Franklin52; 02-02-2011 at 03:21 AM..
This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 02-01-2011
Awesomeness, this worked! Thanks so much! Smilie
# 4  
Old 02-01-2011
If you have GNU date you can also do:
Code:
FILENAME=$(date -d yesterday +%Y%m)

If not a not-so-portable method is to set TZ to 24 hours past your timezone. For example if you are in New York (GMT+5) use GMT+29:

Code:
FILENAME=$(TZ=GMT+29 date +%Y%m)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine multiline to one line with proper format

Hello Guys, I have a file say FILE1.txt contains below data:- A B C D E F G H I J K L I need the output in another file as FILE2 as:- 'A', 'B', 'C', 'D', 'E', (7 Replies)
Discussion started by: jassi10781
7 Replies

2. Shell Programming and Scripting

Need to grep this Data in proper format:- Please Guide

Hi Guys, I need to grep below data in this format backup_id creation expiration policy sched_label backup_id = picoserver38_1212077050, version = 2 creation = 05/29/2008 18:04:10 (1212077050) expiration = 06/29/2008 18:04:10 (1214755450) retention_level = 3, fragment = 2, file_num = 1... (14 Replies)
Discussion started by: manalisharmabe
14 Replies

3. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

4. Shell Programming and Scripting

Awk - Summation in Proper decimal Format

Hi I am executing below command to do summation on 46th coloumn. cat File1| awk -F"|" '{p += $46} END { printf"Column Name | SUM | " p}' I am getting output as Column Name | SUM | 1.01139e+10 Here I want output in Proper decimal format. Can someone tell me what change is required for same? (1 Reply)
Discussion started by: sanranad
1 Replies

5. Shell Programming and Scripting

Need to split a xml file in proper format

Hi, I have a file which has xml data but all in single line Ex - <?xml version="1.0"?><User><Name>Robert</Name><Location>California</Location><Occupation>Programmer</Occupation></User> I want to split the data in proper xml format Ex- <?xml version="1.0"?> <User> <Name>Robert</Name>... (6 Replies)
Discussion started by: avishek007
6 Replies

6. Shell Programming and Scripting

Output file not displayed in the proper format

Hi am using uuencode fro attaching one report which is nothing but sql query output. But when i receive the report through attachement and when it is opened the report is not displayed in proper format. Means if the sql query has 100 rows the mail attachment report displays the report in 2... (2 Replies)
Discussion started by: weknowd
2 Replies

7. Shell Programming and Scripting

How to increment a user defined date value in the DATE format itself using shell script?

I need to increment a date value through shell script. Input value consist of start date and end date in DATE format of unix. For eg. I need increment a date value of 1/1/09 to 31/12/09 i.e for a whole yr. The output must look like 1/1/09 2/2/09 . . . 31/1/09 . . 1/2/09 . 28/2/09... (1 Reply)
Discussion started by: sunil087
1 Replies

8. Shell Programming and Scripting

Net::SSH::Perl ...... how to print the output in a proper format

Hi Guys, my $cmd = "ls -l"; #........ {or let it be as # my $cmd= "ls"; } my $ssh = Net::SSH::Perl->new($host); $ssh->login($user, $pass); my($stdout, $stderr, $exit) = $ssh->cmd("$cmd"); print $stdout; the script works fine, but i am unable to see the output getting displayed in a... (7 Replies)
Discussion started by: gsprasanna
7 Replies

9. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

10. Shell Programming and Scripting

Check for proper e mail id format

Hi, We run an application called meta which reads user information from database and updates in LDAP.For that we have some scripts to check the uniqueness of mail ids between the existing LDAP and Database.It works fine when people enter mail ids in proper format (xxx.yyy@abc.com) but if it... (2 Replies)
Discussion started by: prolay
2 Replies
Login or Register to Ask a Question