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.
This
document lists (See Table A) the
important functions in this library, with recommendations on where each should
be used and working code samples to help you on your way.
Table A
Function |
Explanation |
Example |
getpwnam($name) |
This Use this function to retrieve information about a |
Code: #!/usr/bin/perl # get user info Output: |
getpwuid($id) |
This Use this function to retrieve information about a |
Code: #!/usr/bin/perl # get user info Output: |
getpwent() |
This Use this function in a loop to process the system |
Code: #!/usr/bin/perl # get user info Output: |
getgrnam($name) |
This Use this function to retrieve information about a |
Code: #!/usr/bin/perl # get group info Output: |
getgrgid($id) |
This Use this function to retrieve information about a |
Code: #!/usr/bin/perl # get group info Output: |
getgrent() |
This Use this function in a loop to process the system’s |
Code: #!/usr/bin/perl # get group info Output: |
getlogin() |
This Use this function to identify which user is |
Code: #!/usr/bin/perl # get logged-in user name Output: |
getpgrp($id) |
This Use this function to obtain the current process ID. |
Code: #!/usr/bin/perl # get current process ID Output: |
getppid() |
This Use this function to obtain the current process’s |
Code: #!/usr/bin/perl # get parent process ID Output: |