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
Is user in dialout group?
提问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
dialout
is 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 groups
command. 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 lprod
user):
您正在寻找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 中的正则表达式进行比较。