Shell script assistance with certificate expirations.
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
Read the comments I added for details:
#these aren't commands, so leave them out or comment them:
#Intergra Cert expiration 20130328
#FXdirect Cert expiration 20130328
#Fixingll Cert expiration 20130430
# Use $(..) instead of `..`. And you don't need cat.
# Use gnu date's -d option to get the actual date of comparison:
#TOTALCLIENT=$( wc -l /home/tmp/scripts/ioscertexpiry )
WARNDATE=$( date -d '-30 days' '+%Y%m%d' )
# you can eliminate awk with the proper use of read ("_" is a throw-away variable):
while read _ _ _ field4 _ ; do
# ((..)) is recommended when doing arithmetic comparisons:
# and don't put an exit in the else section or it will terminate
# the script on the first non-matching file
if (( field4 <= WARNDATE )); then
echo "Check the certificate it's about to expire"
By the way, since environment variables are generally all upper-case, it's recommended practice to keep your own user variables in lower-case or mixed-case to help differentiate them.
Yeah, that could be true. To tell the truth I had a hard time figuring out exactly what the OP code was trying to do, so I just threw in my best guess at the time. It doesn't change the general working of my example though, just the configuration of date.
What he is doing is using the date arithmetic built into the date command.
Unfortunately, full date arithmetic is rather complex to implement in a shell script. What is built into the date command is not easily documented, try "info date". The section on the --date (or -d) is there:
Display the date and time specified in DATESTR instead of the
current date and time. DATESTR can be in almost any common
format. It can contain month names, time zones, `am' and `pm',
`yesterday', etc. For example, `--date="2004-02-27
14:19:13.489392193 +0530"' specifies the instant of time that is
489,392,193 nanoseconds after February 27, 2004 at 2:19:13 PM in a
time zone that is 5 hours and 30 minutes east of UTC.
Note: input currently must be in locale independent format. E.g.,
the LC_TIME=C below is needed to print back the correct date in
date -d "$(LC_TIME=C date)"
*Note Date input formats::.
And unfortunately, does not indicate a general implementation (offsets from current time, yes,
but deltas between two dates don't seem to work (date --date "tommorrow - now" just comes up with the tomorrows date, and no error message).
To get general date arithmetic use Perl - there are a number of date arithmetic packages available.
If you really need to do relative date offsets on times other than now, then just convert both strings into %s epoch seconds, subtract one from the other, and compare the differences. All you need to do is ensure that individual date strings are in a format that date can accept.
diff=1209600 #14 days, in seconds
date1_es=$( date -d "$date1" '+%s' )
date2_es=$( date -d "$date2" '+%s' )
if (( date2_es - date1_es > diff )); then
echo "The difference is greater than 14 days."
echo "The difference is less than 14 days."