21 July 2010

Perl: Finding higest UID from passwd file



#! /usr/bin/perl -w
use strict;

#
# Program to find the maximum
# User ID from /etc/passwd.
#

use constant CONF_FILE => "/etc/passwd"; # PATH of the file
use constant SEPERATOR => ":"; # Record seperator
use constant COLTOSPLIT => 4; # Number of columns to split.
use constant COL_USRID => 2; # User id column number.
my $userID = 0; # Temp variable for finding max UID.

#
# Open /etc/passwd file.
# If any error occurs while
# reading file terminate program
# print the error message occured.
#

open (FILE, CONF_FILE) or die ($!);
my @data = ;

#
# Read one line, split the line using SPERATOR.
# Check value at COL_USRID column.
# Store the maximum UID in userID.
#
foreach my $line (@data) {
chomp($line);
my @user = split(SEPERATOR, $line, COLTOSPLIT);
if ($userID < $user[COL_USRID]) {
$userID = $user[COL_USRID];
}
}
#
# Close the file handler.
# close(FILE);
print "Higest user ID : " , $userID;
print "\n"; exit 0;

No comments:

Post a Comment