dfn48
asked on
Software Expire
I have a program that outputs a file on the number days left before software license expired. It reads in a text file named expiredt.txt.
expiredt.txt contents are as follows:
EM software 30NOV2015 11/30/2015
ABM software 30NOV2015 11/30/2015
ABM software 30OCY2015 10/30/2015
Publish software 29JUN2016 06/29/2015
I am getting error messages. I am new to writing shell script programs.
here are errors I am getting:
expr: 0402-050 syntax error.
line 22 [: -lt: unary operator expected
line 25 [: -le: unary operator expected
date:Not recognized flag: d
usage: date [-u] [+ "Field Descriptor"]
expiredt.txt contents are as follows:
EM software 30NOV2015 11/30/2015
ABM software 30NOV2015 11/30/2015
ABM software 30OCY2015 10/30/2015
Publish software 29JUN2016 06/29/2015
I am getting error messages. I am new to writing shell script programs.
here are errors I am getting:
expr: 0402-050 syntax error.
line 22 [: -lt: unary operator expected
line 25 [: -le: unary operator expected
date:Not recognized flag: d
usage: date [-u] [+ "Field Descriptor"]
#!/bin/bash
# Set expiry file date name to first parameter, default to foo5.txt
EXP_FILE=${1:-expiredt.txt}
# Temp file to take the expired products, if any
OUT_FILE=/opt/SM/expired_license/EXP/license/chkexpire.$$
# Get the current date in seconds since the epoch
now_date=$(date "+%s")
while read exp_line; do
# Get the expiry date as the last field in the expiry file
exp_date=$(echo $exp_line | awk '{print $NF}')
# Convert that date to seconds since the epoch
exp_seconds=$(date -d ${exp_date} "+%s")
# Work out days to go
days_to_go=$(expr \( 86399 + $exp_seconds - $now_date \) / 86400)
if [ $days_to_go -lt 0 ]; then
((days_to_go=0 - days_to_go))
echo PROBLEM: $exp_line expired $days_to_go days ago
elif [ $days_to_go -le 30 ]; then
echo $exp_line has $days_to_go days left
fi
done < ${EXP_FILE} > ${OUT_FILE}
if [ -s ${OUT_FILE} ]; then
# One or more products expiring - report them
echo "$(cat ${OUT_FILE} | wc -l) Product(s) Expiring"
cat ${OUT_FILE} | mail -s "$(cat ${OUT_FILE} | wc -l) Product(s) Expiring" j.mj@aol.com
else
echo File ${OUT_FILE} is empty, so no expired software
fi
rm -f ${OUT_FILE}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.