Dup Ver Goto 📝

tidypath

PT2/snippets/perl does not exist
To
47 lines, 186 words, 1001 chars Page 'tidypath' does not exist.

A little script for adding entries to the path and removing duplicates. If you care to, you can then wrap these in bash function()s that update the actual $PATH

#!/usr/bin/perl

use Cwd 'abs_path';

@p = ();
%p = ();
$conf = 1;

for(@ARGV) {
  if( /^-f$/ ) {
    # force inclusion of non-existent paths
    $conf = 0;
    next;
  }
  if( /^-p$/ ) {
    # splice in current PATH
    @a = split ":", $ENV{'PATH'};
    for $a(@a) {
      # force absolute paths (unless -f is active and a path does not exist
      if( -d "$a" ) { $a = abs_path($a); } elsif( $conf ) { next; }

      if( ! $p{$a} ) {
        push @p, $a;
        $p{$a} = 1;
      }
    }
    next
  } 
  @a = split ":";
  for $a(@a) {
    # force absolute paths (unless -f is active and a path does not exist
    if( -d "$a" ) { $a = abs_path($a); } elsif( $conf ) { next; }

    # add it if not already present
    if( ! $p{$a} ) {
      push @p, $a;
      $p{$a} = 1;
    }
  }
}

$path = join ":", @p;
print "$path\n";