我们有一台 Centos 7 机器,我们需要添加许多 NoLogin 密码条目,例如:
用户名:!!:1010:1001:1stName 姓氏:/home/用户名/:/sbin/nologin
我们有一份新用户的 CSV 列表,格式如下:
用户名,UID,名字,姓氏
请帮我找到一种方法来创建一个用户密码条目,其中插入用户名、UID、名字、姓氏、主目录/用户名以及 nologin 后缀,谢谢,罗伯特
答案1
就像是...
#!/bin/sh
# example pass.txt contains...
# username,UID,1stName,LastName
# john,1111,dave,smith
# colin,2222,henry,north
# freda,3333,susan,doig
if test $# -eq 1 && test "${1}" = "-u"
then
# Use userid in file
Do_UID_File=1
else
Do_UID_File=0
fi
IFS=,
while read username UIDnum FirstName LastName
do
# echo "User name is ${username}, UID is ${UIDnum}, 1st name is ${FirstName}, last name is ${LastName}"
$(id "${username}" > /dev/null 2>&1)
if test $? -eq 0
then
echo "Failed to add ${username}, UID is ${UIDnum}, 1st name is ${FirstName}, last name is ${LastName} - username already exists"
else
$(id "${UIDnum}" > /dev/null 2>&1)
if test $? -eq 0
then
# UID in use
if test ${Do_UID_File} -eq 0
then
# UID in use and do not use UID in file
useradd -m -N -s /sbin/nologin "${username}" 1
else
# UID in use and do use UID in file
echo "Failed to add ${username}, UID is ${UIDnum}, 1st name is ${FirstName}, last name is ${LastName} - user ID already exists" 2
fi
else
# UID not in use
if test ${Do_UID_File} -eq 0
then
# UID not in use and do not use UID in file
useradd -m -N -s /sbin/nologin "${username}" 3
else
# UID not in use and do use UID in file
useradd -m -N -s /sbin/nologin -u ${UIDnum} "${username}" 4
fi
fi
fi
done < pass.txt
一如既往,检查、检查、然后再检查。