whence
October 5, 2010 Leave a comment
Ever wonder if a command or file is in your path? Here’s a handy perl script to answer that question:
#!/usr/bin/perl
##################################################################
## whence ##
## ##
## Search PATH for a filename. If you're wondering where ##
## a file or command is being sourced from, or if you want to ##
## know whether a file is in your path: ##
## ##
## whence <filename> ##
## ##
## CLH 5-Oct-2010 ##
##################################################################
if (@ARGV != 1) {
print "\nusage: whence <filename>\n\n";
print "Search PATH for specified filename " .
"and return all occurrences.\n\n";
exit 0;
}
use constant TRUE=>1;
use constant FALSE=>0;
$p=shift;
$found = FALSE;
foreach $path (split(/:/,$ENV{'PATH'})) {
$path =~ s/\/$//;
$find = $path . '/' . $p;
if (-e $find) {
print "$find\n";
$found = TRUE;
}
}
if ($found == FALSE){
print "$p was not found in PATH.\n";
}
exit 1;