博客
关于我
linux 脚本编写实战:通过账号列表文本快速添加和删除用户
阅读量:361 次
发布时间:2019-03-04

本文共 837 字,大约阅读时间需要 2 分钟。

                linux脚本实战:通过账号列表文本快速添加和删除用户

该脚本需要一个写有需要添加的账号列表的文本文档,可放在任意位置,一行是一个账号名称,文件内的所有账号通过编写的脚步快速添加到Linux中,该脚本是带参脚本,如没有使用参数提示缺少参数,如参数不是提供账号的文本文件则提示文件未找到。

以上为该脚本的需求,下面开始编写该简单的脚步。

#!/bin/bashif [ $# -eq 0 ];thenecho "please use xargs"#提示请使用参数,该脚本是带参才可运行exit 1#给定一个返回码,如果脚本运行到这fiif [ ! -f $1 ];thenecho "file is not found"exit 2#给定一个返回码,如果脚本运行到这fiwhile read linedouseradd -s /sbin/login  $line &> /dev/null#这一行是添加用户命令,做了静默运行处理,&》 /dev/null 所有结果不显示#-s 是添加用户命令指定用户的bin,该命令可以指定用户目录位置,用户id和用户所属zuid等#根据实际需要吧done<$1

 假设写有账号的文本文档为user.txt,该文件在/tmp 文件夹下,脚本名称为adduser.sh,脚本存放在root目录下, 运行脚本命令为:

bash adduser.sh /tmp/user.txt

脚本赋权运行后,tail /etc/passwd 查看是否添加成功,如果需要删除以上所建立的用户呢??

脚本中的命令改为userdel即可。

#!/bin/bashif [ $# -eq 0 ];thenecho "please use xargs"exit 1fiif [ ! -f $1 ];thenecho "file is not found"exit 2fiwhile read linedouserdel  $line &> /dev/nulldone<$1

 

 

转载地址:http://tlkr.baihongyu.com/

你可能感兴趣的文章
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>