【shell脚本】批量创建和删除用户

简介:

一、编写shell脚本批量添加用户

    实现方法:判断用户是否存在,存在则返回错误提示,同时判断用户文件是否存在,不存在则退出

1、创建添加用户脚本

[root@localhost ~]# vim useradd.sh 
#!/bin/bash
if [ $# -eq 0 ];then
        echo "你没有输入任何文件!"
        exit 1
fi

if [ ! -f $1 ];then
        echo "输入有误!"
        exit 2
fi

for user in `cat $1` 
do
        id $user &>/dev/null
        if [ $? -eq 0 ];then
                echo "用户$user已存在!"
        else
                useradd -s /sbin/nologin $user
                echo "password" | passwd --stdin $user
                echo "用户$user创建成功。"
        fi
done

2、检查脚本并添加可执行权限

[root@localhost ~]# sh -n useradd.sh 
[root@localhost ~]# chmod +x useradd.sh 


3、添加用户

[root@localhost ~]# ./useradd.sh 
你没有输入任何文件!
[root@localhost ~]# ./useradd.sh user.txt 
更改用户 user1 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
用户user1创建成功。
更改用户 user2 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
用户user2创建成功。
更改用户 user3 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
用户user3创建成功。
更改用户 user4 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
用户user4创建成功。
更改用户 user5 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
用户user5创建成功。


二、删除用户

1、编写删除脚本

[root@localhost ~]# vim userdel.sh

#!/bin/bash
#userdel
if [ $# -eq 0 ];then
        echo "你没有输入文件!"
        exit 1
fi

if [ ! -f "$1" ];then
        echo "输入有误!"
        exit 2
fi

for user in `cat $1`
do
        id $user &>/dev/null
        if [ $? -eq 0 ];then
                userdel -r $user &>/dev/null
                echo "删除$user成功!"
        else
                echo "用户不存在!"
        fi
done

[root@localhost ~]# sh -n userdel.sh 
[root@localhost ~]# chmod a+X userdel.sh 


2、删除用户

[root@localhost ~]# ./userdel.sh 111
输入有误!
[root@localhost ~]# ./userdel.sh user.txt 
删除user1成功!
删除user2成功!
删除user3成功!
删除user4成功!
删除user5成功!
[root@localhost ~]# ./userdel.sh user.txt 
用户不存在!
用户不存在!
用户不存在!
用户不存在!
用户不存在!



while read user
do
        id $user &>/dev/null
        if [ $? -eq 0 ];then
                echo "用户$user已存在!"
        else
                useradd -s /sbin/nologin $user
                echo "password" | passwd --stdin $user
                echo "用户$user创建成功。"
        fi
done < $1




本文转自 HMLinux 51CTO博客,原文链接:http://blog.51cto.com/7424593/1728017
相关文章
|
15天前
|
JavaScript 前端开发 Shell
Shell 脚本编程保姆级教程(上)
Shell 脚本编程保姆级教程(上)
|
10天前
|
Shell Linux C语言
|
12天前
|
Shell 网络安全
shell脚本 配饰ssh
【7月更文挑战第15天】
16 4
|
16天前
|
网络协议 Shell Linux
Shell脚本配置Centos静态ip地址
这是一个用于在CentOS上设置静态IP的Shell脚本摘要: - 脚本交互式获取用户输入的IP地址、子网掩码、网关和DNS。 - 使用`sed`命令动态更新`/etc/sysconfig/network-scripts/ifcfg-ENS33`配置文件。 - 修改`BOOTPROTO`为`static`,并设置IP、NETMASK、GATEWAY和DNS1字段。 - 用`systemctl restart network`重启网络服务。 - 提示用户新配置的静态IP信息。
|
22天前
|
Shell Linux
Linux Shell 脚本入门教程:开启你的自动化之旅
Shell是一种计算机程序,它充当了用户与操作系统之间的接口。在Linux系统中,Shell允许用户通过命令行界面(CLI)来控制计算机。Shell脚本则是一种使用Shell语言编写的脚本,它可以自动执行一系列的命令,帮助用户实现任务自动化,提高工作效率。
|
28天前
|
Shell
蓝易云 - 简单shell脚本的编写教程
以上就是编写一个基本Shell脚本的步骤。当然,Shell脚本可以做的远不止这些,你可以使用变量,控制结构(如if语句和循环),以及各种Shell命令和功能来编写更复杂的脚本。
25 1
|
15天前
|
Shell
Shell 脚本编程保姆级教程(下)
Shell 脚本编程保姆级教程(下)
|
1月前
|
监控 Shell Linux
Linux的Shell脚本详解
Linux的Shell脚本详解
|
1月前
|
Shell
shell脚本
shell脚本
23 2
|
24天前
|
移动开发 网络协议 Shell
查看IP访问量的shell脚本汇总
查看IP访问量的shell脚本汇总