Perl getpwnam 函式



描述

此函式返回從 /etc/passwd 檔案中提取的一系列欄位,在列表上下文中,基於由 EXPR 指定的使用者名稱稱。通常用法如下:

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

在標量上下文中,返回數字使用者 ID。如果您嘗試訪問整個 /etc/passwd 檔案,則應使用 getpwent 函式。如果您想按使用者 ID 訪問詳細資訊,請使用 getpwuid。

語法

以下是此函式的簡單語法:

getpwnam EXPR

返回值

此函式在標量上下文中返回使用者 ID,在列表上下文中返回使用者記錄(名稱、密碼、使用者 ID、組 ID、配額、註釋、真實姓名、主目錄、shell)。

示例

以下示例程式碼展示了其基本用法:

#!/usr/bin/perl

($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwnam("root");
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
perl_function_references.htm
廣告

© . All rights reserved.