I'm working on a script that will traverse directories
(either given from command-line arguments or default if no arguments).
It will list the top disk usage users. I do not intend to
make system calls like "du" etc... but rather do it entirely in Perl.
I have some of the validations up and an idea of the control
flow... but I have problems coding the sub routine for
TRAVERSE works.
Any other comments on my code is also welcomed.
Usage of the script is myscript.pl -N path1 ... path-X
- where N is a numeric number for number of users
if nothing is specified, it will default to 10 users
- path1 ... pathX..
it can potentially take in "infinite" numbers of paths.
paths can be absolute or relative.
Please see my code as follows:
====
#!/usr/bin/perl -w
#
use File::Find;
use strict;
my $path = "."; # default path
my $num = 10; # -n default number of users
my $num_flag = 0;
my $count = 0;
my @paths ; # array to hold paths
my $var = 0;
my %inodes = (); # to track where we've been
my %blocks_of_users = ();
my $num_paths = 0;
$main::size_value = 0;
foreach my $argv (@ARGV) {
if ( $argv =~ m/^-/ ) {
$num_flag++;
print "num_flag" . $num_flag . "\n";
if ($num_flag > 1) {
print STDERR "Cannot have more than one entry for number of users\n";
usage_exit(1);
}
$num = $argv;
print $num . "\n";
print("Matches dashes\n"
;
}
if ( $argv =~ m/^\// ) {
#@paths[$num_paths] = $argv;
$paths[$num_paths] = $argv;
$num_paths++;
print("Matches slashes" . $num_paths . "\n"
;
}
}
# set default path if no path argument is supplied
if ($num_paths == 0) {
@paths = <$path/*>;
}
# testing only, to see what goes inside @paths
foreach (@paths) {
print "printing paths" . $_ . "\n";
}
# testing only...
foreach my $dir(@paths) {
print ("$dir\n"
;
}
my $filesize = 0;
# I have trouble here..!!!
sub traverse ($&) {
my $dir = shift;
my $wanted = shift;
find($wanted, "$dir"
;
};
# seeems to be working..
sub getsize () {
my $size = 0;
$size = -s File::Find::name;
$main::size_value += $size;
print sprintf("%d", (-s $File::Find::name)), "\t$File::Find::name \n";
}
sub usage_exit {
print STDERR "Usage [-n] [path 1..] [path..n]\n";
exit(@_);
}
traverse("$ARGV[1]", \&getsize);
print "Total size: [", sprintf("%d", ($main::size_value)),"]\n";
(either given from command-line arguments or default if no arguments).
It will list the top disk usage users. I do not intend to
make system calls like "du" etc... but rather do it entirely in Perl.
I have some of the validations up and an idea of the control
flow... but I have problems coding the sub routine for
TRAVERSE works.
Any other comments on my code is also welcomed.
Usage of the script is myscript.pl -N path1 ... path-X
- where N is a numeric number for number of users
if nothing is specified, it will default to 10 users
- path1 ... pathX..
it can potentially take in "infinite" numbers of paths.
paths can be absolute or relative.
Please see my code as follows:
====
#!/usr/bin/perl -w
#
use File::Find;
use strict;
my $path = "."; # default path
my $num = 10; # -n default number of users
my $num_flag = 0;
my $count = 0;
my @paths ; # array to hold paths
my $var = 0;
my %inodes = (); # to track where we've been
my %blocks_of_users = ();
my $num_paths = 0;
$main::size_value = 0;
foreach my $argv (@ARGV) {
if ( $argv =~ m/^-/ ) {
$num_flag++;
print "num_flag" . $num_flag . "\n";
if ($num_flag > 1) {
print STDERR "Cannot have more than one entry for number of users\n";
usage_exit(1);
}
$num = $argv;
print $num . "\n";
print("Matches dashes\n"
}
if ( $argv =~ m/^\// ) {
#@paths[$num_paths] = $argv;
$paths[$num_paths] = $argv;
$num_paths++;
print("Matches slashes" . $num_paths . "\n"
}
}
# set default path if no path argument is supplied
if ($num_paths == 0) {
@paths = <$path/*>;
}
# testing only, to see what goes inside @paths
foreach (@paths) {
print "printing paths" . $_ . "\n";
}
# testing only...
foreach my $dir(@paths) {
print ("$dir\n"
}
my $filesize = 0;
# I have trouble here..!!!
sub traverse ($&) {
my $dir = shift;
my $wanted = shift;
find($wanted, "$dir"
};
# seeems to be working..
sub getsize () {
my $size = 0;
$size = -s File::Find::name;
$main::size_value += $size;
print sprintf("%d", (-s $File::Find::name)), "\t$File::Find::name \n";
}
sub usage_exit {
print STDERR "Usage [-n] [path 1..] [path..n]\n";
exit(@_);
}
traverse("$ARGV[1]", \&getsize);
print "Total size: [", sprintf("%d", ($main::size_value)),"]\n";