Dup Goto 📝

PerlScripts

PT2/aw/os/shell 07-31 13:46:42
To Pop
74 lines, 212 words, 1437 chars Monday 2023-07-31 13:46:42

For when we don't want the the passthru behaviour of BashSh (and ZshZh when setopt +o nomatch is set) but would rather it simply drop failed matches:

#!/usr/bin/env perl

use strict;
for(@ARGV) {
  if(stat) { print "$_\n"; }
}

example use case

filterstat *.mkv *.webm *.mp4 # will return a list of files matching these wildcards

whereas with e.g. bash and ls

ls *.mkv *.webm *.mp4

will, if there are no .mp4 files, complain with

ls: cannot access '*.mp4': No such file or directory

For seeing of one of a list of filenames exists (no arguments means fail)

#!/usr/bin/env perl

use strict;
for(@ARGV) {
  if(stat) { exit(0); }
}
exit(1);

For seeing if all of a list of filenames exist (no arguments means success)

#!/usr/bin/env perl

use strict;
for(@ARGV) {
  if(!stat) { exit(1); }
}
exit(0);

For finding the newest file of those listed

#!/usr/bin/env perl

use strict;
my($newest,$newest_mtime);
$newest_mtime = -1;
$newest = "";
for(@ARGV) {
  if(my @stat = stat) { 
    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
        $atime,$mtime,$ctime,$blksize,$blocks)
           = @stat;
    if($mtime > $newest_mtime) {
      $newest = $_;
      $newest_mtime = $mtime;
    }
  } 
}
if($newest) {
  print "$newest\n";
} else {
  die("No matching files");
}

Example usage:

newest *.mp4