【发布时间】:2018-01-15 20:03:28
【问题描述】:
我正在尝试创建一个脚本,它会在其中添加新用户和密码,并在以 Root 运行时检查该用户和密码是否已经存在。
所以,我的脚本运行良好。它只在根目录中运行,它会正确检查用户名是否已被使用。但是,我似乎无法添加新用户和密码。以下是我的整个脚本:
#!/bin/bash
#Creating a script that creates a new user
ROOT_UID=0 #Root has $UID 0
SUCCESS=0
E_USEREXISTS=70
E_NOTROOT=65 #Not root
#Run as root, and this checks to see if the creater is in root. If not, will not run
if [ "$UID" -ne "$ROOT_UID" ]; then
echo "Sorry must be in root to run this script"
exit $E_NOTROOT
fi
if [ $# -eq 2 ]; then
username=$1
pass=$2
grep -q "$username" /etc/passwd
if [ $? -eq $SUCCESS ]; then
echo "User $username already exists"
echo "Please choose another username"
exit $E_USEREXISTS
fi
echo $pass | passwd $username --stdin
echo "the account is setup"
else
echo "this program needs 2 arguments and you have given $#"
echo "you have to call the script $0 username and the pass"
fi
exit 0
而且我没有得到一个直接的错误,但这里是正在发生的事情的一个例子: 我尝试添加某人(用户名然后密码):
[root@localhost Documents]# ./Script dkong 123bif
响应是:
passwd: Unknown user name 'dkong'
the account is setup
有人可以帮助指导我吗?我希望它创建一个新的用户名和密码,但我不明白为什么不这样做。
我已经有一段时间没有使用脚本了,如果答案很明显,我很抱歉!只需要一点方向。提前谢谢!
【问题讨论】: