#!/usr/local/bin/perl $VERSION = '0.03'; use warnings; use strict; use Pod::Usage; use Getopt::Long qw(:config gnu_compat no_ignore_case no_debug); my %OPT = ( 'usage' => undef , 'no-current-dir' => undef , 'usable-dir-only' => undef ) ; set_options(); printf "%s\n" , make_path( @ARGV ) ; exit; sub make_path { my (@paths) = @_; my %uniq; @paths = grep !$uniq{$_}++ , map split( /:+/ , $_ ) , @paths; return join ':' , map { ( $OPT{'no-current-dir'} && $_ eq '.' ) || ( $OPT{'usable-dir-only'} && !( -d $_ && -x _ ) ) ? () : $_ } @paths ; } sub set_options { GetOptions ( 'h|usage|help' => \$OPT{'usage'} , 'c|s|sanity-check' => \$OPT{'usable-dir-only'} , 'n|no-current-dir' => \$OPT{'no-current-dir'} ) || die pod2usage('-exitval' => 2 , '-verbose' => 1); pod2usage('-exitval' => 0 , '-verbose' => 3) if $OPT{'usage'}; unless ( scalar @ARGV ) { pod2usage( '-message' => 'No arguments given and PATH value is empty' , '-exitval' => 1 , '-verbose' => 1 ) unless $ENV{'PATH'}; print "Using: ", $ENV{'PATH'} , "\n"; push @ARGV , $ENV{'PATH'}; } } __END__ =pod =head1 NAME cleanse-path - remove duplicate entries from given path(s). =head1 SYNOPSIS cleanse-path [ -no-current-dir | -n ] [ -sanity-check | -c ] \ [ /path:/entries [ /more:/path ] ] To remove current directory from $PATH ... cleanse-path -no-current-dir To remove '/to' (and get "/path:/to:/cleanse:.") ... cleanse-path /path:/to /cleanse:/to:. To remove current directory (and get "/path:/to:/cleanse") ... cleanse-path -no-current-dir /path:/to:/cleanse:/to:. =head1 DESCRIPTION This program removes duplicate entries from given path(s) and produces a (path) string in the order each entry appread in given arguments. If the program is run without any arguments, value of environment variable C is used. =head1 OPTIONS =over 2 =item B<-help> | B<-usage> Shows this message. =item B<-no-current-dir> | B<-n> Remove current (.) directory. (Default is to keep.) =item B<-sanity-check> | -B<-c> Keep only those path entries which are directories and are executable (in other words, searchable). =back =head1 ENVIRONMENT If no arguments are given PATH value is used. =head1 ERROR CONDITION If no arguments are given or PATH is empty, program exits with error. =head1 BUGS Let me know. =head1 SEE ALSO (There must be something.) =head1 AUTHOR, LICENSE, DISTRIBUTION, ETC. Parv, parv(at)pair(dot)com MODIFIED: Feb 12 2006 This software is free to be used in any form only if proper credit is given. I am not responsible for any kind of damage or loss. Use it at your own risk. =cut