目录

getpwnam

描述 (Description)

此函数返回列表上下文中的字段列表,从/ etc/passwd文件中提取,基于EXPR指定的用户名。 它通常像这样使用 -

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

在标量上下文中,返回数字用户ID。 如果您尝试访问整个/ etc/passwd文件,则应使用getpwent函数。 如果要按用户标识访问详细信息,请使用getpwuid。

语法 (Syntax)

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

getpwnam EXPR

返回值 (Return Value)

此函数在列表上下文中返回标量上下文中的用户ID和用户记录(名称,密码,用户ID,组ID,引用,注释,实名,主目录,shell)。

例子 (Example)

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

#!/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
↑回到顶部↑
WIKI教程 @2018