【shell 练习4】编写Shell用户管理脚本(二)
一、创建、删除、查看用户,随机生成八位数密码
#!/bin/bash
#Author:yanglt
#!/bin/bash
#Author:yanglt
#Blog:https://www.cnblogs.com/yangleitao/
#Time:-- ::
#Version:V1. function wait()
{
echo -n '3秒后继续'
for ((i=;i<;i++))
do
echo -n "...";sleep
done
echo
}
in_check(){
expr $num1 + >/dev/null >&
if [ $? -ne ];then
echo "please input number"
exit
fi
}
empty_check(){
if [ ! $name ];then
echo "The input cannot be empty"
continue
fi
}
while true
do
cat<<EOF
#################################
.create user
.delete user
.query user
.exit
################################
EOF
read -p "Please input num:" num
in_check
case "$num" in
)
read -p "please input name:" name
empty_check
grep -w $name /etc/passwd >/dev/null
if [ $? -eq ];then
echo -e "$name already exist"
exit
else
useradd -s /bin/bash $name
echo -n `mkpasswd -l ` |tee -a ${name}Passwd.txt|passwd --stdin $name
echo -e "$name create successful"
fi
;;
)
read -p "please input delete name:" name
empty_check
grep -w $name /etc/passwd >/dev/null
if [ $? -ne ];then
echo -e "$name is not exist"
exit
else
userdel -r $name
rm -rf ${name}Passwd.txt
echo -e "$name delete successful"
fi
;;
)
read -p "please input need query name:" name
empty_check
grep -w $name /etc/passwd >/dev/null
if [ $? -eq ];then
echo `grep -w $name /etc/passwd`
echo -e `awk '{print "Password:",$0}' ${name}Passwd.txt`
else
echo -e "$name is not exist"
continue
fi
;;
*)
exit
;;
esac
wait
done
二、用户管理改进
[root@localhost ~]# cat UserManger03.sh
#!/bin/bash
#Author:yanglt
#!/bin/bash
#Author:yanglt
#Blog:https://www.cnblogs.com/yangleitao/
#Time:-- ::
#Version:V1.
. /etc/init.d/functions #调用内部函数action function wait()
{
echo -n '2秒后继续'
for ((i=;i<;i++))
do
echo -n "...";sleep
done
echo
}
in_check(){
expr $num1 + >/dev/null >&
if [ $? -ne ];then
echo "please input number"
exit
fi
}
empty_check(){
if [ ! $name ];then
echo "The input cannot be empty"
continue
fi
}
makedir(){
dir=/yanglt/
[ ! -n $dir ]&& mkdir $dir
} action_check(){
if [ $? -eq ];then
action "useradd $name successful" /bin/true
else
action "useradd $name " /bin/false
fi
}
while true
do
cat<<EOF
#################################
.create user
.delete user
.query user
.exit
################################
EOF
read -p "Please input num:" num
in_check
case "$num" in
)
read -p "please input name:" name
empty_check
pass=`mkpasswd -l `
grep -w $name /etc/passwd >/dev/null
if [ $? -eq ];then
echo -e "$name already exist"
exit
else
makedir
useradd -s /bin/bash $name &>/dev/null
echo $pass|passwd --stdin $name &>/dev/null
action_check
echo -e "$name\t$pass" >> /yanglt/Passwd.txt
fi
;;
)
read -p "please input delete name:" name
empty_check
grep -w $name /etc/passwd >/dev/null
if [ $? -ne ];then
echo -e "$name is not exist"
exit
else
userdel -r $name
sed -i "/^$name$/d" /yanglt/Passwd.txt #进一步了解精确匹配,和变量匹配
echo -e "$name delete successful"
fi
;;
)
read -p "please input need query name:" name
empty_check
grep -w $name /etc/passwd >/dev/null #-w 精确匹配
if [ $? -eq ];then
grep -w "$name" /etc/passwd
grep -w "$name" /yanglt/Passwd.txt
else
echo -e "$name is not exist"
continue
fi
;;
*)
exit
;;
esac
wait
done
[root@localhost ~]#
【shell 练习4】编写Shell用户管理脚本(二)的更多相关文章
- linux系统用户管理(二)
5.组命令管理**组账户信息保存在/etc/group和/etc/gshadow两个文件中 /etc/group 组账户信息 [root@localhost ~]# head -2 /etc/grou ...
- Identity用户管理入门二(显示用户列表)
在Controllers中新建AccountController,并在构造方法(函数)中注入SignInManager,UserManager UserManager 用户管理(注册,查找,修改, ...
- 【shell 练习3】用户管理脚本(一)
一.创建十个用户,密码为八位 [root@localhost ~]# cat UserManger02.sh #!/bin/bash . /etc/init.d/functions [ $UID -n ...
- PC Server远程管理卡用户管理脚本实现
1.IPMI工作原理图: 2.脚本实现流程图: 3.适配服务器机型: 4.演示效果: 5.实现代码: #!/usr/bin/env bash # Author : JACK ZHAO # Date : ...
- 转自ruby迷: 使用Net::SSH和Net::SCP编写Linux服务器管理脚本
试了一下perl下安装ssh模块,整了半天linux/window上都装不上,各依赖模块的版本总是匹配不上,后改了一下思路,用ruby吧 Net::SSH和Net::SCP是两个Ruby操作SSH的g ...
- linux用户管理(二)
最简单的加入用户useradd 用户名这样的命令不好,因为会新建一个组为这个用户. 这里提到一个问题,为什么普通用户没有/etc/shadow的写权限却能修改自己的密码呢. 因为 /user/bin/ ...
- Linux 用户管理(二)
一.groupadd --create a new group 创建新用户 -g --gid GID 二.groupdel --delete a group 三.passwd --update us ...
- linux下的用户管理(二)
创建帐号: 手工创建-> 1.在/etc/passwd中添加一条记录 2.创建用户主目录 3.在用户的家目录下设置默认的配置文件 4.设置用户初始口令 命令创建-> useradd或add ...
- 编写shell管理脚本(二)
8.1 先测试“/etc/vsftpd”.“/etc/hosts”是否为目录,并通过“$?”变量查看返回状态值,据此判断测试结果.[root@localhost ~]# [ -d /etc/vsft ...
随机推荐
- Mybatis resultMap灵活用法(使用子查询)
### 背景查询广州每个景点的总流量,和每个景点每日流量 #### 数据表 t_广州|唯一标识id|地点place|流量counts|日期date||:---:|:---:|:---:|:---:|| ...
- oc中文首字母排序
oc中文首字母排序 NSArray *arr2=@[@"小雨",@"安安",@"小风",@"荣荣",@"张涛& ...
- DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描
DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许进制转载 吐槽之后应该有所改了,该方式可以作为一种过渡方式 ...
- IDEA(2018.3.2)
N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjoid3UgYW5qdW4iLCJhc3NpZ25lZU5hbWUiO ...
- 抓包之Charles For Mac 4.0+破解版
前言:突然间发现好久没有写博客了,最近被公司的项目弄得脑壳疼
- 前端js转换时间戳为时间类型显示
//时间戳转换 function add0(m){return m<10?'0'+m:m } function formatDate(timestamp) { //timestamp是整数,否则 ...
- react-router-dom实现全局路由登陆拦截
相比与vue的路由集中式管理,能够很好的进行统一的路由操作,react的路由看起来更乱,想要进行像vue的全局路由管理不是那么得心应手.在我们的项目中,有很多页面是需要登陆权限验证的,最好的方式就是能 ...
- python中的super怎么用?
面向对象有这个强大特点和作用, 著名的三大特点:封装, 继承, 多态 这篇博客写的是super()的简单理解和使用 今天在读restframework的源码的时候, 发现源码中使用了super, 依以 ...
- .net第三方数据库物理卡号同步功能实现
本地数据库用的是Oracle,第三方数据库是SQL Server,连接字符串保存在web.config里面. 第三方数据库为增量,每次读取要记录读取的最大位置.我是保存在本地txt文件里面. //保存 ...
- 集合之Map
Map:存放键值对,根据键对象找对应的值对象.键不能重复!Map键不能重复,有唯一性,一般通过键找对应的的值Map集合的特点: 1.具有映射关系 2.两列 3.一列要唯一 一列可以重复 键类似于 Se ...