Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find out if an executable is in my path 1

Status
Not open for further replies.

wardy66

Programmer
Jun 18, 2002
102
AU
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

Code:
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
 
Tia,

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
 
Code:
# this wont do ?
@paths = split (/;/, $ENV{PATH});
foreach (@paths) { print "$_\n"; }
---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Tia,

This small sub will probably be something you could start with:
[tt]

$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));
}
}
[/tt]

Note that this will only work if you provide the full name, including extension.

Thierry
AD AUGUSTA PER ANGUSTA

Thierry
 
Well, seems my original fears are confirmed. I was hoping I had missed something :-(

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 [ponder]
 
Tia,

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
 
Thanks guys

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
Code:
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
Code:
/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
 
Although this thread is quite old by now, I though I would close it by suggesting the following solution:

use File::Which;
my $exe_path = which('perldoc');

Hope this ever helps somebody in need.
AD AUGUSTA PER ANGUSTA

Thierry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top