Solved

Generic shell function to display dates from existing system date (given current date)  (for hpux, solaris and linux)

Posted on 2014-01-19
5
402 Views
Last Modified: 2014-01-19
I need a ascript that can help to find the last day of the previous month by given a current date in all above server OS.

For example, if cuurent date = '20/Mar/2013'
result of  "last_date = X_function" will be 28/Feb or 29/Feb depending on the year

For example, if cuurent date = '22/Mar/2013'
result of  "last_date = X_function" will be 28/Feb or 29/Feb depending on the year

if current date = '23/Jan/2014'
result of  "last_date = X_function" will be 31/Dec/2014

if current date = '03/Jan/2014'
result of  "last_date = X_function" will be 31/Dec/2014

perl calling is allowed, but has to be generic so that the perl calling can work in all above server OS.
0
Comment
Question by:tindavid
5 Comments
 
LVL 10

Expert Comment

by:lanboyo
ID: 39793263
Linux is easier than most because date is a lot better.

The key is always the unix époq

NOW=$( date +%s )
DOM=$( date +%d )
   # to figure out what the unix époq was the last day of last month, we would subtract the day of the month * number of seconds in a day from the current époq
LESS=$((  $DOM * 60 * 60 *24 ))
ELDOM=$(( $NOW - $LESS ))
OUTPUT= $( echo $ELDOM  | date -p +%d/%b/%Y )
echo $OUTPUT


Solaris is kind of icky, to get the current époq you have to do backflips. I will copy what I did for a different script if no one hits it first.
0
 
LVL 10

Expert Comment

by:lanboyo
ID: 39793273
Seriously I have greped out the output of the date command thru the adb debugger to get the époq. Honestly, what a pain, the system is clearly using it internally, but the date command won't expose it for me in Solaris.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39793306
What do you mean by the below?
perl calling is allowed, but has to be generic so that the perl calling can work in all above server OS.
Is it allowable to use perl modules not in the core?  What version of perl do you have installed on the machines?
0
 

Author Comment

by:tindavid
ID: 39793352
We have Oracle software (9i - 11g)  installed in most of the servers, therefore a perl version that comes with Oracle software will do.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39793444
perl -le '($h,$d)=(localtime)[2,3]; print join"/",(split/ /,localtime time-($d*24+$h-12)*60*60)[2,1,4];'
will work across daylight saving time switchover
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question