How to find out if an executable is in my path
How to find out if an executable is in my path
(OP)
Hello all
I am converting some old shell scripts into Perl (yay!) and have come up with a small snag.
The old UNIX scripts do things like
if ! whence symdg > /dev/null; then
echo "symdg not found in path"
return 1
fi
I also want this stuff to work on NT/2000
so the shell builtin "whence" is no good.
Is there a nice way to do this in Perl, short of skipping through $ENV{PATH} entries?
I tried using require and friends but they only seem concerned with @INC, ie, modules and not executables.
TIA
I am converting some old shell scripts into Perl (yay!) and have come up with a small snag.
The old UNIX scripts do things like
if ! whence symdg > /dev/null; then
echo "symdg not found in path"
return 1
fi
I also want this stuff to work on NT/2000

Is there a nice way to do this in Perl, short of skipping through $ENV{PATH} entries?
I tried using require and friends but they only seem concerned with @INC, ie, modules and not executables.
TIA
RE: How to find out if an executable is in my path
Very good question...
I have the same problem on NT.
This is not a solution, but just my ideas to solve the problem:
- find a NT version of "which"
- write a sub-routine to search through the path.
I would prefer the NT which solution as I see the possible problems with the path search:
- performance
- lowercase/upercase
- extensions: is notepad the same as notepad.exe
Never the less it could be worth while to try the sub.
AD AUGUSTA PER ANGUSTA
Thierry
RE: How to find out if an executable is in my path
@paths = split (/;/, $ENV{PATH});
foreach (@paths) { print "$_\n"; }
---------------------------------------

someone knowledge ends where
someone else knowledge starts
RE: How to find out if an executable is in my path
This small sub will probably be something you could start with:
$exe = 'perl.exe';
if (&Which($exe)) {
print "$exe was found in path\n";
} else {
print "$exe was NOT found in path\n";
}
sub Which {
# Get the passed value
$program = shift;
# Return if nothing is provided
return if (not defined($program));
# Load the ENV
use ENV;
# Load the path
$path = $ENV{PATH};
# Let's replace all \ by /
$path =~ s/\\/\//g;
# Substitute all /; by ; as there could be some trailing / in the path
$path =~ s/\/;/;/g;
# Now make an array
@path = split(/;/,$path);
# Loop and find if the file is in one of the paths
foreach (@path) {
# Concatenate the file
my $file = $_ . '/' . $program;
# Return the path if it was found
return $file if ((-e $file) && (-f $file));
}
}
Note that this will only work if you provide the full name, including extension.
Thierry
AD AUGUSTA PER ANGUSTA
Thierry
RE: How to find out if an executable is in my path
Thanks for the suggestions, especially Thierry's subroutine. However, as noted, we need to be careful of case and .exe .bat whatever extensions
Is it possible to use the open call to do it, I wonder? Things like system() and open(|) ask the shell (or NT/2000) to find things.
Can we ask the "shell" to do all the hard work for us
RE: How to find out if an executable is in my path
I did find a which.exe included in MKS toolkit, but unfortunatly this is not GPL.
I'll keep on browsing....
AD AUGUSTA PER ANGUSTA
Thierry
RE: How to find out if an executable is in my path
I did find a which.exe at http://www.edv.agrar.tu-muenchen.de/~syring/win32/UnxUtils.html
It seems to work fine, but it does need complete name as:
which eventvwr.exe
returns:
C:\WINNT\system32\eventvwr.exe
I would be easy to include this in a perl script.
AD AUGUSTA PER ANGUSTA
Thierry
RE: How to find out if an executable is in my path
I really want to get away from worrying about whether I'm on windoze or not.
But thanks for the input. It looks like it might be best to just walk the PATH variable
But, of course, the windoze one looks like C:\Perl\bin\;C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;"C:\Program Files\PKWARE\PKZIPC\";C:\binaries and can take .exe as an optional suffix but UNIX looks like /usr/bin:/bin
My version of windoze Perl seems to have an environment variable called PATHEXT, which is all the valid, executable suffixes.
If I get around to writing the subroutine, I'll post back.
Thanks again
RE: How to find out if an executable is in my path
use File::Which;
my $exe_path = which('perldoc');
Hope this ever helps somebody in need.
AD AUGUSTA PER ANGUSTA
Thierry