×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to find out if an executable is in my path

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

RE: How to find out if an executable is in my path

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

RE: How to find out if an executable is in my path

# this wont do ?
@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

Tia,

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

(OP)
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

RE: How to find out if an executable is in my path

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

RE: How to find out if an executable is in my path

Tia,

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

(OP)
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 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

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close