Perl setpwent 函数

  • 描述

    setpwent 将枚举设置(或重置)到密码条目集的开头。应该在第一次调用getpwent之前调用此函数。
  • 句法

    以下是此函数的简单语法-
    
    setpwent
    
  • 返回值

    此函数不返回任何值。
  • 示例

    以下是显示其基本用法的示例代码-
     
    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";
    }
    
    setpwent() ; # Rewind the databse /etc/passwd
    
    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";
    }
    
    endpwent(); # Closes the database;
    
    尝试一下
    执行以上代码后,将产生以下结果--
    
    Name = root
    Password = x
    UID = 0
    GID = 0
    Quota = 
    Comment = 
    Gcos = root
    HOME DIR = /root
    Shell = /bin/bash
    Name = daemon
    Password = x
    UID = 1
    GID = 1
    Quota = 
    Comment = 
    Gcos = daemon
    HOME DIR = /usr/sbin
    Shell = /usr/sbin/nologin
    Name = bin
    Password = x
    UID = 2
    GID = 2
    Quota = 
    Comment = 
    Gcos = bin
    HOME DIR = /bin
    Shell = /usr/sbin/nologin
    Name = sys
    Password = x
    UID = 3
    GID = 3
    Quota = 
    Comment = 
    Gcos = sys
    HOME DIR = /dev
    Shell = /usr/sbin/nologin
    Name = sync
    Password = x
    UID = 4
    GID = 65534
    Quota = 
    Comment = 
    Gcos = sync
    HOME DIR = /bin
    ............