#!/usr/bin/perl # vi: expandtab shiftwidth=4: use strict; use warnings; use Getopt::Std; sub usage { print "usage: $0 [-hnr]\n"; print "\t-h\tPrint this help\n"; print "\t-n\tPrint commands, don't execute\n"; print "\t-r\tRemove directories\n"; exit(1); } our ( $opt_r, $opt_n, $opt_h ); my $user_pattern = '^@user'; my $group_pattern = '^@group'; getopts('hnr') or usage(); usage if $opt_h; if ($<) { print "not root, just printing what would be done\n"; $opt_n = 1; } while (<>) { chomp; if (m/$user_pattern/x) { my @token = split /[\s:]/x, $_; my $user = $token[1]; my @cmd = ( '/usr/sbin/userdel', "-v", $user ); print join( ' ', @cmd ), "\n"; system @cmd unless $opt_n; } elsif (m/$group_pattern/x) { my @token = split /[\s:]/x, $_; my $group = $token[1]; my @cmd = ( '/usr/sbin/groupdel', "-v", $group ); print join( ' ', @cmd ), "\n"; system @cmd unless $opt_n; } else { my $file = $_; if ( -d $file ) { if ($opt_r) { print "delete directory ", $file, "\n"; rmdir "$file" or warn "error removing directory $file: $!\n"; } else { print "keeping directory ", $file, "\n"; } } else { print "delete file ", $file, "\n"; unlink $file unless $opt_n; } } }