Perl getgrnam 函数



说明

此函数通过组名查找组文件条目。在列表上下文中返回以下内容 ($name, $passwd, $gid, $members)

$members 标量包含属于该组的登录名(以空格分隔)。在标量上下文中返回组名。有关检索整个组文件的效率更高的方法,请参见 getgrent。在 Windows 下,建议使用 Win32API::Net 模块。

语法

以下是此函数的简单语法 −

getgrnam NAME

返回值

此函数在 scalr 上下文中返回组名,在列表上下文中返回名称、密码、组 ID 和成员列表。

示例

以下示例代码显示了基本用法 −

#!/usr/bin/perl

while( ($name,$passwd,$gid,$members) = getgrent() ) {
   ($name,$passwd,$gid,$members) = getgrnam $name;
   print "Name  = $name\n";
   print "Password  = $passwd\n";
   print "GID  = $gid\n";
   print "Members  = $members\n";
}

执行上述代码后,将产生以下结果 −

Name  = root
Password  = x
GID  = 0
Members  = 
Name  = bin
Password  = x
GID  = 1
Members  = 
Name  = daemon
Password  = x
GID  = 2
Members  = 
Name  = sys
Password  = x
GID  = 3
Members  = 
Name  = adm
Password  = x
GID  = 4
Members  = 
Name  = tty
Password  = x
GID  = 5
Members  = 
Name  = disk
Password  = x
GID  = 6
Members  = 
.
.
.
.
.
Name  = apache
Password  = x
GID  = 48
Members  = 
Name  = rexx
Password  = x
GID  = 1001
Members  = 
Name  = objc
Password  = x
GID  = 1002
Members  = 
perl_function_references.htm
广告