jeudi 27 octobre 2016

Bash write/read to/from array

I have problem with displaying array from below script (it shows how many days left until password expires):

#!/bin/bash
WARN=30
USERS=$(awk -F: '{if ($3 >= 1000) { print $1 } }' /etc/passwd)
DATE=$(date "+%s")
for USER in $USERS
        do
                        GET_EXP_DATE=$(/usr/sbin/lchage -l $USER | grep 'Password Expires' | cut -d: -f2)
                        if [[ ${GET_EXP_DATE} == *"Never"* ]]
                        then
                                GET_EXP_DATE=01/01/2000
                        fi
                        PW_EXP_DATE=$(date -d "$GET_EXP_DATE" "+%s")
                        DIFF=$(($PW_EXP_DATE-$DATE))
                        let DAYS=$(($DIFF/(60*60*24)))
                        if (($DAYS<=$WARN && $DAYS>=-365)) # Do not send notification to legacy/non-expiring accounts - 365 days and more from pw change
                        then
                                USER_ARRAY=${USER}
                                DAYS_ARRAY=${DAYS}
                        fi
                        echo "${USER_ARRAY[@]} has ${DAYS_ARRAY[@]} days to password expiry"

Example output is:

 has  days to password expiry
 has  days to password expiry
 has  days to password expiry
 has  days to password expiry
 has  days to password expiry
 has  days to password expiry
 has  days to password expiry
andjac has -255 days to password expiry
apoole has -255 days to password expiry
rubtho has -255 days to password expiry
rubtho has -255 days to password expiry
rubtho has -255 days to password expiry
pfowle has -255 days to password expiry
pglomr has -255 days to password expiry
vrajan has -255 days to password expiry
vrajan has -255 days to password expiry
vrajan has -255 days to password expiry
vrajan has -255 days to password expiry
vrajan has -255 days to password expiry
vrajan has -255 days to password expiry
vrajan has -255 days to password expiry
vrajan has -255 days to password expiry

First lines are empty, and some are duplicated. It's array issue, as simple cmd:

echo $USER $DAYS

shows correct values.

Can you please advise? Thank you!

Aucun commentaire:

Enregistrer un commentaire