Bash写入/读取数组

时间:2016-10-27 16:11:24

标签: arrays bash if-statement

我从下面的脚本显示数组时遇到问题(它显示密码过期还有多少天):

#!/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)))

    # Do not send notification to legacy/non-expiring accounts - 365 days
    # and more from pw change
    if (($DAYS<=$WARN && $DAYS>=-365))
    then
            USER_ARRAY=${USER}
            DAYS_ARRAY=${DAYS}
    fi
    echo "${USER_ARRAY[@]} has ${DAYS_ARRAY[@]} days to password expiry"
done

示例输出是:

 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

第一行是空的,有些是重复的。 它的数组问题,简单的cmd:

echo $USER $DAYS

显示正确的值。

2 个答案:

答案 0 :(得分:2)

问题是你没有定义任何数组。

#!/bin/bash
warn=30
readarray -t users < <(awk -F: '{if ($3 >= 1000) { print $1 } }' /etc/passwd)
current_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")
    date_diff=$(($pw_exp_date - $current_date))
    days=$(($date_diff / (60*60*24)))

    # Do not send notification to legacy/non-expiring accounts - 365 days
    # and more from pw change
    if (($days <= $warn && $days >= -365))
    then
      user_array+=( "$user" )
      days_array+=( "$days" )
    fi
done

for ((i=0; i < ${#user_array[@]}; i++)); do
  echo "${user_array[i]} has ${days_array[i]} days to password expiry"
done

通常,不要为自己的变量使用全大写变量名;假设任何此类值已被保留用于其他目的。

答案 1 :(得分:0)

您是否已婚于使用阵列?只需在找到结果时回显结果。

#!/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)))

    # Do not send notification to legacy/non-expiring accounts - 365 days
    # and more from pw change
    if (($DAYS<=$WARN && $DAYS>=-365))
    then
        echo "$USER has $DAYS days to password expiry"
        #your mail code here
    fi
done