Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Perl中获取当前登录用户的主要组和其他组?_Perl - Fatal编程技术网

如何在Perl中获取当前登录用户的主要组和其他组?

如何在Perl中获取当前登录用户的主要组和其他组?,perl,Perl,据我所知,我可以简单地写 $user = getlogin(); 但是组处理函数似乎不能接受用户名/用户ID作为参数。我真的应该遍历所有/etc/group文件行并从中解析组名吗?无需解析系统文件,在类似UNIX的操作系统上,我将使用内置接口来进行getpwuid和getgrgid系统调用: use strict; use warnings; # use $< for the real uid and $> for the effective uid my ($user, $pa

据我所知,我可以简单地写

$user = getlogin();

但是组处理函数似乎不能接受用户名/用户ID作为参数。我真的应该遍历所有/etc/group文件行并从中解析组名吗?

无需解析系统文件,在类似UNIX的操作系统上,我将使用内置接口来进行getpwuid和getgrgid系统调用:

use strict;
use warnings;

# use $< for the real uid and $> for the effective uid
my ($user, $passwd, $uid, $gid ) = getpwuid $< ;
my $group = getgrgid $gid ;

printf "user: %s (%d), group: %s (%d)\n", $user, $uid, $group, $gid;
也可以,因为$(和$)已经包含GID和EGID

最后是POSIX模块中定义的getgroups函数

use POSIX qw(getgroups)
正如dsw所建议的,如果您的操作系统(与Linux不同)支持同时拥有多个活动组,那么还应该允许您获取辅助组

查找非活动辅助组可能确实需要解析/etc/group文件, 它可以直接使用,也可以通过getgrend内置和标准User::grent模块的组合使用。

您应该使用。更具体地说,这里包含的
getpw*
getgr*
getu*
函数

use POSIX qw(getgroups)