Formatting chage command date output

I wanted a script to visually warn users on my system that their password is about to expire. I found this one here.

The thing is, the author of that script gets the number of days to password expiration by converting dates to seconds, subtracting and then passing seconds to days.

The problem is that my system is outputting those dates as "ago 08, 2018" (for today). If I stick to using date command for date conversion to seconds as the author did, then I get an error: invalid date 'ago 08, 2018'.

Any help?

Here is the full script:

#! /bin/bash
# Issue a desktop notification if the user password is about to expire
# Uses the "chage" command frome the "passwd" package (likely installed)
# Best added to the session startup scripts
# get password data in array
saveIFS=$IFS
IFS=$'\n'
chagedata=( $(chage -l $USER | cut -d ':' -f 2 | cut -d " " -f 2-) )
IFS=$saveIFS
# obtain times in seconds
now=$(date +%s)
expires=$(date +%s -d "${chagedata[1]}")
# compute days left (roughly...)
daysleft=$(( ($expires-$now)/(3600*24) ))
echo "Days left: $daysleft"
# leave some evidence that the script really ran at startup
echo "Days left: $daysleft" > /var/tmp/$(basename $0).out
# determine and send the notification (stays mute if outside the warning period)
if [[ $daysleft -le 0 ]]
then notify-send -i face-worried.png -t 0 "Password expiration" "Your password expires within a day"'!'
elif [[ $daysleft -le ${chagedata[6]} ]]
then notify-send -i face-smirk.png -t 0 "Password expiration" "Your password expires in $daysleft days."
fi
2

1 Answer

According to date's documentation, input currently must be in locale independent format. They suggest to use LC_TIME=C to produce locale independent date output. In your case, you'll have to prepend the chage command to make it output a date string that date will be able to parse:

chagedata=( $(LC_TIME=C chage -l $USER | cut -d ':' -f 2 | cut -d " " -f 2-) )

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like