![]() |
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
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
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
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
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
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
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
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
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
|
Re: Obtaining basic system info with Perl
useful functions
|
| All times are GMT +5.5. The time now is 22:55. |