Perl setgrent 函数

  • 描述

    setgrent 此函数将枚举设置(或重置)到组条目集的开头。该函数应在首次调用getgrent之前调用。
  • 句法

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

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

    以下是显示其基本用法的示例代码-
     
    while( ($name,$passwd,$gid,$members) = getgrent() ) {
       print "Name  = $name\n";
       print "Password  = $passwd\n";
       print "GID  = $gid\n";
       print "Members  = $members\n";
    }
    
    setgrent();    # Set the beginnging of the group database;
    
    print "Again reading /etc/passwd file\n";
    while( ($name,$passwd,$gid,$members) = getgrent() ) {
       print "Name  = $name\n";
       print "Password  = $passwd\n";
       print "GID  = $gid\n";
       print "Members  = $members\n";
    }
    
    endpwent;  #claose the database;
    
    尝试一下
    执行以上代码后,将产生以下结果--
    
    Name = root
    Password = x
    GID = 0
    Members = 
    Name = daemon
    Password = x
    GID = 1
    Members = 
    Name = bin
    Password = x
    GID = 2
    Members = 
    Name = sys
    Password = x
    GID = 3
    Members = 
    Name = adm
    Password = x
    GID = 4
    Members = 
    Name = tty
    Password = x
    GID = 5
    Members = 
    Name = disk
    Password = x
    GID = 6
    Members = 
    Name = lp
    Password = x
    GID = 7
    Members = 
    Name = mail
    Password = x
    GID = 8
    Members = 
    Name = news
    Password = x
    GID = 9
    Members = 
    Name = uucp
    Password = x
    GID = 10
    Members = 
    Name = man
    Password = x
    GID = 12
    Members = 
    Name = proxy
    Password = x
    GID = 13
    Members = 
    Name = kmem
    Password = x
    GID = 15
    Members = 
    Name = dialout
    Password = x
    GID = 20