SAS License expire in 30 days .

When I execute my shell script program, it errors.  The program calculate the number of days left before licenses expire which the target is 30 days between the  current date and the expiration date. The program send email with the number days left and the product names.





# Convert date to check in seconds 
current_date='date "+%s"'

#foo5.txt 

expires=' /path/to/ foo5.txt
-noout -enddate | grep | awk '{print $1, $2, $3}''

date_to_check=date -d  | awk -v expires="$expires" '{print $2 expires}'expires "+%s"'

#calculate the datediff
datediff='expr $date_to_check - $current_date'


#30 days left - send email: 30 days in seconds(2592000)

if [ 'expr $date_diff - 2592000' -gt 0 ]
then 
echo | awk expires="$expires"'{print "30 days left. Product and Expiration date: " $1, $3 }'expires'| mail -s "Product Expiring" mary@rer.com

# 29 days left - send email- 29 days in seconds(2505600)

elif [ 'expr $date_diff - 2505600' -gt 0 ]
then
echo | awk expires="$expires"'{print "29 days left. Product and Expiration date: " $1, $3}'expires'| mail -s "Product Expiring" mary@rer.com

# 28 days left send email. 28 days in seconds(2419200) 

elif [ 'expr $date_diff - 2419200' -gt 0 ]
then 
echo | awk expires="$expires"'{print "28 days left. Product and Expiration date:"  $1, $3}'expires'| mail -s "Product Expiring" mary@rer.com

# 27 days left -  send email 27 days in seconds (233200)

elif [ 'expr $date_diff - 233200' -gt 0 ]
then 
echo | awk expires="$expires"'{print "27 days left. Product and Expiration date:" $1, $3}'expires'| mail -s "Product Expiring" mary@rer.com
fi

Open in new window


Text file: foo5.txt:

B colb software    2015/11/30           30Nov2015
S/G                         2015/10/30           30Oct2015
GH/Software        2015/11/30           30Nov2015
KP/software         2015/10/30            30Oct2015
license-expires-in-30-days.txt
foo5.txt
dfn48Asked:
Who is Participating?
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.

simon3270Commented:
I'm not sure whether formatting was confused in your script, but some of it simply won't work (the "variable=command | commnd2 | command3" bits, for example - I think you meant "variable=$(command1 | command2 | command3)")

I've reorganised it a bit, so that it sends a single email if any products are about to expire or have indeed already expired.

What is the reason for putting the expiry date twice, in two different formats, in the expiry date file?  I've used the one at the end of the line (e.g. "30Nov2015"), and included the entire text line on the email body (removing the last-but-one field would be doable if you like).

By the way, you make it harder for yourself by having spaces within the product names = you can't just assume that $3 is the field at the end of the line - in the first line, for example, the last field is $5, because the product name takes up $1, $2 and $3.

#!/bin/bash

# Set expiry file date name to first parameter, default to foo5.txt
EXP_FILE=${1:-foo5.txt}

# Temp file to take the expired products, if any
OUT_FILE=/tmp/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" mary@rer.com
else
  echo File ${OUT_FILE} is empty, so no expired software
fi

rm -f ${OUT_FILE}

Open in new window

0

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
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.