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!
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
# then
# echo "Check the certificate it's about to expire"
# else
# exit 0
#fi
done <"$EXPIREDCERT"
If i run the above the output is
20130328
20130328
20130430
If i change it to assign the DIFF=`expr $line - $TODAYSDATE`
and echo $DIFF
The error is shown below
###
can somebody assist and tell me what's wrong?
./certexp2.sh: line 10: Intergra: not found
expr: syntax error
./certexp2.sh: line 10: FXdirect: not found
expr: syntax error
./certexp2.sh: line 10: Fixingll: not found
expr: syntax error
I'm just figuring out how to subtract the value in the file with the current date and if less than 14 days, i would send out an email warning to myself.
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:
Code:
#!/bin/bash
#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:
EXPIREDCERT="/home/tmp/scripts/ioscertexpiry"
#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"
fi
done <"$EXPIREDCERT"
exit 0
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:
Code:
`-d DATESTR'
`--date=DATESTR'
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
many locales:
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.
Code:
diff=1209600 #14 days, in seconds
date1=20130315
date2=20130331
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."
else
echo "The difference is less than 14 days."
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.