学习linux是从基础的命令开始的,当熟悉命令后,我们就得学习shell脚本的编写。在实际运维中,我们不可能一直盯着服务器看,机器式的维护,而是通过脚本,实现自动化运维,这也是运维的一种趋势。本菜鸟也是刚刚接触shell脚本。今天兴致大发,就写来一个简单的”系统用户管理菜单“脚本
该脚本功能如下:
1.可以实现增加删除用户;
2.判断用户是否已设置密码;
3.并能选择用户进行设置密码;
4.查询和更改uid、gid;
5.统计用户数,系统用户和普通用户数;
该脚本的代码如下:
#!/bin/bash
#Author:***
#Date:2014/05/14
#mail:690968964@qq.com
# This is system user admin menu
menu(){
echo -e "\t\t\tSystem user admin menu"
echo -e "\t\t1.add user"
echo -e "\t\t2.del user"
echo -e "\t\t3.query user information"
echo -e "\t\t4.reset user passwd"
echo -e "\t\t5.modify user information"
echo -e "\t\t6.all user information"
echo -e "\t\t7.exit the program\n"
}
Useradd(){
echo -n "please input you want add username:"
read username
useradd $username
if [ $? -eq 0 ]
then
echo "OK.add user "
else
echo "add user error"
return 1
fi
}
Userdel(){
echo -n "Please input you want delete username:"
read username
userdel -r $username
if [ $? -eq 0 ]
then
echo "OK,del user"
else
echo "del user error"
return 1
fi
}
query_user_info(){
id $username | awk ‘{print $1,$2}‘
cat /etc/passwd | grep $username |awk -F: ‘{print $6}‘
Passwd=`cat /etc/shadow | grep $username |awk -F: ‘{print $2}‘`
if [ "$Passwd" == "!!" ];then
echo "$username没有设置密码"
else
echo "$username设置了密码"
fi
}
reste_user(){
cat /etc/shadow |awk ‘/!!/‘| awk -F: ‘{print $1}‘
read -p "请输入你选择的用户:" username
passwd $username
}
modify_user_info(){
cat /etc/passwd | awk -F: ‘{print $1}‘
read -p "请输入你选择的用户:" username
usermod -u 600 $username
groupmod -g 700 $username
secret=`cat /etc/shadow | grep $username |awk -F: ‘{print $2}‘`
if [ "$secret" == "!!" ];then
passwd $username
else
echo "$username设置了密码"
fi
}
all_user_info(){
user_all=`cat /etc/passwd | awk -F: ‘{print $1}‘ | wc -l`
user_sys=`cat /etc/passwd | awk -F: ‘$3<500{print $1}‘| wc -l `
user_pro=`cat /etc/passwd | awk -F: ‘$3>=500 && $3<60000{print $1}‘| wc -l`
echo "所有用户的数量为:"$user_all
echo "系统用户的数量为:"$user_sys
echo "程序用户的数量为:"$user_pro
}
main(){
while [ 1 ]
do
menu
echo -n "Enter your option:"
read option
case $option in
"1")
Useradd
if [ $? -eq 1 ]
then
exit 2
fi
;;
"2")
Userdel
if [ $? -eq 1 ]
then
exit 2
fi
;;
"3")
query_user_info
;;
"4")
reste_user
;;
"5")
modify_user_info
;;
"6")
all_user_info
;;
"7")
break
;;
"*")
break
;;
esac
done
}
main;
请求各位大侠指正错误,并指导完善,谢谢!
本文出自 “菜鸟高飞” 博客,请务必保留此出处http://liyuanji.blog.51cto.com/8671343/1411254
用shell脚本实现增加,删除用户,查询更改UID和GID以及统计用户数,布布扣,bubuko.com
用shell脚本实现增加,删除用户,查询更改UID和GID以及统计用户数
原文:http://liyuanji.blog.51cto.com/8671343/1411254