Manish Tiwari wrote:
> Thanx Brian and Bill
> 
> one approach
> 
> Actually this code is in a subroutine that returns a list of files in a
> path name supplied as an argument to this subroutine
> 
> i have to do to get list of files
> 
> @data = `ls -ltr | grep -v total | sort +8` ;
> 
> now $? returns exist status of "sort" not "ls"
> 
> Main objective : how to make sure that "ls" command works fine ?
> so one approach is divide it into two parts
> 
> @data = `ls -ltr ` ;
> if ( $? != 0 ) {
>         print "ls command failed " ;
>         exit 1 ;
> }
> 
> now
> 
> i want like
> 
> @data = `echo @data | grep -v total | sort +8 ` ;
> this wont work  ..as data is not environment variable
> 
> i can however do
> 
> ls -ltr
> write output to some temporary file
> 
> then do
> 
> @data = `cat < file name > | grep -v total | sort +8 ' ;
> 
> 
> but then i have to create a temp file
> and write to it
> and delete it

No need to shell out to do the last two pipes - actually you can do all
three in Perl.  This will eliminate the last two:

use strict;

my @data = `ls -ltr`;

print "\$? = $?\n";
print "data[0] = $data[0]\n" if @data;

if ($? != 0 or not @data or $data[0] !~ /^total/i) {
         print "ls command failed " ;
         exit 1 ;
}

# second pipe

shift @data;    # drop total line

# third pipe

my @sdata = map { $_->[0] }
            sort { $a->[9] cmp $b->[9] }
            map { [$_, split /\s+/] } @data;
print @sdata, "\n";


__END__

You could use opendir/readdir/closedir/stat to do the ls.

I never shell out unless it's necessary - makes your script more portable
and usually faster.

Here's an ls -lF sub that you could modify:


sub format_lslF {
        my $dir = shift;
        my $file = shift;

my @s = stat "$dir/$file";

my $mode = convert_mode ($s[2]);
my $uid  = sprintf "%-8u", $s[4];
my $gid  = sprintf "%-8u", $s[5];

if (not $windoze) {     # change this to check your appropriate OS
        eval {
                $uid = getpwuid ($s[4]);
                $gid = getgrgid ($s[5]);
        }
}

my $date = convert_date ($s[9]);
my $type = '*' if -x "$dir/$file";
$type = '/' if -d "$dir/$file";

return sprintf "%s%-9.9s %3u %-8.8s %-8.8s %8u %s %-s%s",
   -d "$dir/$file" ? 'd' : '-', $mode, $s[3], $uid, $gid, $s[7], $date,
   $file, $type;

}

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub convert_mode {
        my $mode = shift;
        my $str = '';

for (my $ii = 0; $ii < 9; $ii += 3) {

        if ($mode & (1 << $ii)) {               # mode & 01, 010, 0100
                $str .= 'x';
        } else {
                $str .= '-';
        }
        if ($mode & (1 << ($ii+1))) {           # mode & 02, 020, 0200
                $str .= 'w';
        } else {
                $str .= '-';
        }
        if ($mode & (1 << ($ii+2))) {           # mode & 04, 040, 0400
                $str .= 'r';
        } else {
                $str .= '-';
        }
}
substr ($str, 0, 1) = 't' if $mode & 01000;     # 4775 = rwsrwxr-x
substr ($str, 3, 1) = 's' if $mode & 02000;     
substr ($str, 6, 1) = 's' if $mode & 04000;
return reverse $str;

}

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub convert_date {
        my $date = shift;
        my $str = '';

# localtime form:       Sun Oct 28 18:00:22 2001

my @d = localtime $date;
$str = localtime $date;

# 'Mmm dd hh:mm'
# 'Mmm dd  yyyy' if > 6 mos old

if ($date - time > 6 * 30 * 86400) {
        $str = sprintf "%-3.3s %2u  %4u", substr ($str, 4, 3), $d[3],
          $d[5] + 1900;
} else {
        $str = sprintf "%-3.3s %2u %02u:%02u", substr ($str, 4, 3), $d[3],
          $d[2], $d[1];
}

}

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

-- 
   ,-/-  __      _  _         $Bill Luebkert   ICQ=162126130
  (_/   /  )    // //       DBE Collectibles   Mailto:[EMAIL PROTECTED]
   / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/

_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to