> -----Original Message-----
> From: William Melanson [mailto:[EMAIL PROTECTED]]
> Sent: Monday, June 10, 2002 8:21 AM
> To: [EMAIL PROTECTED]
> Subject: Is this an eye sore?
>
>
>
> Might I trouble you kind folks for a bit of feedback? This is
> my first real script I put together in which it collects all
> users from the output of the 'repquota -a' command who have not
> yet had quotas applied. It does work. I'm just curious if it's
> an eye sore. Maybe I should just give up attempting this whole
> programming stuff and stick to wielding my light sabre at those
> selfless stormtroopers (who come in the hundreds) til the wee
> hours of the morning?
>
> - Bill
>
> ## start
>
> #!/usr/bin/perl -w
"use strict" should be used.
>
> system("/usr/bin/clear");
I would take this out, on the grounds that most programs don't
do this. But that's up to you.
> @arg1 = qw(
> mail
> sysquota
> root
> nobody
> smmsp
> daemon
> testing
> );
I assume this is a list of users to ignore. A hash would make it
easy to check a user. Also, a more descriptive variable name would
be better:
my %ignore = map { ($_ => 1) } qw(mail sysquota ..and so on..);
>
> unless (open(RQUOTA,"/usr/sbin/repquota -a |")) {
> print("Unable to open repquota file:\n");
> } else {
This is typically written as:
open(RQUOTA,"/usr/sbin/repquota -a |")
or die "Unable to open repquota file: $!\n";
Then you avoid wrapping the rest of the program in an else block.
> if (-e "/tmp/stat.tmp") {
> unlink("/tmp/stat.tmp");
> }
I see below you open this file for append mode inside the loop.
I would suggest just opening the file for output mode here before
the loop starts. Then you can dispense with the unlink and the
repeated opening of the file.
open(NQUOTA,">/tmp/stat.tmp")
or die "Unable to open /tmp/stat.tmp: $!\n";
> print("Users Without Quotas * * * *\n\n");
You want to skip the heading lines of the output (2 lines?), so
you could add something like:
<RQUOTA> for 1..2; # skip first two lines of output
> while (<RQUOTA>) {
> if (/\s0\s{7}0\s{1,}/) {
Here's where some comments in your code would help. This appears
to be checking for a line representing a user without quotas.
> for ( $i = 0; $i <=6; $i++ ) {
> s/\b$arg1[$i]\b\s{0,}\-\-\s{0,}[0-9]{1,}.*$//g;
> }
Here it looks like you are "blanking out" the line if it's a
user you want to ignore. Better IMO to just skip to the next
line and bypass the code below. Extract the user and check it
%ignore hash.
Also, since the data appears to be in a fixed format, you could
use unpack to grab the portions you want:
my ($user, undef, $quota) = unpack('A8 A12 A8', $_);
next if $quota; # skip if user has a quota
next if exists $ignore{$user}; # skip if user is to be ignored
(Note, you may have to adjust the unpack template; I'm guessing at
which field(s) you want to look at).
> if (/^[a-zA-Z0-9]/) {
> print("$_");
> }
> if (/(\w+)/) {
> unless (open(NQUOTA,">>/tmp/stat.tmp")) {
> print("Unable to open /tmp/stat.tmp
file:\n");
} else {
print(NQUOTA "$1\n");
}
}
}
Now we have a user without a quota, so:
print $_;
print NQUOTA "$user\n";
}
print("\n");
close(NQUOTA);
close(RQUOTA);
}
Let's put it all together:
#/usr/bin/perl -w
use strict;
# users to ignore
my %ignore = map { ($_ => 1) } qw(mail sysquota
root nobody smmsp daemon testing);
open(RQUOTA,"/usr/sbin/repquota -a |")
or die "Unable to open repquota file: $!\n";
open(NQUOTA,">/tmp/stat.tmp")
or die "Unable to open /tmp/stat.tmp: $!\n";
<RQUOTA> for 1..2; # skip first two lines of output
while (<RQUOTA>) {
my ($user, undef, $quota) = unpack('A8 A12 A8', $_);
next if $quota > 0; # skip if user has a quota
next if exists $ignore{$user}; # skip if user is to be ignored
print $_;
print NQUOTA "$user\n";
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]