快速业务通道

Linux下批量添加/删除用户

作者 佚名技术 来源 Linux系统 浏览 发布时间 2012-03-22

运行结果如下:

[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

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号