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 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. | Code: #!/usr/bin/perl # get user info ($name, $pass, $uid, $gid, $quota, $comment, $gcos, $dir, $shell, $expire) = getpwnam('victor'); print "Name: $name \nUID: $uid \nShell: $shell\n"; Output: Name: victor UID: 1000 Shell: /bin/bash |
| getpwuid($id) | 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. | 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"; Output: Name: victor UID: 1000 Shell: /bin/bash |
| getpwent() | 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. | 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"; } Output: root 0 /root bin 1 /bin daemon 2 /sbin adm 3 /var/log lp 4 /var/spool/lpd sync 5 /sbin shutdown 6 /sbin halt 7 /sbin mail 8 / |
| getgrnam($name) | 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. | Code: #!/usr/bin/perl # get group info ($name, $passwd, $gid, $members) = getgrnam('wheel'); print "$name \t $gid \t $members\n"; Output: wheel 10 root |
| getgrgid($id) | 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. | Code: #!/usr/bin/perl # get group info ($name, $passwd, $gid, $members) = getgrgid(1); print "$name \t $gid \t $members\n"; Output: bin 1 root bin daemon |
| getgrent() | 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. | Code: #!/usr/bin/perl # get group info while (($name, $passwd, $gid, $members) = getgrent()) { print "$name \t $gid \t $members\n"; } Output: root 0 root bin 1 root bin daemon daemon 2 root bin daemon sys 3 root bin adm adm 4 root adm daemon tty 5 disk 6 root adm |
| getlogin() | 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. | Code: #!/usr/bin/perl # get logged-in user name print "Current user is " . getlogin(); Output: Current user is joe |
| getpgrp($id) | 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. | Code: #!/usr/bin/perl # get current process ID print "Current PID is " . getpgrp(0); Output: Current PID is 403 |
| getppid() | 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). | Code: #!/usr/bin/perl # get parent process ID print "Current process' parent PID is " . getppid(); Output: Current process' parent PID is 111 |



