当前位置:首页 » Perl » Perl getpwuid()函数

Perl getpwuid()函数

perl getpwuid()函数实例代码在线教程 - 在列表上下文中,返回字段列表,如在/etc/passwd文件中提取,根据EXPR通过指定的用户名。

语法

getpwuid EXPR


定义和使用

在列表上下文中,返回字段列表,如在/etc/passwd文件中提取,根据EXPR通过指定的用户名。 它通常是这样的:

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

在标量上下文中返回用户名。如果要尝试访问整个/etc/passwd文件, 应该使用getpwent函数,如果想访问用户名,那么可以使用getpwnam

返回值

  • 在标量上下文中的用户名称

  • 在列表上下文中的用户记录(name, password, user ID, group ID, quote, comment, real name, home directory, shell)

实例

试试以下实例:

#!/usr/bin/perl
#by www.gitbook.net


   ($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";


// It will produce following result

Name = root
Password = *******
UID = 0
GID = 0
Quota =
Comment =
Gcos = root
HOME DIR = /root
Shell = /bin/bash