1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Obtaining basic system info with Perl

Discussion in 'Perl' started by pradeep, May 4, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    How often have you wanted to find out the current user's name or system group within a shell script? Or wanted to get the current process ID?

    Well, if you're using Perl, it's nowhere near as difficult as you might think. That's because Perl comes with a library of functions designed specifically to provide user, group, and process information.

    Below are the important functions in this library, with recommendations on where each should be used and working code samples to help you on your way.

    Function: getpwnam($name)
    Explanation:
    This function returns the password file entry for the user $name.
    Use this function to retrieve information about a user, given the user's login name on the system.
    Example:
    Code:
     #!/usr/bin/perl
     
     # get user info
     ($name, $pass, $uid, $gid, $quota, $comment, $gcos, $dir, $shell, $expire) = getpwnam('pradeep');
     print "Name: $name \nUID: $uid \nShell: $shell\n";
     
    Function: getpwuid($id)
    Explanation:
    This function returns the password file entry for the user ID $id.
    Use this function to retrieve information about a user, given the user ID.
    Example:
    Code:
     #!/usr/bin/perl
     
     # get user info
     ($name, $pass, $uid, $gid, $quota, $comment, $gcos, $dir, $shell, $expire) = getpwuid(1000);
     print "Name: $name \nUID: $uid \nShell: $shell\n";
     
    Function: getpwent()
    Explanation:
    This function returns the next available line from the system password file.
    Use this function in a loop to process the system password file line by line.
    Example:
    Code:
    #!/usr/bin/perl
     
     # get user info
     while (($name, $pass, $uid, $gid, $quota, $comment, $gcos, $dir, $shell, $expire) = getpwent()) {
     print "$name \t $uid \t $dir\n";
     }
    Function: getgrnam($name)
    Explanation:
    This function returns the group file entry (including the member list) for the group $name.
    Use this function to retrieve information about a user, given the group name.
    Example:
    Code:
    #!/usr/bin/perl
     
     # get group info
     ($name, $passwd, $gid, $members) = getgrnam('wheel');
     print "$name \t $gid \t $members\n";
    Function: getgrgid($id)
    Explanation:
    This function returns the group file entry (including the member list) for the group ID $id.
    Use this function to retrieve information about a group, given the group ID.
    Example:
    Code:
    #!/usr/bin/perl
     
     # get group info
     ($name, $passwd, $gid, $members) = getgrgid(1);
     print "$name \t $gid \t $members\n";
    Function: getgrent()
    Explanation:
    This function returns the next available line from the system group file.
    Use this function in a loop to process the system's groups one after another.
    Example:
    Code:
    #!/usr/bin/perl
     
     # get group info
     while (($name, $passwd, $gid, $members) = getgrent()) {
     print "$name \t $gid \t $members\n";
     }
    Function: getlogin()
    Explanation:
    This function returns the name of the currently logged-in user.
    Use this function to identify which user is currently logged in and/or which user the script is currently running as.
    Example:
    Code:
    #!/usr/bin/perl
     
     # get logged-in user name
     print "Current user is " . getlogin();
    Function: getpgrp($id)
    Explanation:
    This function returns the process group for the PID $id. When the PID is 0, it returns the process group for the current process.
    Use this function to obtain the current process ID.
    Example:
    Code:
    #!/usr/bin/perl
     
     # get current process ID
     print "Current PID is " . getpgrp(0);
    Function: getppid()
    Explanation:
    This function returns the ID for the parent of the currently-executing process.
    Use this function to obtain the current process's parent ID (for interactive scripts, this is usually the PID of the controlling terminal).
    Example:
    Code:
    #!/usr/bin/perl
     
     # get parent process ID
     print "Current process' parent PID is " . getppid();
     
  2. tarunt

    tarunt New Member

    Joined:
    Feb 4, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    programmer
    Location:
    india, u.p
    useful functions
     

Share This Page