LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script assistance with certificate expirations. (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-assistance-with-certificate-expirations-4175456983/)

liketheshell 04-05-2013 10:52 AM

Shell script assistance with certificate expirations.
 
Hello All,

Can somebody please assist and tell me what i'm doing wrong? I have a flat file that holds the name and expiration of certificates which i maintain.

contents of file below:

Intergra Cert expiration 20130328
FXdirect Cert expiration 20130328
Fixingll Cert expiration 20130430

EXPIREDCERT="/home/tmp/scripts/ioscertexpiry" #| awk '{ print $4 }'`
#$TOTALCLIENT=`cat /home/tmp/scripts/ioscertexpiry | wc -l`
TODAYSDATE=`date '+%Y%m%d'`
#for((i=1; i<=$TOTALCLIENT; i++ ))
while read line

do
echo $line | awk '{ print $4 }'
#DIFF=`expr $line - $TODAYSDATE`

# if [ "$TODAYSDATE" -lt 30 ]

# 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.

All the best

shivaa 04-05-2013 11:01 AM

Declare date without "'", as:
Code:

#!/bin/bash
.....
.....
TODAYSDATE=$(date +%Y%m%d)
.....
.....
DIFF=$(expr $line - $TODAYSDATE)
echo $DIFF


liketheshell 04-05-2013 11:08 AM

Shivva,

thanks for the response. i replaced it but still getting the same error.

shivaa 04-05-2013 11:32 AM

Since content of EXPIREDCERT file is not a number (but a string), so you need to first redirect it's numbers to other file, and then try as:
Code:

#!/bin/bash
EXPIREDCERT="/home/tmp/scripts/ioscertexpiry"
NEWFILE="/tmp/newfile.txt"
TODAYSDATE=$(date +%Y%m%d)

awk '{print $4}' $EXPIREDCERT > $NEWFILE
while read -r line; do
echo $line | awk '{ print $4 }'
DIFF=$(expr $line - $TODAYSDATE)
echo $DIFF
done < $NEWFILE
\rm $NEWFILE
........
........


liketheshell 04-05-2013 11:46 AM

Thanks Shivva.. It worked!!

shivaa 04-05-2013 12:06 PM

You can Mark the thread as solved (option is under Thread Tools on top menu).

jpollard 04-05-2013 05:32 PM

Quote:

Originally Posted by liketheshell (Post 4925770)
Thanks Shivva.. It worked!!

Acutally, it likely didn't work as you expect.

The dates you have shown are in "yyyymmdd" format. which is ok.

But consider the following two dates:

20130131, and 20130201.

How many days are between? using the simple 20130201 - 20130131, and you get... 70 days.
Using date arithmetic, you should get 1 day.

http://www.walkernews.net/2007/06/03...shell-scripts/

David the H. 04-06-2013 04:20 PM

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.

Scripting With Style

jpollard 04-06-2013 08:37 PM

I think you want +30 days, not -30 days. What you have there for WARNDATE is 30 days ago, not 30 days in the future.

David the H. 04-07-2013 05:35 AM

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.

jpollard 04-07-2013 06:14 AM

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.

David the H. 04-07-2013 07:17 AM

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



All times are GMT -5. The time now is 11:28 PM.