LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 04-05-2013, 10:52 AM   #1
liketheshell
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Rep: Reputation: Disabled
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
 
Old 04-05-2013, 11:01 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Declare date without "'", as:
Code:
#!/bin/bash
.....
.....
TODAYSDATE=$(date +%Y%m%d)
.....
.....
DIFF=$(expr $line - $TODAYSDATE)
echo $DIFF
 
Old 04-05-2013, 11:08 AM   #3
liketheshell
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
Shivva,

thanks for the response. i replaced it but still getting the same error.
 
Old 04-05-2013, 11:32 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
........
........

Last edited by shivaa; 04-05-2013 at 12:02 PM.
 
Old 04-05-2013, 11:46 AM   #5
liketheshell
LQ Newbie
 
Registered: Sep 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
Thanks Shivva.. It worked!!
 
Old 04-05-2013, 12:06 PM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
You can Mark the thread as solved (option is under Thread Tools on top menu).
 
Old 04-05-2013, 05:32 PM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by liketheshell View Post
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/
 
Old 04-06-2013, 04:20 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
Old 04-06-2013, 08:37 PM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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.
 
Old 04-07-2013, 05:35 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 04-07-2013, 06:14 AM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
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.

Last edited by jpollard; 04-07-2013 at 06:17 AM.
 
Old 04-07-2013, 07:17 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with SSL certificate while executing shell script soumyacs Linux - Newbie 1 09-13-2012 07:33 AM
Need assistance on shell script gunturian Linux - General 3 01-08-2010 01:15 PM
Need assistance with shell script to pull MP3 file Cpare Linux - Newbie 5 06-28-2008 08:53 PM
Shell script assistance required.. TheEngineer Linux - Newbie 4 10-26-2006 06:42 AM
Assistance with 'find -exec cp' shell script dick.swift Linux - Software 6 01-23-2006 10:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration