设置用户的到期日期

设置用户的到期日期

我使用 ubuntu 12.04,我有一个文件“用户配置文件”我把这个文件里的用户喜欢这个

F: user pass { enddate=2017-01-24 }

该文件存在于/var/etc/

这是文件的图片

我想要一个脚本来从这个文件中自动删除过期的用户“用户配置文件”

谢谢。

答案1

你可以做:

#!/bin/bash

## Reading "/var/etc/user.cfg", saving the line being read in variable "line"
while read -r line; do

    ## Getting the username from "user.cfg", saving in variable "user"
    user="$(grep -Po '^F:\s+\K[^\s]+' <<<"$line")"

    ## Getting the expiry date from "user.cfg", saving in variable "exp_cfg"  
    exp_cfg="$(grep -Po 'enddate=\K[^\s]+' <<<"$line")"

        ## Getting expiry time, days since Epoch from 
        ## "/etc/shadow" into variable "exp_shadow"
        exp_shadow="$(sudo grep -Po "^$user:.*:\K[^:]+(?=:[^:]*$)" /etc/shadow)"

            ## Check if "exp_shadow" is not Null
            [[ -n $exp_shadow ]] &&

            ## If not null, then convert the date from "exp_cfg" into days 
            ## since epoch and then check if its less than "exp_shadow"
            ## Replace ">" with "<" if you want to see the expired accounts :)
            (( $exp_shadow > $(($(date '+%s' <<<"$exp_cfg")/86400)) )) &&

            ## If so, print the line
            echo "$line"

done </var/etc/user.cfg

将其另存为例如user_exp_check.sh,现在您可以通过运行以下命令来查看具有未过期帐户的用户(假设您在脚本的目录中):

bash user_exp_check.sh

将输出保存在新文件中,例如new_user.cfg

bash user_exp_check.sh >new_user.cfg

然后您可以删除旧/var/etc/user.cfg文件。

相关内容