Link to home
Start Free TrialLog in
Avatar of jchristena
jchristena

asked on

Why do Perl 5 backtick (` `) calls fail on Linux only?

Well, I don't really know what to make of this.  I have run this script on 3 different RedHat boxes, all with the same result.  The same script obviously runs fine on any other Unixy type interp(AIX, HP, Solaris, Windows BASH), but SHOULD run on Linux bash or any other shell for that matter...even if you get a failure.

#!/usr/bin/perl

$binpath = `type perl`;  #don't really care if we get the parsed path here...just want SOMETHING
print "binpath = $binpath\n";

OUTPUT:
binpath=


I just don't get it.   This SHOULD work and is very simple.    It works from the CLI, and I am using ActiveState 5.8.6.  It does the same thing on 5.6.  Please don't spend any unnecessary time on this, but if you have any ideas off the top of your head...

I am using RedHat 3.0 AS.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of jmcg
jmcg
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
Did you want `which perl` ?
Avatar of Tintin
Tintin

Your script could not have worked on AIX, HP, Solaris, unless you had a script or binary called 'type', and that isn't a standard command on any of those Unix flavours.

jmcg has given you the reason why it doesn't work.

What are you actually trying to acheive?
Avatar of jchristena

ASKER

It did work on the other platforms, and I think I have a reason for it.  The default path on my Linux boxes consists of essentially /usr/sbin and thats it.  The other boxes contain /usr/bin and /bin by default which contain the bash and sh binaries.  If I explicitly specify a shell:

$var = `bash -c \"type perl\"`;

everything works fine.    In the end, it comes down to the fact that jmcg is correct.  Thanks for all the responses.

That seems like an odd default path to have. Lots of stuff is not going to work if you can only call on commands present in /usr/sbin.

My favorite method for finding perl is the one I use on shebang lines:

#! /usr/bin/env perl

I needed to go to this when I worked in an environment where I wanted to share my scripts but different machines had been set up with different conventions on finding Perl: some in /usr/bin/perl, some in /usr/local/bin/perl, and, on one machine, /var/local/bin/perl.

You haven't explained quite what you're trying to do, but rather than call an external command, it might be simpler to do:

   for (map {"$_/perl"} split /:/, $ENV{PATH}) { if( -x $_) {$binpath=$_; last}}