-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckPath
executable file
·156 lines (114 loc) · 3.62 KB
/
checkPath
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env perl -w
#
# checkpath: Test the PATH env. var for potential problems.
#
use strict;
use Getopt::Long;
our %metadata = (
'title' => "checkPath",
'description' => "Test the PATH env. var for potential problems.",
'rightsHolder' => "Steven J. DeRose",
'creator' => "http://viaf.org/viaf/50334488",
'type' => "http://purl.org/dc/dcmitype/Software",
'language' => "Perl 5.18",
'created' => "2007",
'modified' => "2022-02-03",
'publisher' => "http://github.com/sderose",
'license' => "https://creativecommons.org/licenses/by-sa/3.0/"
);
our $VERSION_DATE = $metadata{'modified'};
our $VERSION_DATE = "2013-10-08";
=pod
=head1 Usage
checkpath [options] [file]
Checks \$PATH for problems such as missing directories, directories listed
more than once, or directories with no executable files. Also checks that
it contains several directories it typically should, and that entries
do not end in "/".
These are not necessarily errors, but you should probably know I<why> any of
them is the case.
=head1 Options
=over
=item * B<--list>
Show each directory as it is checked.
=item * B<--quiet> OR B<-q>
Suppress most messages.
=item * B<--verbose> OR B<-v>
Add more detailed messages.
=item * B<--version>
Display version info and exit.
=back
=head1 Related commands
Several shell functions defined in my F<BashSetup/path_utils>, for re-ordering,
displaying, uniqifying, etc.
C<env> -- prints out environment variables (such as \$PATH) legibly.
=head1 History
2007-??: Written by Steven J. DeRose.
2007-11-08 sjd: strict, Getopt, fix -d test.
2010-09-17 sjd: Cleanup. -list.
2012-09-05 sjd: Cleanup.
2013-10-08: Also check for trailing slash and dups.
2022-02-03: New layout.
=head1 To do
Quote all printed paths, or escape spaces.
=head1 Rights
Copyright 2007 by Steven J. DeRose. This work is licensed under a
Creative Commons Attribution-Share Alike 3.0 Unported License.
For further information on this license, see
L<https://creativecommons.org/licenses/by-sa/3.0>.
For the most recent version, see L<http://www.derose.net/steve/utilities> or
L<https://github.com/sderose>.
=cut
my @required = (
"/bin", "/sbin",
"/usr/bin", "/usr/sbin",
"/usr/local/bin", "/usr/local/sbin"
);
my $list = 0;
my $quiet = 0;
my $verbose = 0;
###############################################################################
# Process options
#
my %getoptHash = (
"h|help|?" => sub { system "perldoc $0"; exit; },
"list!" => \$list,
"q|quiet!" => \$quiet,
"v|verbose+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
},
);
Getopt::Long::Configure ("ignore_case");
GetOptions(%getoptHash) || die("Bad options.\n");
###############################################################################
# Main
#
print "Checking your PATH for missing directories or executables...\n";
my @places = split(":",$ENV{PATH});
my %seen = ();
foreach my $x (@places) {
($list || $verbose) && print "Checking dir $x\n";
if ($x =~ m|/$|) {
print "$x ends with a slash, makes brew &c unhappy.\n";
}
if (defined $seen{$x}) {
print "Duplicate entry for $x.\n";
next;
}
$seen{$x}++;
if (!-d $x) {
print "$x is not a directory.\n";
next;
}
my $execs = `ls -F $x 2>/dev/null | grep '*'`;
if ($execs eq "") {
print "$x contains no executables.\n";
}
}
for my $x (@required) {
if (!defined $seen{$x}) {
print "PATH does not contain expected dir '$x'\n";
}
}
print "checkpath: done.\n";