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