我编写了以下脚本,以从单词列表中删除用户。单词表有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