在mac或linux中获取组名的groupid的命令是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10910096/
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-08-06 06:40:48  来源:igfitidea点击:

What is the command to get groupid of a group name in mac or linux?

linuxmacosmacros

提问by Rajasekhar

How can I get group id of a group in mac os or linux?

如何在 mac os 或 linux 中获取组的组 ID?

ie., Command GroupName ==> should return the groupid

即,命令 GroupName ==> 应该返回 groupid

Eg:

例如:

Command staff   ==>   20

采纳答案by math

On Linux, you can use getent(1):

在 Linux 上,您可以使用getent(1)

$ getent group staff
staff:x:20:

If you only want 20:

如果你只想要 20 个:

$ getent group staff | cut -d: -f3
20

On OS X, you can use dscl(1):

在 OS X 上,您可以使用dscl(1)

$ dscl . -read /Groups/staff | awk '( == "PrimaryGroupID:") { print  }'
20

It can be easier to use this simple python command (using the grp library) to have the same result on both platforms:

使用这个简单的 python 命令(使用grp 库)可以更容易地在两个平台上获得相同的结果:

$ python -c 'import grp; print grp.getgrnam("staff").gr_gid'
20