bash 用户是否在拨出组中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36360565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:26:45  来源:igfitidea点击:

Is user in dialout group?

bash

提问by Jesse_Pinkman

In my script I need to find out whether user is in dialout group and if he is not, add him to the group. I have found out how to add him but is there a way how to know if he is in the group ?

在我的脚本中,我需要找出用户是否在拨出组中,如果他不在,则将他添加到组中。我已经找到了添加他的方法,但是有没有办法知道他是否在组中?

采纳答案by heemayl

You can do this in both ways:

您可以通过两种方式执行此操作:

  • Check all groups a specific user is member of and check if dialoutis there:

    id username
    

    Or as Wolf said already:

    groups username
    
  • Check all members of the specific group (dialout) and check if the user is in the list:

    getent group dialout
    
  • 检查特定用户所属的所有组并检查是否dialout存在:

    id username
    

    或者正如沃尔夫已经说过的那样:

    groups username
    
  • 检查特定组 ( dialout) 的所有成员并检查用户是否在列表中:

    getent group dialout
    

回答by Wolf

You are looking for the groupscommand. Here's an example of me using it on a server I just happened to be talking to at the moment (I'm logged in as the lproduser):

您正在寻找groups命令。这是我在当时碰巧与之交谈的服务器上使用它的示例(我以lprod用户身份登录):

[lprod@milazgit01 ~] $ groups wolf
wolf : prod tools

Then you can compare that to a regular expression in whatever shell you are using.

然后,您可以将其与正在使用的任何 shell 中的正则表达式进行比较。