• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

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.
0
jchristena
Asked:
jchristena
1 Solution
 
jmcgOwnerCommented:
The 'type' command is a builtin in only some shells. Unless you have a separate command named 'type' in your PATH, perl is going to look at that string and, seeing no shell metacharacters, try to exec a command named 'type', and silently fail.
0
 
ozoCommented:
Did you want `which perl` ?
0
 
TintinCommented:
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?
0
 
jchristenaAuthor Commented:
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.

0
 
jmcgOwnerCommented:
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}}

0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now