Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Pathfinder - find duplicate (shadowed) programs in your PATH

by merlyn (Sage)
on Aug 26, 2000 at 09:12 UTC ( [id://29787]=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info Randal L. Schwartz - merlyn
Description: Run this program (no arguments) and see which items in your PATH environment setting are shadowing later programs of the same name. This is an indication that you might get failures running the scripts of others, or perhaps if you ever rearrange your PATH.
    #!/usr/bin/perl -w
    use strict;

    my @path = split /:/, $ENV{PATH};
    my %path_inodes;
    my @clean_path;

    for (@path) {
      next unless m#^/#;
      my ($dev,$ino) = stat;
      next unless defined $dev;
      my $key = "$dev $ino";
      if (exists $path_inodes{$key}) {
        print "warning: $_ is linked to $path_inodes{$key}\n";
        next;
      }
      $path_inodes{$key} = $_;
      push @clean_path, $_;
    }

    my %progs;

    ## print "clean path is @clean_path\n";

    for my $dir (@clean_path) {
      use DirHandle;
      my @files =
        sort grep !/^\.\.?$/,
        DirHandle->new($dir)->read;
      ## print "$dir: @files\n";
      for my $file (@files) {
        if (exists $progs{$file}) {
          print "$file in $dir is shadowed by $progs{$file}\n";
          next;
        }
        $progs{$file} = $dir;
      }
    }
Replies are listed 'Best First'.
Re: Pathfinder - find duplicate (shadowed) programs in your PATH
by toolic (Bishop) on Jul 20, 2009 at 21:22 UTC
    I like how the code identifies duplicate PATH directories by using the inode. The system administrators who set up the environment I use at $work have added in a few duplicate directories. In my case, they are not links; so the warning message is a little misleading, but the important thing is that I get some kind of warning.

    Currently, the code only filters out the specially-name directories (. and ..). I adapted the code to filter out all directories which are sub-directories of each PATH directory. Also, I filter out all files which do not have execute permissions.

    To add these filters, change:

    sort grep !/^\.\.?$/,

    to:

    sort grep {-x "$dir/$_"} grep {! -d "$dir/$_"}

      Does grep {  ! -d "$dir/$_" && -x _ } not work?

      Few hours later, changed second "$dir/$_" with _.

      Thanks to toolic for catching the missing quote that I had lost during editing.

        Does grep { ! -d "$dir/$_ && -x _ } not work?
        Yes, I believe your Other Way To Do It is functionally equivalent to my code (assuming you close the quotes, of course). Your version has fewer keystrokes, and it may even run faster since it has one fewer grep, if one were so inclined to benchmark it. It produced the same results as my code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://29787]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-23 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found