我编写了以下脚本来从单词列表中删除用户。单词列表有3个或4个字段,分别是名字、中间名、姓氏和用户ID。我使用awk创建一个用户名,其中包含用户的名字首字母、姓氏和ID的最后两位数字。然后使用带有标志r的命令userdel删除用户及其主目录。
然而,当我运行脚本时,它给我一个错误,显示以下内容:
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脚本:
#! /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发布于 2019-11-09 19:26:35
如何调用userdel
userdel -r username
这将删除用户名为的用户的帐户,并删除该用户的主目录和关联的邮件文件。
为此,您需要使用变量,而不是调用函数。
否则userdel会抱怨,就像它已经在做的那样,或者就像输入userdel一样。
https://stackoverflow.com/questions/58775171
复制相似问题