perl function getpwent
The getpwent
function is a built-in Perl function that is used to retrieve information about the next user account from the system's password file. It returns an array containing information about the user, such as the user's login name, encrypted password, numeric user ID, numeric group ID, and home directory.
Here's an example that demonstrates how to use getpwent
:
#!/usr/bin/perl # Open the password file open(PASSWD, "< /etc/passwd"); # Read the user accounts one by one while ($userinfo = getpwent()) { # Print the login name and home directory print "Login Name: $userinfo->[0]\n"; print "Home Directory: $userinfo->[7]\n"; } # Close the password file endpwent();
In this example, we first open the system's password file using the open
function. We then use a while
loop to call getpwent
repeatedly until we have retrieved information about all user accounts in the file. Inside the loop, we print the login name and home directory of each user using the array returned by getpwent
.
Finally, we close the password file using the endpwent
function.