Perl getpwuid 函式



描述

此函式在列表上下文中返回一個欄位列表,這些欄位是從 `/etc/passwd` 檔案中提取的,基於由 EXPR 指定的使用者名稱。它通常像這樣使用:

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

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

語法

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

getpwuid EXPR

返回值

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

示例

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

#!/usr/bin/perl

($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid(0);
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
廣告