Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to emulate the Unix command "which"

Reply
Thread Tools

How to emulate the Unix command "which"

 
 
Leon Nabot
Guest
Posts: n/a
 
      08-19-2005
Hello,

I try to find a quick solution to find onto the PATH where is located an
executable as "which" do.

I have tried to use the "which" command into a pipe or backtick but the
returns are not the same in all Unixes, in particular if the searched
command is aliased, .

Anyone would know an easy way to do this ?

For the moment the only solution seems to write a module which browse
each directory of the PATH searching the file.

We need a solution that works only with the defaults functions/libs of a
Perl 5.005 (And up)

Rgds.

Léo


 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      08-19-2005
Leon Nabot <nabotleon@hotmail_N0_SPAMZ.com> wrote in comp.lang.perl.misc:
> Hello,
>
> I try to find a quick solution to find onto the PATH where is located an
> executable as "which" do.
>
> I have tried to use the "which" command into a pipe or backtick but the
> returns are not the same in all Unixes, in particular if the searched
> command is aliased, .


If it reports aliases it isn't the external /usr/bin/which (or whatever),
but the shell builtin. Aliases are in she shell's workspace, external
programs don't know about them.

So you have essentially three choices of which "which" to use: The
external one, the one in the csh family and the one in the sh family.

> Anyone would know an easy way to do this ?


It isn't hard, but there is no trick that makes it particularly easy.
Walk through the $PATH directories and look for files with the right
name that are executable by the invocant. You can't report aliases
that way.

Alternatively you can use the external "which", or one of the shell's
builtins and post-process the output depending on the OS you're on.
The shell builtins may be more regular than it appears when you take
care to use the same shell everywhere.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
 
chris-usenet@roaima.co.uk
Guest
Posts: n/a
 
      08-19-2005
Leon Nabot <nabotleon@hotmail_n0_spamz.com> wrote:
> I try to find a quick solution to find onto the PATH where is located an
> executable as "which" do.


> For the moment the only solution seems to write a module which browse
> each directory of the PATH searching the file.


A quick way for a single command is:

foreach (split(/:/,$ENV{PATH})) { print "Found $_/rsh\n" if -x "$_/rsh" }


Or, if you want a fancier version:

my %what = (
ls => undef,
rsh => undef,
rfc => undef,
);

foreach my $path (reverse split (/:/, $ENV{PATH})) {
$path = '.' if $path eq '';
foreach my $cmd (keys %what) {
my $file = "$path/$cmd";
$what{$cmd} = $file if -x $file;
}
}

use Data:umper;
print Dumper (\%what), ".\n";

Chris
 
Reply With Quote
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      08-19-2005
Leon Nabot <nabotleon@hotmail_n0_spamz.com> wrote:
> I try to find a quick solution to find onto the PATH where is located an
> executable as "which" do.


> I have tried to use the "which" command into a pipe or backtick but the
> returns are not the same in all Unixes, in particular if the searched
> command is aliased, .


How are you specifying 'which' - in other words, which 'which' are
you using. It can appear as a shell built in command in tcsh which
will pick up aliases. However the standalone version (maybe
/usr/bin/which depending on your system) will not pick up aliases.

Axel


 
Reply With Quote
 
Ilya Zakharevich
Guest
Posts: n/a
 
      08-19-2005
[A complimentary Cc of this posting was sent to

<(E-Mail Removed)>], who wrote in article <(E-Mail Removed)>:
> Leon Nabot <nabotleon@hotmail_n0_spamz.com> wrote:
> > I try to find a quick solution to find onto the PATH where is located an
> > executable as "which" do.

>
> > For the moment the only solution seems to write a module which browse
> > each directory of the PATH searching the file.

>
> A quick way for a single command is:
>
> foreach (split(/:/,$ENV{PATH})) { print "Found $_/rsh\n" if -x "$_/rsh" }


Splitting PATH on /:/ is wrong. Use $Config{path_sep} instead.

Hope this helps,
Ilya
 
Reply With Quote
 
Leon Nabot
Guest
Posts: n/a
 
      08-19-2005

Thank's to all.

The solution suggested by Chris is just good for me (One line of code)

I give up the use of the system "which" command because it seems that on
Solaris even the standalone version reports the alias (When in Linux and
HPUX it reports the real path)

Rgds.

Léo


> Hello,
>
> I try to find a quick solution to find onto the PATH where is located an
> executable as "which" do.
>
> I have tried to use the "which" command into a pipe or backtick but the
> returns are not the same in all Unixes, in particular if the searched
> command is aliased, .


 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      08-19-2005
Leon Nabot <nabotleon@hotmail_N0_SPAMZ.com> wrote in news:4305ce66$0
$4171$(E-Mail Removed):

[ Please do not top-post.
Please quote the article you are replying to.
]

> The solution suggested by Chris is just good for me (One line of code)


Is that really a good criterion?

Let's review the solution:

> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in
> news:(E-Mail Removed):
>
>> A quick way for a single command is:
>>
>> foreach (split(/:/,$ENV{PATH})) { print "Found $_/rsh\n" if -x
>> "$_/rsh" }


This, IMHO, is needlessly platform dependent. One can make this fairly
portable by using File::Spec:

#!/usr/bin/perl

use strict;
use warnings;

use File::Spec::Functions qw'catfile path';

use constant LOOK_FOR => shift;

unless( LOOK_FOR ) {
die "Please provide the name of the command you want to find\n";
}

my @path = path;

for my $dir ( @path ) {
my $fn = catfile $dir, LOOK_FOR;
print "Found $fn\n" if -x $fn;
}

__END__

D:\Home\asu1\UseNet\clpmisc> mywhich perl.exe
Found C:\opt\Perl\bin\perl.exe
Found C:\opt\cygwin\bin\perl.exe

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
usenet@isbd.co.uk
Guest
Posts: n/a
 
      08-19-2005
Leon Nabot <nabotleon@hotmail_n0_spamz.com> wrote:
>
> Thank's to all.
>
> The solution suggested by Chris is just good for me (One line of code)
>
> I give up the use of the system "which" command because it seems that on
> Solaris even the standalone version reports the alias (When in Linux and
> HPUX it reports the real path)
>

Yes, I think the standalone 'which' on Solaris does wierd things like
looking at what shell you are running and then trying to parse the
relevant files to find aliases as well as actual executables on the
PATH.

--
Chris Green

 
Reply With Quote
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      08-19-2005
A. Sinan Unur <(E-Mail Removed)> wrote:
>>> A quick way for a single command is:


>>> foreach (split(/:/,$ENV{PATH})) { print "Found $_/rsh\n" if -x
>>> "$_/rsh" }


> This, IMHO, is needlessly platform dependent. One can make this fairly
> portable by using File::Spec:


> #!/usr/bin/perl


> use strict;
> use warnings;


> use File::Spec::Functions qw'catfile path';


> use constant LOOK_FOR => shift;


Why do you use this form rather than a normal variable?

Axel

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      08-19-2005
(E-Mail Removed) wrote in
news:9GkNe.6860$(E-Mail Removed):

> A. Sinan Unur <(E-Mail Removed)> wrote:
>>>> A quick way for a single command is:

>
>>>> foreach (split(/:/,$ENV{PATH})) { print "Found $_/rsh\n" if -x
>>>> "$_/rsh" }

>
>> This, IMHO, is needlessly platform dependent. One can make this
>> fairly portable by using File::Spec:

>
>> #!/usr/bin/perl

>
>> use strict;
>> use warnings;

>
>> use File::Spec::Functions qw'catfile path';

>
>> use constant LOOK_FOR => shift;

>
> Why do you use this form rather than a normal variable?


For variety

Actually, to match the way the command line argument is used in the
script: It is a value given from the outside which the script will not
modify.

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Run Unix shell command $ parse command line arguments in python rkoida@yahoo.com Python 4 04-23-2005 04:42 AM
Perl CGI script to emulate a shell command line window Alain Star Perl Misc 7 01-18-2005 03:20 PM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C Programming 18 01-10-2005 03:35 AM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C++ 12 01-10-2005 03:35 AM



Advertisments