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"]


# Set expiry file date name to first parameter, default to foo5.txt

# Temp file to take the expired products, if any

# 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
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"
  echo File ${OUT_FILE} is empty, so no expired software

rm -f ${OUT_FILE}

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Looks like your "date" doesn't support "-d" (I'm pretty sure it's a GNU extension), so we need some other way to work out the date.

That error meant that $exp_seconds was not set, so the "expr" error on line 20 meant that $days_to_go was not set, so the "if" test on line 22 looked like:
    if [ -lt 0 ]; then
which is not valid.

You can use Perl if installed, to do
    exp_seconds=$(perl -e 'use Time::Local; print timelocal(0,0,0,$ARGV[1], $ARGV[0]-1, $ARGV[2]);' $(echo $exp_date | sed 's,/, ,g'))

Open in new window

The rest shoudl then work fine

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.