-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_cleanpath
executable file
·122 lines (81 loc) · 2.24 KB
/
_cleanpath
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env perl -w
#
# Returns $PATH with duplicates removed.
#
# 2006-??: Written by Steven J. DeRose.
# 2010-09-12, 2012-10-05, 2015-09-19 sjd: Cleanup.
#
# To do:
#
use strict;
use Getopt::Long;
our $VERSION_DATE = "2015-09-19";
my $printablyVerbose = 0;
my $quiet = 0;
my $verbose = 0;
###############################################################################
# Process options
#
my %getoptHash = (
"h|help|?" => sub { system "perldoc $0"; exit; },
"q|quiet!" => \$quiet,
"v|verbose+" => \$verbose,
"pv" => \$printablyVerbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
},
);
Getopt::Long::Configure ("ignore_case");
GetOptions(%getoptHash) || die("Bad options.\n");
###############################################################################
# Main
#
my @p = split(":",$ENV{PATH});
my $pcount = scalar @p;
my $newpath = "";
my %seen = ();
my $kept = 0;
for (my $i=0; $i<$pcount; $i++) {
if (!$seen{$p[$i]}) {
$seen{$p[$i]}++;
$newpath .= "$p[$i]:";
$kept++;
}
}
$newpath =~ s/:$//;
if ($verbose) {
warn "Original items: $pcount, final: $kept.\n";
}
if ($printablyVerbose) {
warn "Original: $pcount, final: $kept.\n";
(my $p = $newpath) =~ s/:/\n /g;
warn " $p\n";
}
print "$newpath";
exit;
###############################################################################
###############################################################################
#
=pod
=head1 Usage
_cleanpath [options] [file]
Return $PATH, but with duplicates removed.
=head1 Options
=over
=item * B<-q> OR B<--quiet>
Suppress most messages.
=item * B<--pc>
Like I<-v>, but show final PATH legibly.
=item * B<-v> OR B<--verbose>.
Add more detailed messages.
=item * B<--version>
Display version info and exit.
=back
=head1 Notes
Must access from an alias or shell function to actually change \$PATH.
=head1 Ownership
This work by Steven J. DeRose is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, see L<here|"http://creativecommons.org/licenses/by-sa/3.0/">.
For the most recent version, see L<here|"http://www.derose.net/steve/utilities/">.
=cut