Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   Obtaining basic system info with Perl (http://www.go4expert.com/articles/obtaining-basic-info-perl-t4133/)

pradeep 4May2007 14:51

Obtaining basic system info with Perl
 
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: Perl

#!/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: Perl

#!/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: Perl

#!/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: Perl

#!/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: Perl

#!/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: Perl

#!/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: Perl

#!/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: Perl

#!/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: Perl

#!/usr/bin/perl
 
 # get parent process ID
 print "Current process' parent PID is " . getppid();


tarunt 5Feb2010 10:46

Re: Obtaining basic system info with Perl
 
useful functions


All times are GMT +5.5. The time now is 09:30.