9

I have a question about how to execute the perl file inside of a shell script

I have 2 files now, "test.sh" and "test.pl", here are example of my scripts

SHELL script

#!/bin/bash
perl FILEPATH/test.pl
......

PERL script

#!/usr/bin/perl
my $a = "hello"
sub saysomething
{
    print $a;
}
.....

The way I call the shell script is : under the path of shell scripts, execute "./test.sh"

All mentioned above are working under the environment GUN bash, version 4.2.24(1)-release (i686-pc-linux-gnu) + perl (v5.14.2)

But if I put those scripts on server (which I couldn't change the bash / perl version) GNU bash, version 4.2.10(1)-release (x86_64-pc-linux-gnu) + perl (v5.12.4), I got the followign message:

FILEPATH/test.pl: line 2: my: command not found

Does anybody know how can I solve this problem?

BTW, if I execute the perl script individually (perl FILEPATH/FILENAME.pl), it works perfectly.

10
  • is FILEPATH an absolute or relative path? Should be absolute Commented Apr 15, 2013 at 22:37
  • Is FILEPATH supposed to be an environment variable? If so, then it should be $FILEPATH. Commented Apr 15, 2013 at 22:38
  • 1
    You said I have 2 files: test.sh & test.pl, so what's filename.pl ? Commented Apr 15, 2013 at 22:45
  • 2
    Are you sure that the first line of your Perl script is #!/usr/bin/perl? If you're going to show us code, it's best to copy-and-paste it; if you re-type it, you risk losing vital information. Commented Apr 15, 2013 at 22:49
  • 1
    Is there any way that the perl in perl FILEPATH/test.pl can be 'lost'. For example, you might have ${PERL-perl} instead of just perl (or the better ${PERL:-perl}), or you have alias perl='' or something equally weird? The error you're seeing indicates that the shell is executing the file, not Perl. And the only way I can see for that to happen is there's some hidden change to the code. Commented Apr 16, 2013 at 6:04

4 Answers 4

4

In order to execute a perl script by .sh script you dont need to use perl prefix, but only:

#!/bin/sh

/somewhere/perlScript.pl

It will work without problem.

Sign up to request clarification or add additional context in comments.

2 Comments

This presupposes that the script has executable permission and a proper shebang. You should properly explain this; but then, yours will basically be a duplicate of one of the existing answers.
any other ideas for the same .
2

This problem is at least two-fold. One, you have to have the location of Perl in your environment PATH. Two, the location of Perl may be different on different machines. One solution to both problems that I, and others, have used for years is to make use of a "magic header" of some sort at the top of Perl programs. The header identifies itself as a sh shell script and leverages the fact that /bin/sh exists in every version/flavor of Linux/UNIX. The header's job is to fortify the PATH with various possible Perl locations and then run the Perl script in place of itself (via exec). Here is a "Hello World" example:

1  #! /bin/sh --
2  eval '(exit $?0)' && eval 'PERL_BADLANG=x;PATH="/usr/bin:/bin:/usr/local/bin:$PATH";export PERL_BADLANG;: \
3  ;exec perl -x -S -- "$0" ${1+"$@"};#'if 0;
4  exec 'setenv PERL_BADLANG x;exec perl -x -S -- "$0" $argv:q;#'.q
5  #!/bin/perl -w
6  +($0=~/(.*)/s);do(index($1,"/")<0?"./$1":$1);die$@if$@;__END__+if 0;
7  # Above is magic header ... real Perl code begins here
8  use strict;
9  use warnings;
10 print "hello world!\n";

Note: I added line numbers just to make it clear where lines start and end.

Comments

1

First check where perl is installed on your system, e.g. which perl and use that location in the shebang line instead of /usr/bin/perl, if it is different.

If all other recommendations fail, check the first line of the script on the machine where it is not running properly by doing this: head -1 test.pl | xxd. Does the output show the last two bytes as 0d 0a? If so, you probably copied over the file via Windows and didn't do a dos2unix conversion.

Comments

0

"command not found" is an error emitted by the shell. You are trying to run your Perl script by the shell, not by Perl.

2 Comments

Then how can I fix it? and also why it works on another machine?
@user1672190: Are you sure you pasted the actual scripts? Are you sure you correctly transferred the files to the remote machine? How is the environment different on the two machines?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.