当前位置: 首页>>代码示例>>用法及示例精选>>正文


Perl getpwent用法及代码示例


描述

此函数返回 /etc/passwd 文件中的下一个密码条目。这与 setpwent 和 endpwent 函数结合使用以迭代密码文件。在列表上下文中,返回

($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwent;

用法

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

getpwent

返回值

此函数返回标量上下文中的用户名和列表上下文中的用户记录(名称、密码、用户 ID、组 ID、引号、评论、真实姓名、主目录、shell)。

示例

以下是显示其基本用法的示例代码 -

#!/usr/bin/perl

while(($name, $passwd, $uid, $gid, $quota, $comment, $gcos,
   $dir, $shell) = getpwent()) {
   print "Name = $name\n";
   print "Password = $passwd\n";
   print "UID = $uid\n";
   print "GID = $gid\n";
   print "Quota = $quota\n";
   print "Comment = $comment\n";
   print "Gcos = $gcos\n";
   print "HOME DIR = $dir\n";
   print "Shell = $shell\n";
}

执行上述代码时,会产生以下结果 -

Name = root
Password = x
UID = 0
GID = 0
Quota = 
Comment = 
Gcos = root
HOME DIR = /root
Shell = /bin/bash
Name = bin
Password = x
UID = 1
GID = 1
Quota = 
Comment = 
Gcos = bin
HOME DIR = /bin
Shell = /sbin/nologin
.
.
.
Name = com
Password = x
UID = 501
GID = 501
Quota = 
Comment = 
Gcos = 
HOME DIR = /home/com
Shell = /bin/bash
Name = railo
Password = x
UID = 497
GID = 495
Quota = 
Comment = 
Gcos = 
HOME DIR = /opt/railo

相关用法

注:本文由纯净天空筛选整理自 Perl getpwent Function。非经特殊声明,原始代码版权归原作者所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。