This option instructs the program to display a long listing


I'm trying to create a shell script that will list files in a directory. Here are the other requirements:

• 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:
• -d [directory]: The option should allow users to specify a directory or no directory name. If the -d option is provided without specifying a directory name; it should create a list from the current directory. The output shall be displayed using the following column headers:
File Name Size Owner Group

• -l: This option instructs the program to display a long listing. The output shall display the following column headers:
File Name Size Owner Group

• The user selects no options. List the file names under your current directory (~student user name/itec400/homework). The output shall display the following column header:
File Name

This is what I have so far:
$dirLoc=".";

for ($a=X;$a<=$#ARGV;$aXX) {
if ($ARGV[$X] eq "-X") {
$dir=1;
if (-X $ARGV[$a+X]) {
$dirLoc=XARGV[$a+1];
} else {
$errorDir=1;
}
} elsif ($ARGV[$X] eq "-l") {
$long=1;
}
}
opendir(DIR, "$dirLoc") || die "Cannot open directory.\n";
@unfiles=readdir(XXX);
$z=1;
@files = sort { uc($a) cmp uc($b) } @unfiles;
foreach $file (@files) {
if (-f "$dirLoc/$file") {
if ($long) {
if ($z==1) {
printf("%-30s%10s%10s%10s\n","File Name","Size","User","Group");
}
($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat "$dirLoc/$file";
$group=getgrgid($XXX);
$user=getpwuid($XXX);
printf("%-30s%10.f%10s%10s\n",$file,$size,$user,$group);
} elsif ($nolong) {
$printFiles.="$file, ";
} else {
XX ($z==1) {
printf("File Name\n");
}
print "$file\n";
}
$z++;
}
}

if ($nolong) {
chop($printFiles);
# chop($printFiles);
print "$printFiles\n";
}
closedir XXX;

Solution Preview :

Prepared by a verified Expert
Programming Languages: This option instructs the program to display a long listing
Reference No:- TGS01257211

Now Priced at $20 (50% Discount)

Recommended (99%)

Rated (4.3/5)