perl program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl program
# 1  
Old 06-16-2010
perl program

I wish to write a Perl program that will provide a listing of files in a directory. The files must be listed in sorted order by the file name.
• By default, the program displays only file names.
• By default, the program lists the files in the current directory.
• The program must provide the following command line options to the user:
1)-d [directory]: This option allows the user to specify a directory or no directory name. If the -d option is provided without specifying a directory name, list your current directory. The output shall be displayed using the following column headers:
File Name Size Owner Group
2) -l: This option instructs the program to display a long listing. The output shall display the following column headers:
File Name Size Owner Group
3) The user selects no options. List the file names under your current directory. The output shall display the following column header:
File Name

NOTE: All other options are invalid for usage.

what would be some good ways to go about doing this?
# 2  
Old 06-16-2010
Any basic perl tutorial will probably answer your questions.

Take a crack at it, and post your code, and I'm sure you'll get some help, then.
# 3  
Old 06-17-2010
Check this and let me know if it meets your requirement:
Code:
#!/usr/local/bin/perl

use strict;
use warnings;

sub default_ls {

print "_"x12 . "File Name" . "_"x12 . "\n";

  foreach (glob("./*")) {
    $_ =~ s/.*\///g;
    print $_ . "\n" if ( -f $_ );
  }

}

sub dir_ls {

$ARGV[1]="." if @ARGV == 1;

print "_"x12 . "File Name" . "_"x12 . " " . "_"x5 . "Size" . "_"x5 . " " .
      "_"x5 . "Owner" . "_"x5 . " " . "_"x5 . "Group" . "_"x5 . "\n";

  foreach (glob("$ARGV[1]/*")) {

    if ( -f $_ ) {
    my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
        $atime, $mtime, $ctime, $blksize, $blocks) = stat($_);
    my $owner=getpwuid($uid);
    my $group=getgrgid($gid);
    $_ =~ s/.*\///g;
    printf "%-33s %14s %15s %15s\n", $_, $size, $owner, $group;
    }
  }

}

sub usage {

   print "Usage: perlls -d [directory]\n       perlls -l\n";
   exit 1;

}

  if (@ARGV == 0) {
   default_ls();
  }
  elsif (@ARGV == 1 && $ARGV[0] eq "-d") {
   dir_ls();
  }
  elsif (@ARGV == 1 && $ARGV[0] eq "-l") {
   dir_ls();
  }
  elsif (@ARGV == 2 && $ARGV[0] eq "-d") {
   if ( -d $ARGV[1] ) {
     dir_ls();
   }
   else {
   print "Provided directory is no directory!\n";
   usage();
   }
  }
  else {
  usage();
  }

Test run / script demonstration:
Code:
$ ./perlls.pl
____________File Name____________
abc.dat
autoftp.pl
bankdata
bankdata.bak
bankfile
bankfile.invalid
changexml.pl
checkexam
checkexam.bak
checkexam.pl
checkhash.pl
checkmatch.final.pl
checkmatch.pl
checkninput.pl
checknninput.pl
checkscore.pl
checkscore1.pl
chgxml.pl
chkexam.pl
chkmatch.pl
$ ./perlls.pl -l
____________File Name____________ _____Size_____ _____Owner_____ _____Group_____
abc.dat                                       54         myuser7         myuser7
autoftp.pl                                   483         myuser7         myuser7
bankdata                                      70         myuser7         myuser7
bankdata.bak                                  80         myuser7         myuser7
bankfile                                     389         myuser7         myuser7
bankfile.invalid                             388         myuser7         myuser7
changexml.pl                                 556         myuser7         myuser7
checkexam                                   1029         myuser7         myuser7
checkexam.bak                               1442         myuser7         myuser7
checkexam.pl                                1096         myuser7         myuser7
checkhash.pl                                 637         myuser7         myuser7
checkmatch.final.pl                          416         myuser7         myuser7
checkmatch.pl                                506         myuser7         myuser7
checkninput.pl                               325         myuser7         myuser7
checknninput.pl                              367         myuser7         myuser7
checkscore.pl                               1114         myuser7         myuser7
checkscore1.pl                              1082         myuser7         myuser7
chgxml.pl                                    263         myuser7         myuser7
chkexam.pl                                   564         myuser7         myuser7
chkmatch.pl                                  395         myuser7         myuser7
$ ./perlls.pl -d
____________File Name____________ _____Size_____ _____Owner_____ _____Group_____
abc.dat                                       54         myuser7         myuser7
autoftp.pl                                   483         myuser7         myuser7
bankdata                                      70         myuser7         myuser7
bankdata.bak                                  80         myuser7         myuser7
bankfile                                     389         myuser7         myuser7
bankfile.invalid                             388         myuser7         myuser7
changexml.pl                                 556         myuser7         myuser7
checkexam                                   1029         myuser7         myuser7
checkexam.bak                               1442         myuser7         myuser7
checkexam.pl                                1096         myuser7         myuser7
checkhash.pl                                 637         myuser7         myuser7
checkmatch.final.pl                          416         myuser7         myuser7
checkmatch.pl                                506         myuser7         myuser7
checkninput.pl                               325         myuser7         myuser7
checknninput.pl                              367         myuser7         myuser7
checkscore.pl                               1114         myuser7         myuser7
checkscore1.pl                              1082         myuser7         myuser7
chgxml.pl                                    263         myuser7         myuser7
chkexam.pl                                   564         myuser7         myuser7
chkmatch.pl                                  395         myuser7         myuser7
$ ./perlls.pl -d ~/dirtest
Provided directory is no directory!
Usage: perlls -d [directory]
       perlls -l
$ ./perlls.pl -d ~/testdir
____________File Name____________ _____Size_____ _____Owner_____ _____Group_____
file3.dat                                    868         myuser7         myuser7
my file1.dat                                 434         myuser7         myuser7
my file2.dat                                 310         myuser7         myuser7
$ ./perlls.pl -d ~/testdir another/dir
Usage: perlls -d [directory]
       perlls -l
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl program get a response before the program quits

I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade. I have: if ( $response =~ m/^/ ) { $user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies

2. Shell Programming and Scripting

Perl program

can anyone help me out to write a code by connecting to the sql database and I need to print the list of tables present in the databse. any ideas please. (1 Reply)
Discussion started by: ramkumar15
1 Replies

3. Shell Programming and Scripting

getting no output with my perl program

hi, i have posted the same kind of the question in some other forum of the same site. but realized that it is supposed to be here so i am reposting it .this is the perl script written to check for particular pattern. my file 1 would look like this hwk:678:9878:asd:09: abc cfgb 12 nmjk ......... (3 Replies)
Discussion started by: anurupa777
3 Replies

4. Shell Programming and Scripting

Help regarding a Perl Program

I want to traverse a durectory for a particular file. Situataion is like this. Path is ABC/a/c/g. it has around 100 folders in it. Search a directory which has word "*latest*" in its path. and then from the latest go through z/x/c to file final.html. In total, i want it to go through... (4 Replies)
Discussion started by: hemasid
4 Replies

5. Programming

Perl program

Hi I am new to perl, i need to write a program to convert horizontal words to vertical eg: cat, dog, cow,.....(text file) this should be written as 1.cat 2.dog like this. can u pls help me to work out.. (4 Replies)
Discussion started by: nitha
4 Replies

6. Shell Programming and Scripting

Exiting a program! - perl

how would you exit out of a programm correctly, without having for the program to crash? for example print "Enter quit"; $input = <STDIN>; if ($input eq "quit") { {QUIT THE PROGRAM} } else { print "invalid input"; } one more thing, how do u exit a program "correctly", if... (3 Replies)
Discussion started by: bshell_1214
3 Replies

7. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

8. Shell Programming and Scripting

perl program

could i get any help with how to link this program together. i dont know what to put where the X's are print `flush`; thank(); #print thank header use Getopt::Std; # use declaration with the options function getopts("ld:") or usage() and exit; ... (3 Replies)
Discussion started by: livewire06
3 Replies

9. Linux

an error in perl program

Hi I am having a file with 243 lines.. The file format s given below eg P25787 hsa03050 1 P20618 hsa03050 1 P25786 hsa03050 1 P49721 hsa03050 1 P54132 hsa03440 1 Q13472 hsa03470 1 Q05513 hsa04530 hsa04910 hsa04930 3 Q04759 ... (0 Replies)
Discussion started by: binnybio
0 Replies

10. Shell Programming and Scripting

My crap PERL program

Hey all, I've been trying to learn Perl on my BSD box. When it came to printing the files out, it bothered me that the lines weren't numbered. So here's my little *crap* claim to some-form-of-fame Perl script which numbers files: ... (9 Replies)
Discussion started by: WIntellect
9 Replies
Login or Register to Ask a Question