File Coverage

bin/printpath
Criterion Covered Total %
statement 31 82 37.8
branch 2 18 11.1
condition n/a
subroutine 6 7 85.7
pod n/a
total 39 107 36.4


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