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 |
Declare date without "'", as:
Code:
#!/bin/bash |
Shivva,
thanks for the response. i replaced it but still getting the same error. |
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 |
Thanks Shivva.. It worked!!
|
You can Mark the thread as solved (option is under Thread Tools on top menu).
|
Quote:
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/ |
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 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 |
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.
|
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' 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 |
All times are GMT -5. The time now is 11:28 PM. |