简体   繁体   English

用于添加和删除用户的 Bash 脚本

[英]Bash script to add and remove users

I am a beginner in bash scripting and I have created a bash script to add users and remove users on Linux.我是 bash 脚本的初学者,我创建了一个 bash 脚本来在 Linux 上添加用户和删除用户。 But since I am facing some issues with the script not really major issues but would be helpful if anyone could point me how to improve the script and the worst practice I am doing the script would be helpful但是由于我面临的脚本问题并不是真正的主要问题,但是如果有人能指出我如何改进脚本和最坏的做法,我正在编写脚本会有所帮助

however the problem I have noticed is that the script takes -a to add a user -d to remove user and -h to get help the -a flag as 2 optional arguments -p for password and -s for shell so the command would be但是我注意到的问题是脚本需要 -a 添加用户 -d 删除用户和 -h 获取帮助 -a 标志作为 2 个可选参数 -p 用于密码和 -s 用于 shell 所以命令将是

./useradd.sh -a user -p password -s shell

this works as expected and the user is added to the system but the problem I am facing is that if I do not enter -a flag and specify the -s and -p flag the script is just exited I want to show a clear idea to the user why it exited and there is so many such errors I am assuming but I have not tested it out so much any help would be appreciated, so here is my script这按预期工作,用户已添加到系统中,但我面临的问题是,如果我不输入 -a 标志并指定 -s 和 -p 标志,脚本刚刚退出,我想向您展示一个清晰的想法用户为什么退出,我假设有很多这样的错误,但我还没有测试过这么多,任何帮助都将不胜感激,所以这是我的脚本

https://github.com/abhijith7025/bash_scripts/blob/master/useradd.sh https://github.com/abhijith7025/bash_scripts/blob/master/useradd.sh

... I have created a bash script to add users and remove users on Linux. ...我创建了一个 bash 脚本来在 Linux 上添加用户和删除用户。

You might want to reconsider this, given that Linux has separate commands for these two operations.鉴于 Linux 对这两个操作有单独的命令,您可能需要重新考虑这一点。 There's a lot more to creating a user than there is is getting rid of one, so you may be asking for trouble trying to conjoin the two.创建一个用户比删除一个用户要多得多,因此您可能会在尝试将两者结合起来时遇到麻烦。

Anyway:反正:

Your case statement has no "other" branch (" * ) ").您的 case 语句没有“其他”分支(“ * ) ”)。 This may be why you're getting no errors when "misusing" your script.这可能就是“误用”脚本时没有错误的原因。

case $opt in 
  a )  
    ;; 
  d )  
    ;; 
  h )  
    ;; 
  * )
     usage 
     exit 1 
     ;; 
esac 

Other things to look out for:其他需要注意的事项:

user add is, perhaps, a poor name for a script that can delete users. user add可能是一个可以删除用户的脚本的糟糕名称。

You allow a shell to be specified as an argument, but you don't check to see if that exists .您允许将 shell 指定为参数,但不检查它是否存在

It's conventional for Linux commands to operate on a "thing" or list of "things" and for that operation to be qualified by options that [usually] precede the thing(s). Linux 命令通常对“事物”或“事物”列表进行操作,并且该操作由 [通常] 位于事物之前的选项限定。 Thus, your script might be better invoked like this:因此,您的脚本可能会像这样更好地调用:

./manage_user  -a  -p password  -s shell_exe  user1 
                |   |            |            |
                |   |            |            User name 
                |   |            Shell for User
                |   Password for User
                Add a User

./manage_user  -d  user2 
                |  |
                |  User name 
                Delete a User

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM