File Coverage

bin/printpath
Criterion Covered Total %
statement 39 91 42.8
branch 4 22 18.1
condition n/a
subroutine 8 9 88.8
pod n/a
total 51 122 41.8


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2             #ABSTRACT: check for binary in your $PATH variable an nicely print the results
3             #PODNAME:printpath
4 1     1   7188 use v5.12;
  1         5  
5 1     1   7 use warnings;
  1         1  
  1         71  
6 1     1   834 use Term::ANSIColor;
  1         18898  
  1         202  
7 1     1   1091 use Getopt::Long;
  1         29270  
  1         10  
8 1     1   988 use FindBin qw($RealBin);
  1         1748  
  1         289  
9 1 50       122189 if (-e "$RealBin/../dist.ini") {
  0         0  
10 1 50       8 say STDERR "[dev mode] Using local lib" if ($ENV{"DEBUG"});
11 1     1   571 use lib "$RealBin/../lib";
  1         1020  
  1         10  
12             }
13 1     1   934 use NBI::Slurm;
  1         5  
  1         108929  
14             # Define help function
15             sub show_help {
16 1     1   7 print color('bold white');
17 1         56 print "Usage: $0 [options] [binary1 binary2 ...]\n\n";
18 1         3 print color('reset');
19            
20 1         58 print "Options:\n";
21 1         2 print " -h, --help Show this help message and exit\n\n";
22            
23 1         3 print "Description:\n";
24 1         3 print " This script displays all directories in your \$PATH, one per line.\n";
25 1         3 print " If binary names are provided as arguments, it checks each directory\n";
26 1         2 print " for those binaries and indicates whether they are executable.\n";
27 1         2 print " It also verifies if the binaries are regular executables or aliases/functions.\n\n";
28            
29 1         3 print "Examples:\n";
30 1         5 print " $0 # List all PATH directories\n";
31 1         5 print " $0 ls python vim # Check for these binaries in PATH\n";
32            
33 1         98 exit(0);
34             }
35              
36             # Process command line options
37 1         3 my $help = 0;
38             GetOptions(
39             "help|h" => \$help,
40 0     0   0 'version' => sub { say "printpath v", $NBI::Slurm::VERSION; exit }
  0         0  
41 1 50       40 ) or show_help();
42              
43             # Show help if requested
44 1 50       875 show_help() if $help;
45              
46             # Get the directories from the PATH environment variable
47 0         0 my @dirs = split /:/, $ENV{PATH};
48 0         0 my @binaries = @ARGV; # Get binary names from command-line arguments
49              
50             # Print header
51 0         0 print color('bold cyan');
52 0         0 print "=== PATH Directories ===\n";
53 0         0 print color('reset');
54              
55             # Process each directory
56 0         0 foreach my $dir (@dirs) {
57             # Print the directory name
58 0         0 print color('green');
59 0         0 print "$dir\n";
60 0         0 print color('reset');
61            
62             # Check for requested binaries in this directory
63 0 0       0 if (@binaries) {
64 0         0 foreach my $binary (@binaries) {
65 0         0 my $full_path = "$dir/$binary";
66            
67 0 0       0 if (-e $full_path) {
68 0         0 print " ";
69            
70             # Check if the file is executable
71 0 0       0 if (-x $full_path) {
72 0         0 print color('yellow');
73 0         0 print "✓ ";
74             } else {
75 0         0 print color('red');
76 0         0 print "! ";
77             }
78            
79 0         0 print color('magenta');
80 0         0 print "$binary\n";
81 0         0 print color('reset');
82             }
83             }
84             }
85             }
86              
87             # Check if the binaries might be aliases or something else
88 0 0       0 if (@binaries) {
89 0         0 print "\n";
90 0         0 print color('bold cyan');
91 0         0 print "=== Command Verification ===\n";
92 0         0 print color('reset');
93            
94 0         0 foreach my $binary (@binaries) {
95 0         0 print color('blue');
96 0         0 print "$binary: ";
97 0         0 print color('reset');
98            
99             # Use command -v to check what the binary actually is
100 0         0 my $result = `command -v $binary 2>/dev/null`;
101 0         0 chomp($result);
102            
103 0 0       0 if ($result) {
104             # Check if it's in PATH or possibly an alias/function
105 0         0 my $in_path = 0;
106 0         0 foreach my $dir (@dirs) {
107 0 0       0 if ($result eq "$dir/$binary") {
108 0         0 $in_path = 1;
109 0         0 last;
110             }
111             }
112            
113 0 0       0 if ($in_path) {
114 0         0 print color('green');
115 0         0 print "found in PATH: $result\n";
116 0         0 print color('reset');
117             } else {
118 0         0 print color('yellow');
119 0         0 print "found as alias or function: $result\n";
120 0         0 print color('reset');
121             }
122             } else {
123 0         0 print color('red');
124 0         0 print "not found\n";
125 0         0 print color('reset');
126             }
127             }
128             }
129              
130             __END__