运行结果如下:
[root@server1 shell.sh]# ./useradd.sh
Please Input 1 or 2 :
1) Add User
2) Del User
#? 1
Please Input your username prefix:user
Please Input your user number:10
Please Enter your user password: 123
Add user1 Sucessful!
Add user2 Sucessful!
Add user3 Sucessful!
Add user4 Sucessful!
Add user5 Sucessful!
Add user6 Sucessful!
Add user7 Sucessful!
Add user8 Sucessful!
Add user9 Sucessful!
Add user10 Sucessful!
以下是脚本内容:
#!/bin/bash
#this scripts for batch add/del user
#DATE:2011/9/25 AUTHOR:LEO Email:chanyipiaomiao@163.com
#if you are not root,exit
ID=$(id -u)
if [ $ID -ne 0 ];then
echo "Your are not root,only root can Add/Del user"
exit 1
fi
#define variables
ADD="/usr/sbin/useradd"
DEL="/usr/sbin/userdel"
PASSWD="/usr/bin/passwd"
num=1
echo "Please Input 1 or 2 :"
select var in "Add User" "Del User"
do
break
done
read -p "Please Input your username prefix:" pre
read -p "Please Input your user number:" user_num
case $var in
"Add User")
read -p "Please Enter your user password: " PW
while [ $num -le $user_num ]
do
if [ -e /home/$pre$num -o -e /var/mail/$pre$num ]
then
echo "$pre$num already exists!!"
num=$(expr $num 1)
else
$ADD $pre$num && echo $PW | $PASSWD --stdin $pre$num &>/dev/null
if [ $? = 0 ];then
echo "Add $pre$num Sucessful!"
fi
num=$(expr $num 1)
fi
done
;;
"Del User")
while [ $num -le $user_num ]
do
$DEL -r $pre$num &>/dev/null && echo "Delete $pre$num Sucessful" || echo "There is no $pre$num"
num=$(expr $num 1)
done
;;
*)
echo "Invaild Input"
esac
本文出自 “雷纳科斯的博客” 博客,请务必保留此出处http://75280.blog.51cto.com/65280/750836
|