首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于从Wordlist中删除用户的Bash脚本

用于从Wordlist中删除用户的Bash脚本
EN

Stack Overflow用户
提问于 2019-11-09 08:04:46
回答 1查看 148关注 0票数 1

我编写了以下脚本来从单词列表中删除用户。单词列表有3个或4个字段,分别是名字、中间名、姓氏和用户ID。我使用awk创建一个用户名,其中包含用户的名字首字母、姓氏和ID的最后两位数字。然后使用带有标志r的命令userdel删除用户及其主目录。

然而,当我运行脚本时,它给我一个错误,显示以下内容:

代码语言:javascript
复制
Usage: userdel [options] LOGIN

Options:
  -f, --force                   force some actions that would fail otherwise
                                e.g. removal of user still logged in
                                or files, even if not owned by the user
  -h, --help                    display this help message and exit
  -r, --remove                  remove home directory and mail spool
  -R, --root CHROOT_DIR         directory to chroot into
  -Z, --selinux-user            remove any SELinux user mapping for the user

脚本:

代码语言:javascript
复制
#! /bin/bash

# Removing users using positional parameters

getusername(){
    line=${1}
    len=`echo ${line}|awk '{ FS = " " } ; { print NF}'`
    if [[ ${len} -eq 3 ]]
    then
        initial=`echo ${line}| awk {'print $1'} |cut -c1`
        lastname=`echo ${line} | awk {'print $2'}`
        id=`echo ${line}| awk {'print $3'}|grep -o '..$'`
        username=`echo ${initial}${lastname}${id} |tr '[:upper:]' '[:lower:]'`
    elif [[ ${len} -eq 4 ]]
    then
        initial=`echo ${line} | awk {'print $1'} |cut -c1`
        lastname=`echo ${line} | awk {'print $3'}`
        id=`echo ${line}| awk {'print $4'}|grep -o '..$'`
        username=`echo ${initial}${lastname}${id} |tr '[:upper:]' '[:lower:]'`
    else
        echo "Line ${line} is not expected as it should be considered for creating Username and Password"
    fi    
}

sudo userdel -r  $getusername
EN

回答 1

Stack Overflow用户

发布于 2019-11-09 19:26:35

如何调用userdel

userdel -r username

这将删除用户名为的用户的帐户,并删除该用户的主目录和关联的邮件文件。

为此,您需要使用变量,而不是调用函数。

否则userdel会抱怨,就像它已经在做的那样,或者就像输入userdel一样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58775171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档