Perl-getpwent

提供:Dev Guides
移動先:案内検索

Perl getpwent関数

説明

この関数は、/etc/passwdファイルから次のパスワードエントリを返します。 これをsetpwentおよびendpwent関数と組み合わせて使用​​して、パスワードファイルを反復処理します。 リストコンテキストでは、戻ります

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

構文

以下は、この関数の簡単な構文です-

getpwent

戻り値

この関数は、スカラーコンテキストでユーザー名を返し、リストコンテキストでユーザーレコード(名前、パスワード、ユーザーID、グループID、引用、コメント、実名、ホームディレクトリ、シェル)を返します。

以下は、その基本的な使用法を示すコード例です-

#!/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