Help answer threads with 0 replies.
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, 08:33 AM   #1
Registered: Jul 2011
Location: Melbourne, AU
Distribution: Centos 5
Posts: 44

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 09:35 AM.
Old 06-23-2013, 10:25 AM   #2
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,876

Rep: Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908Reputation: 2908
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, 03:05 AM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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 11:50 AM
Script for hosts, numbers of hosts and users connected to squid server arunabh_biswas Programming 5 08-28-2010 05:11 AM
Bash/Perl script - provide list of hosts to choose from and ssh to chosen host loadedmind Programming 4 04-07-2010 02:19 PM
can't restrict sshd access through hosts.allow and hosts.deny but was working earlier farhan Linux - Security 4 04-18-2008 08:41 AM
/etc/hosts.deny/hosts.allow have no effect on sshd access bganesh Linux - Security 4 05-04-2006 09:06 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:59 AM.

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