【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 ...
随机推荐
- 用java集合模拟登录和注册功能
package com.linkage.login; import java.util.HashMap;import java.util.Iterator;import java.util.Map;i ...
- 破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988)
破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988) 题意描述 你在输入文章的时候,键盘上的Home键和End键出了问题,会不定时的按下. ...
- hdu_5187_zhx's contest
Problem Description As one of the most powerful brushes, zhx is required to give his juniors n probl ...
- MySQL高可用之MGR安装测试
Preface We've learned the machenism of MGR yesterday,Let's configurate an environment and have s ...
- linux 安装 node 环境
本篇学习的分享主要说在linux 安装 node 环境,个人也是在腾讯云的实验室课程学习的,这里只是个人的一个学习记录, 大家也可以去腾讯的实验室来体验一下,教程十分详细易学. 1 .安装 Node. ...
- shell编程基础-01
1.1 前言 1.1.1 为什么学Shell Shell脚本语言是实现Linux/UNIX系统管理及自动化运维所必备的重要工具Linux/UNIX系统的底层及基础应用软件的核心大都涉及Shell脚本的 ...
- 微信小程序快速转成百度小程序的方法
1.安装Node.js(>8.5.0)https://nodejs.org/ npm升级到最新版本的方法:npm install -g npm自动更新到最新版本 2.配置cnpm:在命令行中输入 ...
- Hive操作之向分区表中导入数据的语义错误
1.建完分区表之后,向表中导入数据 命令为: load data local inpath '/home/admin/Desktop/2015082818' into table db_web_dat ...
- ECharts使用过程遇到的问题汇总
获取ECharts npm install echarts --save 自定义构建ECharts 我选用的是常用版的echarts/dist/echarts.common.js 在我的项目根目录下m ...
- Leecode刷题之旅-C语言/python-206反转链表
/* * @lc app=leetcode.cn id=206 lang=c * * [206] 反转链表 * * https://leetcode-cn.com/problems/reverse-l ...