當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Perl getgrent用法及代碼示例



描述

此函數迭代 /etc/group 文件中的條目。在列表上下文中返回以下內容 -

($name, $passwd, $gid, $members)

$members 標量包含作為組成員的登錄名的空格分隔列表。僅在標量上下文中使用時才返回組名。在 Windows 下,考慮使用 Win32API::Net 模塊。

用法

以下是此函數的簡單語法 -

getgrent

返回值

此函數返回標量上下文和列表上下文中的組名稱(名稱、密碼、組 ID 和成員列表。

示例

以下是顯示其基本用法的示例代碼 -

#!/usr/bin/perl

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

執行上述代碼時,會產生以下結果 -

Name  = root
Password  = x
GID  = 0
Members  = root
Name  = bin
Password  = x
GID  = 1
Members  = root bin daemon
Name  = daemon
Password  = x
GID  = 2
Members  = root bin daemon
Name  = sys
Password  = x
GID  = 3
Members  = root bin adm
Name  = adm
Password  = x
GID  = 4
Members  = root adm daemon
Name  = tty
Password  = x
GID  = 5
Members  = 
.
.
.
Name  = fuse
Password  = x
GID  = 496
Members  = 
Name  = kvm
Password  = x
GID  = 36
Members  = qemu
Name  = qemu
Password  = x
GID  = 107
Members  = 
Name  = com
Password  = x
GID  = 501
Members  = 
Name  = webgrp
Password  = x
GID  = 502
Members  = com
Name  = railo
Password  = x
GID  = 495
Members  = 

相關用法


注:本文由純淨天空篩選整理自 Perl getgrent Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。