【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 ...
随机推荐
- Hibernate连接各种数据库的配置
转自CSDN shl7765856的专栏:http://blog.csdn.net/shl7765856/article/details/7411987 目录: 1.MySql连接配置 2.Sql S ...
- Oracle特性总结
最近开发项目使用了Oracle,根据总体架构一开始选择使用Mybatis,发现核心模块用Mybatis效率不够,切换到jdbc实现,效率大增.Oracle可是个庞然大物,特性多多,丝毫不能马虎,否则很 ...
- 不用注解,获取spring容器里的bean(ApplicaitonContext)
以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext. 使用方法:SpringContextHolder.getBe ...
- #leetcode刷题之路35-搜索插入位置
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置.你可以假设数组中无重复元素. 示例 1:输入: [1,3,5,6], 5输出: ...
- zabbix基本监控各指标简解
监控项目及使用模板 监控http和https: Template App HTTP Service Template App HTTPS Service 监控cpu,内存,网络等: Templ ...
- ElasticSearch : APT-GET安装方式
Ubuntu下安装ES 新建非ROOT管理员用户 安装和配置JDK 打开下面的地址 https://www.elastic.co/guide/en/elasticsearch/reference/cu ...
- SHOPEX快递物流单号查询插件
本SHOPEX快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅急送快递.德邦物流.百世快递.汇通快递.中通快递.天天快递等知 ...
- Django项目中关于redis包版本的坑
1.环境 python:3.6 django:1.11.8 redis:3.2.1 2.遇到的问题 报错:redis.exceptions.DataError: Invalid input of ty ...
- SaltStack error: No module named 'salt'
启动saltstack的时候出现下面的错误 问题原因 是因为我在centos7中安装了多版本的python导致的 解决方案 将文件下面文件首行更改成python2 [root@saltstack-12 ...
- Java学习笔记二十:Java中的内部类
Java中的内部类 一:什么是内部类: (1).什么是内部类呢? 内部类( Inner Class )就是定义在另外一个类里面的类.与之对应,包含内部类的类被称为外部类. (2).那为什么要将一个类定 ...