For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > So lost... Need help









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 So lost... Need help
Jay

2006-10-21, 6:57 pm

Hi there,

I've been given the task to write a small Perl program which provides a
listing of files in a directory. The files need to be sorted by file
name. By default, the program displays only file names and lists files
in the current directory. The program must provide the following
command line options.

-d [directory]: This option will allow the user to specify the
directory. If this option is not used than the command will use the
current directly by default.
-l: This option instructs the program to display a long listing.
Instead of just filenames, the program must display the file name, the
file size, the file owner, and the file group data.
-w: This option will list just file names, but instead of listing one
file per line, all the file names will be listed on the same line,
separated by commas. There shall be no comma after the last filename is
printed.

The -w command line argument cannot be mixed with -l. Your program
must provide suitable error checking. In the event of invalid input,
the program should display a help message to the user.

Here's the sample output
User selects no options (only subset of output is shown. Also, "File
Name" is a header):
>listdir

File Name
4th
4th_log
4th.old
4th.old.2
address.cpp
address.h
address.o

User selects "-w" option (no header because of -w)
>listdir -w

4th, 4th_log, 4th.old, 4th.old.2, address.cpp, address.h, address.o

User selects "-w" and "-d" option (no header because of -w)
>listdir -w -d /export/home/vaughang/public_html/itec400/general_info

DirStruct.doc, Executing, Grading_Policy.doc, ITEC400Survey.doc,
knoppix_and_floppies.doc, Schedule.xls, UnixCommands.doc

User selects "-l" option (only subset of output is show):
>listdir -l

File Name Size Owner Group
4th 4096 vaughang faculty
4th_log 116 vaughang faculty
4th.old 4096 vaughang faculty
4th.old.2 4096 vaughang faculty
address.cpp 18 vaughang faculty
address.h 64 vaughang faculty
address.o 704 vaughang faculty

=B7 User selects "-l" and "-w" option (illegal combination):
>listdir -l -w

usage: listdir [-d dir] [-l] [-h] [-d directory name]
-d dir : use specified directory (current dir is default)
-l : print using long form
-w : print a comma separated list of files.
NOTE: The -w option cannot be used with -l option.

I've been given the following code, but it's not complete. Parts are
missing.
#!/usr/bin/perl -w
$number_of_arguments =3D $#ARGV;
$help_file =3D 'usage: listdir [-d dir] [-n x] [-l h] [-d directory name]
-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.';

if ($number_of_arguments gt -1)
{
$first_arg =3D $ARGV[0];

# check for -n option
if ($first_arg eq '-n')
{
#check for mandatory second argument
if ($ARGV[1] eq "")
{
print $help_file;
print "\n";
exit;
}
else
{
`ls >aTempFile`;
$number_of_lines =3D $ARGV[1];
}
if ($first_arg eq '-d')
{
#check for mandatory second argument
if ($ARGV[1] eq "")
{
print $help_file;
print "\n";
exit;
}
else
{
$second_arg =3D $ARGV[1];
`ls $second_arg >aTempFile`;
}
}
}

# check for -w option
if ($first_arg eq '-w')
if ($number_of_arguments gt 0)
{
$second_arg =3D $ARGV[1];
if ($second_arg eq '-d') # check for directory arg
{
$third_arg =3D $ARGV[2];
`ls $third_arg >aTempFile`;
}
elsif ($second_arg eq '-n') # check for -n arg
{
print $help_file;
print "\n";
exit;
}
}
else
{

# check for -l option grep everything except for total
if ($first_arg eq '-l')
{
if ($number_of_arguments gt 0)
{
$second_arg =3D $ARGV[1];
if ($second_arg eq '-d') # check for -d option
{
$third_arg =3D $ARGV[2];
`ls -l $third_arg | grep -v 'total' >aTempFile`; #>aTempFile`;
}
elsif ($second_arg eq '-n') # check for -n option
{
$number_of_lines =3D $ARGV[2];
`ls -l | grep -v 'total' >aTempFile`; #>aTempFile`;
}
else # print help file invalid options
{
print $help_file;
print "\n";
exit;
}
}
else #default
{
`ls -l | grep -v 'total' >aTempFile`;
}
# open file for reading
open(MY_FILE, "aTempFile") or die "Can't open tempfile: $!\n";
$counter =3D 0;

# write format for header
$~ =3D "HEADER";
write;

format HEADER =3D
File Name Size Owner Group


I need help filling the holes. This is out of my league. Any help
would be appreciated. Thanks.

Jay

2006-10-21, 6:57 pm

I think I'm going to go in a different direction w/ this perl script so
I'll post if/when I have questions. Thanks for reading.


Jay wrote:
> Hi there,
>
> I've been given the task to write a small Perl program which provides a
> listing of files in a directory. The files need to be sorted by file
> name. By default, the program displays only file names and lists files
> in the current directory. The program must provide the following
> command line options.
>
> -d [directory]: This option will allow the user to specify the
> directory. If this option is not used than the command will use the
> current directly by default.
> -l: This option instructs the program to display a long listing.
> Instead of just filenames, the program must display the file name, the
> file size, the file owner, and the file group data.
> -w: This option will list just file names, but instead of listing one
> file per line, all the file names will be listed on the same line,
> separated by commas. There shall be no comma after the last filename is
> printed.
>
> The -w command line argument cannot be mixed with -l. Your program
> must provide suitable error checking. In the event of invalid input,
> the program should display a help message to the user.
>
> Here's the sample output
> User selects no options (only subset of output is shown. Also, "File
> Name" is a header):
> File Name
> 4th
> 4th_log
> 4th.old
> 4th.old.2
> address.cpp
> address.h
> address.o
>
> User selects "-w" option (no header because of -w)
> 4th, 4th_log, 4th.old, 4th.old.2, address.cpp, address.h, address.o
>
> User selects "-w" and "-d" option (no header because of -w)
> DirStruct.doc, Executing, Grading_Policy.doc, ITEC400Survey.doc,
> knoppix_and_floppies.doc, Schedule.xls, UnixCommands.doc
>
> User selects "-l" option (only subset of output is show):
> File Name Size Owner Group
> 4th 4096 vaughang faculty
> 4th_log 116 vaughang faculty
> 4th.old 4096 vaughang faculty
> 4th.old.2 4096 vaughang faculty
> address.cpp 18 vaughang faculty
> address.h 64 vaughang faculty
> address.o 704 vaughang faculty
>
> =B7 User selects "-l" and "-w" option (illegal combination):
> usage: listdir [-d dir] [-l] [-h] [-d directory name]
> -d dir : use specified directory (current dir is default)
> -l : print using long form
> -w : print a comma separated list of files.
> NOTE: The -w option cannot be used with -l option.
>
> I've been given the following code, but it's not complete. Parts are
> missing.
> #!/usr/bin/perl -w
> $number_of_arguments =3D $#ARGV;
> $help_file =3D 'usage: listdir [-d dir] [-n x] [-l h] [-d directory name]
> -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.';
>
> if ($number_of_arguments gt -1)
> {
> $first_arg =3D $ARGV[0];
>
> # check for -n option
> if ($first_arg eq '-n')
> {
> #check for mandatory second argument
> if ($ARGV[1] eq "")
> {
> print $help_file;
> print "\n";
> exit;
> }
> else
> {
> `ls >aTempFile`;
> $number_of_lines =3D $ARGV[1];
> }
> if ($first_arg eq '-d')
> {
> #check for mandatory second argument
> if ($ARGV[1] eq "")
> {
> print $help_file;
> print "\n";
> exit;
> }
> else
> {
> $second_arg =3D $ARGV[1];
> `ls $second_arg >aTempFile`;
> }
> }
> }
>
> # check for -w option
> if ($first_arg eq '-w')
> if ($number_of_arguments gt 0)
> {
> $second_arg =3D $ARGV[1];
> if ($second_arg eq '-d') # check for directory arg
> {
> $third_arg =3D $ARGV[2];
> `ls $third_arg >aTempFile`;
> }
> elsif ($second_arg eq '-n') # check for -n arg
> {
> print $help_file;
> print "\n";
> exit;
> }
> }
> else
> {
>
> # check for -l option grep everything except for total
> if ($first_arg eq '-l')
> {
> if ($number_of_arguments gt 0)
> {
> $second_arg =3D $ARGV[1];
> if ($second_arg eq '-d') # check for -d option
> {
> $third_arg =3D $ARGV[2];
> `ls -l $third_arg | grep -v 'total' >aTempFile`; #>aTempFile`;
> }
> elsif ($second_arg eq '-n') # check for -n option
> {
> $number_of_lines =3D $ARGV[2];
> `ls -l | grep -v 'total' >aTempFile`; #>aTempFile`;
> }
> else # print help file invalid options
> {
> print $help_file;
> print "\n";
> exit;
> }
> }
> else #default
> {
> `ls -l | grep -v 'total' >aTempFile`;
> }
> # open file for reading
> open(MY_FILE, "aTempFile") or die "Can't open tempfile: $!\n";
> $counter =3D 0;
>
> # write format for header
> $~ =3D "HEADER";
> write;
>
> format HEADER =3D
> File Name Size Owner Group
>
>
> I need help filling the holes. This is out of my league. Any help
> would be appreciated. Thanks.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com