3

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
  • Is ago the abbreviated month name in your language? perhaps try LANG=en_US.UTF-8 chage -l $USER . . . Aug 8, 2018 at 15:05
  • Or, as date's documentation states, use LC_TIME=C.
    – danzel
    Aug 8, 2018 at 15:11

1 Answer 1

5

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-) )

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .