Did you know LQ has a Linux Hardware Compatibility List?
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-23-2013, 07:33 AM   #1
Registered: Jul 2011
Location: Melbourne, AU
Distribution: Centos 5
Posts: 43

Rep: Reputation: Disabled
Cool perl script to rationalize /etc/groups

Hi all,

I am trying to write a simple perl script to optimize my /etc/groups, to make sure that it doesn't contain any members that don't have corresponding /etc/passwd entries.

here's what I have so far, but it all seems a little cumbersome:

open(F, "/etc/passwd");
@passwd = <F>;
open(F, "/etc/group");
@group = <F>;

for ($i=0; $i<$#group; $i++) {
        $line = $group[$i];
        $line =~ /^(..*):(..*):(..*):(..*)$/;
        ($group, undef, undef, $members) = ($1, $2, $3, $4);
        @members = split(/,/, $members);
        foreach $m (@members) {
                printf("%s\n", $m) unless grep(/^$m:/, @passwd);
Is there a neater way of doing this in perl?

(PS: it needs to be in perl, as it's also for my programming class )

Last edited by scottmusician; 06-23-2013 at 08:35 AM.
Old 06-23-2013, 09:25 AM   #2
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 6,498

Rep: Reputation: 1765Reputation: 1765Reputation: 1765Reputation: 1765Reputation: 1765Reputation: 1765Reputation: 1765Reputation: 1765Reputation: 1765Reputation: 1765Reputation: 1765
yes, you can try to use a hash:
store all the usernames in a hash, %users (values are not important, we need to keys only).
and you can write your search as: grep (! exists $users{$_}, split(/,/, $members));
Not tested, there can be other possibilities, for example using map.
Old 06-25-2013, 02:05 AM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.7, Centos 5.10
Posts: 16,634

Rep: Reputation: 2149Reputation: 2149Reputation: 2149Reputation: 2149Reputation: 2149Reputation: 2149Reputation: 2149Reputation: 2149Reputation: 2149Reputation: 2149Reputation: 2149
1. always use -w & strict
#!/usr/bin/perl -w
use strict;
2. use 3 arg open and always close files you've opened asap (especially key ones like passwd!) and check open/close worked ok
open(KFILE, "<", "kfile.txt" ) or die "Can't open kfile: $!\n";
@passwd = <F>;
close(KFILE) or die "Can't close kfile: $!\n";
& prefer chomp() to chop().


groups, hosts, networking, perl, programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] dnsmasq: how to prevent upstream queries for hosts in /etc/hosts? catkin Linux - Networking 2 02-18-2013 10:50 AM
Script for hosts, numbers of hosts and users connected to squid server arunabh_biswas Programming 5 08-28-2010 04:11 AM
Bash/Perl script - provide list of hosts to choose from and ssh to chosen host loadedmind Programming 4 04-07-2010 01:19 PM
can't restrict sshd access through hosts.allow and hosts.deny but was working earlier farhan Linux - Security 4 04-18-2008 07:41 AM
/etc/hosts.deny/hosts.allow have no effect on sshd access bganesh Linux - Security 4 05-04-2006 08:06 PM

All times are GMT -5. The time now is 02:28 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration