Home > Archive > PERL Beginners > October 2006 > Perl Script - Not Getting Results When Using Command Line Arguments
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Perl Script - Not Getting Results When Using Command Line Arguments
|
|
|
| Hello all,
I have to write a perl script which accepts different arguments from
the command line and manipulate the data in those ways. The code is as
follows:
#!/usr/bin/perl -w
$helpfile = 'usage: listdir.pl [-d dir] [-l] [-w] [-d dir]
-d dir : use specified directory (current is default)
-l : print using long form
-w : print a comma seperated list of files
NOTE: The -w option cannot be used with the -l option.';
if ($#ARGV == -1)
{
opendir($THISDIR, ".");
@DIRLIST = readdir($THISDIR);
print "Filename\n";
@SORTED_DIRLIST = sort @DIRLIST;
foreach $result(@SORTED_DIRLIST)
{
#if ($ARGV[0] eq "-l" && $ARGV[1] ne "-w")
# {
# opendir($THISDIR, ".");
# @DIRLIST = readdir($THISDIR);
# @SORTED_DIRLIST = sort @DIRLIST;
# foreach $result(@SORTED_DIRLIST)
# {
# if (-f $result)
# {
# $resultsize = -s $result;
# $groups=(stat($result))[5];
# $groups=getgrgid($groups);
# $users=(stat($result))[4];
# $users=getpwuid($users);
# format =
# Filename Size Owner
Group
# @<<<<<<<<<<<<~~ @<<<<<<~~ @<<<<<<<~~
@<<<<<<<~~
# $result; $resultsize; $users;
$groups;
# .
# write;
# } #end if
# } #end foreach
# } #end total
# exit (0);
if (($ARGV[0] eq "-l") && ($ARGV[1] eq "-w"))
{
die "$helpfile";
}
if (($ARGV[0] eq "-w") && ($ARGV[1] eq "-l"))
{
die "$helpfile";
}
if ($ARGV[0] eq "-d")
{
if ($ARGV[1] eq " ")
{
die "Error. Use -d with directory name or use -l for
current directory.";
}
opendir($THISDIR, $ARGV[1]) or die "Can't open directory.";
@DIRLIST = readdir($THISDIR);
@SORTED_DIRLIST = sort @DIRLIST;
foreach $result(@SORTED_DIRLIST)
{
if (-f $result)
{
print "$result\n";
} #end if
} #end foreach
} #end if
if (($ARGV[0] eq "-w") && ($ARGV[1] ne "-d"))
{
opendir($THISDIR, ".");
@DIRLIST = readdir($THISDIR);
@SORTED_DIRLIST = sort @DIRLIST;
$last = pop(@DIRLIST);
foreach $result(@DIRLIST)
{
if (-f $result)
{
print "$result, ";
}
}
print "$last\n";
}
if (($ARGV[0] eq "-w") && ($ARGV[1] eq "-d"))
{
opendir($THISDIR, $ARGV[2]);
@DIRLIST = readdir($THISDIR);
@SORTED_DIRLIST = sort @DIRLIST;
$last = pop(@DIRLIST);
foreach $result(@DIRLIST)
{
if (-f $result)
{
print "$result, ";
} #end if
}# end foreach
print "$last\n";
exit(0);
} #end if
The only section that works is the first one with no command line
options. All others, for example the -d argument, don't return
results. This is an assignment and I don't get much help. Thanks.
| |
| DJ Stunks 2006-10-22, 9:56 pm |
| Jay wrote:
> Hello all,
> I have to write a perl script which accepts different arguments from
> the command line and manipulate the data in those ways. The code is as
> follows:
>
> #!/usr/bin/perl -w
>
> $helpfile = 'usage: listdir.pl [-d dir] [-l] [-w] [-d dir]
> -d dir : use specified directory (current is default)
> -l : print using long form
> -w : print a comma seperated list of files
> NOTE: The -w option cannot be used with the -l option.';
>
> if ($#ARGV == -1)
> {
> opendir($THISDIR, ".");
> @DIRLIST = readdir($THISDIR);
> print "Filename\n";
> @SORTED_DIRLIST = sort @DIRLIST;
> foreach $result(@SORTED_DIRLIST)
> {
> #if ($ARGV[0] eq "-l" && $ARGV[1] ne "-w")
> # {
> # opendir($THISDIR, ".");
> # @DIRLIST = readdir($THISDIR);
> # @SORTED_DIRLIST = sort @DIRLIST;
> # foreach $result(@SORTED_DIRLIST)
> # {
> # if (-f $result)
> # {
> # $resultsize = -s $result;
> # $groups=(stat($result))[5];
> # $groups=getgrgid($groups);
> # $users=(stat($result))[4];
> # $users=getpwuid($users);
> # format =
> # Filename Size Owner
> Group
> # @<<<<<<<<<<<<~~ @<<<<<<~~ @<<<<<<<~~
> @<<<<<<<~~
> # $result; $resultsize; $users;
> $groups;
> # .
> # write;
> # } #end if
> # } #end foreach
> # } #end total
> # exit (0);
> if (($ARGV[0] eq "-l") && ($ARGV[1] eq "-w"))
> {
> die "$helpfile";
> }
> if (($ARGV[0] eq "-w") && ($ARGV[1] eq "-l"))
> {
> die "$helpfile";
> }
> if ($ARGV[0] eq "-d")
> {
> if ($ARGV[1] eq " ")
> {
> die "Error. Use -d with directory name or use -l for
> current directory.";
> }
> opendir($THISDIR, $ARGV[1]) or die "Can't open directory.";
> @DIRLIST = readdir($THISDIR);
> @SORTED_DIRLIST = sort @DIRLIST;
> foreach $result(@SORTED_DIRLIST)
> {
> if (-f $result)
> {
> print "$result\n";
> } #end if
> } #end foreach
> } #end if
> if (($ARGV[0] eq "-w") && ($ARGV[1] ne "-d"))
> {
> opendir($THISDIR, ".");
> @DIRLIST = readdir($THISDIR);
> @SORTED_DIRLIST = sort @DIRLIST;
> $last = pop(@DIRLIST);
> foreach $result(@DIRLIST)
> {
> if (-f $result)
> {
> print "$result, ";
> }
> }
> print "$last\n";
> }
> if (($ARGV[0] eq "-w") && ($ARGV[1] eq "-d"))
> {
> opendir($THISDIR, $ARGV[2]);
> @DIRLIST = readdir($THISDIR);
> @SORTED_DIRLIST = sort @DIRLIST;
> $last = pop(@DIRLIST);
> foreach $result(@DIRLIST)
> {
> if (-f $result)
> {
> print "$result, ";
> } #end if
> }# end foreach
> print "$last\n";
> exit(0);
> } #end if
>
> The only section that works is the first one with no command line
> options. All others, for example the -d argument, don't return
> results. This is an assignment and I don't get much help. Thanks.
you should almost never roll your own parser for command line
arguments. use one of the heavily tested modules provided with every
Perl distribution - like Getopt::Std or Getopt::Long.
you should also move the actual work your script does into subroutines.
the script you posted above has far too much repitition. this will
lead to errors and inconsistencies down the road.
-jp
| |
|
| Thanks, but this is just for an assignment. It has to work. Here's
the revised code:
#!/usr/bin/perl -w
#Help file provides command usage in event of error.
$helpfile = 'usage: listdir.pl [-d dir] [-l] [-w] [-d dir]
-d dir : use specified directory (current is default)
-l : print using long form
-w : print a comma seperated list of files
NOTE: The -w option cannot be used with the -l option.';
#First section provides dirlist for current directory in long format
#with no details.
if ($#ARGV == -1)
{
opendir($THISDIR, ".");
@DIRLIST = readdir($THISDIR);
print "Filename\n";
@SORTED_DIRLIST = sort @DIRLIST;
foreach $result(@SORTED_DIRLIST)
{
if (-f $result)
{
print "$result\n";
} #end if
#Fifth section provides dirlist in wide format for current directory.
if (($ARGV[0] eq "-w") && ($ARGV[1] ne "-d"))
{
opendir($THISDIR, ".");
@DIRLIST = readdir($THISDIR);
@SORTED_DIRLIST = sort @DIRLIST;
$last = pop(@SORTED_DIRLIST);
foreach $result(@SORTED_DIRLIST)
{
if (-f $result)
{
print "$result, ";
}
}
print "$last\n";
}
#Sixth section provides dirlist in wide format for another directory.
if (($ARGV[0] eq "-w") && ($ARGV[1] eq "-d"))
{
opendir($THISDIR, $ARGV[2]);
@DIRLIST = readdir($THISDIR);
@SORTED_DIRLIST = sort @DIRLIST;
$last = pop(@SORTED_DIRLIST);
foreach $result(@SORTED_DIRLIST)
{
if (-f $result)
{
print "$result, ";
} #end if
}# end foreach
print "$last\n";
exit(0);
{
die "Error: Use -d with directory name.";
}
opendir($THISDIR, $ARGV[1]) or die "Can't open directory.";
@DIRLIST = readdir($THISDIR);
@SORTED_DIRLIST = sort @DIRLIST;
foreach $result(@SORTED_DIRLIST)
{
if (-f $result)
{
print "$result\n";
} #end if
} #end foreach
} #end if
#Fifth section provides dirlist in wide format for current directory.
if (($ARGV[0] eq "-w") && ($ARGV[1] ne "-d"))
{
opendir($THISDIR, ".");
@DIRLIST = readdir($THISDIR);
@SORTED_DIRLIST = sort @DIRLIST;
$last = pop(@SORTED_DIRLIST);
foreach $result(@SORTED_DIRLIST)
{
if (-f $result)
{
print "$result, ";
}
}
print "$last\n";
}
#Sixth section provides dirlist in wide format for another directory.
if (($ARGV[0] eq "-w") && ($ARGV[1] eq "-d"))
{
opendir($THISDIR, $ARGV[2]);
@DIRLIST = readdir($THISDIR);
@SORTED_DIRLIST = sort @DIRLIST;
$last = pop(@SORTED_DIRLIST);
foreach $result(@SORTED_DIRLIST)
{
if (-f $result)
{
print "$result, ";
} #end if
}# end foreach
print "$last\n";
exit(0);
} #end if
Two problems:
1) I'm getting three errors similiar to this: "use of uninitialized
value in string eq at ./listdir2.pl line 55." How is this solved?
2) I can't get the script to read another folder using the -d option
that is in the script. I can't find the error in the code. Is there
something wrong with the code?
Help on these problems would be appreciated. Thanks.
-Jay
| |
| DJ Stunks 2006-10-22, 9:56 pm |
| Jay wrote:
> Thanks, but this is just for an assignment. It has to work. Here's
> the revised code:
> <snip>
> Two problems:
> 1) I'm getting three errors similiar to this: "use of uninitialized
> value in string eq at ./listdir2.pl line 55." How is this solved?
> 2) I can't get the script to read another folder using the -d option
> that is in the script. I can't find the error in the code. Is there
> something wrong with the code?
>
> Help on these problems would be appreciated. Thanks.
ok, I missed it was an assignment when I first responded. my second
advice stands though - your script has two basic functions and
therefore is almost identical no matter which directory it starts in,
or whether it prints in normal or long format. that means that you
should use two subroutines - one to grab all the files in a given
directory, and a second one to print out a list of files in a given
format. You'd figure out what the user asks for, then call the first
subroutine to generate a list of files in that dir. then pass that
list to the second sub to print out the results.
once you recode your script to take this advice into account you won't
see the inconsistent errors you see now.
finally, you should be coding with strict in effect particularly as a
beginner. use lexical warnings for bonus points...
-jp
| |
| charley@pulsenet.com 2006-10-23, 6:57 pm |
| Jay wrote:
> Thanks, but this is just for an assignment. It has to work. Here's
> the revised code:
[snip]
>
> #Sixth section provides dirlist in wide format for another directory.
> if (($ARGV[0] eq "-w") && ($ARGV[1] eq "-d"))
> {
> opendir($THISDIR, $ARGV[2]);
> @DIRLIST = readdir($THISDIR);
> @SORTED_DIRLIST = sort @DIRLIST;
> $last = pop(@SORTED_DIRLIST);
> foreach $result(@SORTED_DIRLIST)
> {
> if (-f $result)
This filetest won't work. The following from the docs for the readdir
function explains why.
If you're planning to filetest the return values out of a readdir,
you'd better prepend the directory in question. Otherwise, because we
didn't chdir there, it would have been testing the wrong file.
opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
@dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
closedir DIR;
> {
> print "$result, ";
> } #end if
> }# end foreach
> print "$last\n";
> exit(0);
> {
> die "Error: Use -d with directory name.";
> }
> opendir($THISDIR, $ARGV[1]) or die "Can't open directory.";
> @DIRLIST = readdir($THISDIR);
> @SORTED_DIRLIST = sort @DIRLIST;
> foreach $result(@SORTED_DIRLIST)
> {
> if (-f $result)
> {
> print "$result\n";
> } #end if
> } #end foreach
> } #end if
>
> #Fifth section provides dirlist in wide format for current directory.
> if (($ARGV[0] eq "-w") && ($ARGV[1] ne "-d"))
> {
> opendir($THISDIR, ".");
> @DIRLIST = readdir($THISDIR);
> @SORTED_DIRLIST = sort @DIRLIST;
> $last = pop(@SORTED_DIRLIST);
> foreach $result(@SORTED_DIRLIST)
> {
> if (-f $result)
> {
> print "$result, ";
> }
> }
> print "$last\n";
> }
>
> #Sixth section provides dirlist in wide format for another directory.
The same problem here as above with filetests on a file not in the
current directory
> if (($ARGV[0] eq "-w") && ($ARGV[1] eq "-d"))
> {
> opendir($THISDIR, $ARGV[2]);
> @DIRLIST = readdir($THISDIR);
> @SORTED_DIRLIST = sort @DIRLIST;
> $last = pop(@SORTED_DIRLIST);
> foreach $result(@SORTED_DIRLIST)
> {
> if (-f $result)
> {
> print "$result, ";
> } #end if
> }# end foreach
> print "$last\n";
> exit(0);
> } #end if
>
> Two problems:
> 1) I'm getting three errors similiar to this: "use of uninitialized
> value in string eq at ./listdir2.pl line 55." How is this solved?
> 2) I can't get the script to read another folder using the -d option
> that is in the script. I can't find the error in the code. Is there
> something wrong with the code?
>
> Help on these problems would be appreciated. Thanks.
> -Jay
In addition, as JP recommended, you would get much cleaner and error
free code if you use one of the supplied modules Getopt::Std or
Getopt::Long.
Below is some code that demonstrates how you might use them.
Chris
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
my $helpfile = 'usage: listdir [-d dir] [-l] [-w] [-n x]
-d dir : use specified directory (current dir is default)
-n x : print x lines of output
-l : print using long form
-w : print a comma separated list of files.
NOTE: The -w option cannot be used with either -n or -l option.';
my (%opts);
getopts ('wld:n:',\%opts);
die "$helpfile\n" if $opts{w} && ($opts{l} || $opts{n});
my $dir = $opts{d} || '.';
opendir DIR, $dir or die $!;
my @contents = sort readdir DIR, $dir or die $!;
close DIR;
if ($opts{l}) {
}
elsif ($opts{w}) {
}
else { # if not -w or -l
}
|
|
|
|
|