交互式创建用户(适用于网络小白)
此脚本可以防止小白用户在创建用户时出现错误,从而提高效率!
1.实现的功能
1.用户输入用户名或密码有两次机会 2.当用户两次都不输入用户名,会自动退出程序,需要重新执行脚本 3.当用户只输入用户名,而不输入密码,会自动为用户设置默认密码(123.com) …
2.代码内容
echo -n
"请输入用户名:"
read user1
if [ -z
$user1 ]
then
echo -n
"请重新输入用户名:"
read user2
if [ -z
$user2 ]
then
echo "输入错误,请重新执行脚本"
exit
else
useradd
$user2
id
$user2 &>
/dev
/null
if [ $?
-eq 0
]
then
echo "$user2 用户创建成功"
fi
fi
else
useradd
$user1
id
$user1 &>
/dev
/null
if [ $?
-eq 0
]
then
echo "$user1 用户创建成功"
fi
fi
echo -n
"请输入密码:"
read password1
if [ -z
$password1 ]
then
echo -n
"请重新输入密码:"
read password2
if [ -z
$password2 ]
then
echo "输入错误,已为你设置默认密码(123.com)"
password_default=
"123.com"
echo "$password_default" | passwd
--stdin
$user1 &>
/dev
/null
echo "$password_default" | passwd
--stdin
$user2 &>
/dev
/null
else
echo "$password2" | passwd
--stdin
$user1 &>
/dev
/null
echo "$password2" | passwd
--stdin
$user2 &>
/dev
/null
if [ $?
-eq 0
]
then
echo "密码设置成功"
fi
fi
else
echo "$password1" | passwd
--stdin
$user1 &>
/dev
/null
echo "$password1" | passwd
--stdin
$user2 &>
/dev
/null
if [ $?
-eq 0
]
then
echo "密码设置成功"
fi
fi
3.基础命令
touch user
.sh
vim user
.sh
sh user
.sh
cd
/home
ls
del user_name