469,613 Members | 1,645 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

2>&1 arg in exec(perl.... + "sh: /perl not found error" TIA for help

I have the following line in a php file:

$msg= exec("perl $scriptPath/insert.pl $d $u $t 2>&1", $returnVal);

Can someone explain the "2>&1" argument?
Second problem, this same line of code when run from the unix command line
returns the following error:

sh: /perl: No such file or directory

I've verified that perl is located in /usr/bin/
/usr/bin is in the environment variable "PATH"
all the directories from php program to the root and back down to usr/bin
have 755 permissions.
if I run the program (insert.pl) from the command line it works
I've tried putting the following in both the php and perl programs:
#!/usr/bin/perl
#!/usr/bin

Any suggestions would be appreciated.

Thanks
Marty
Dec 7 '05 #1
2 10124
Marty Meyers wrote:
I have the following line in a php file:

$msg= exec("perl $scriptPath/insert.pl $d $u $t 2>&1", $returnVal);

Can someone explain the "2>&1" argument?
Error redirection.

&1 is stdout (standard output stream)
&2 is stderr (standard error stream)

if you do

command | more

and you get valid output, with lines of errors in between the output,
the errors are not piped to more and will mess up the display
unless you do:

command 2>&1 | more

which will work fine.

Or if you do

command > output.txt
output.txt does not contain any errors created(output) by command,
only the normal output of the command.

and

command 2> errors.txt
will contain only errors, none of the normal output.

You can do

command > output.txt 2> errors.txt
to get seperate output for normal and error reports
in two different files

command 2>&1 > output_and_errors.txt
to get all output into one report.
(Which places the errors in the same stream as the normal output,
and then redirects the normal output to output_and_errors.txt)

The reason your exec has 2>&1 is so
that the return value will contain any
possible errors and/or normal output.

If this was not added and if the
perl command should fail, you would
not even have that information in
returnVal.

Second problem, this same line of code when run from the unix command line
returns the following error:

sh: /perl: No such file or directory


Just checking, are you saying that the
command works fine with:
$> perl insert.pl

but that it does not work from neither
php php_file_containg_exec.php

nor

$> perl $scriptPath/insert.pl $d $u $t 2>&1

Did you substitute $scriptPath; $d and $u with
the relevant values before trying this, or
exactly what did you do ?

--
Etienne Marais
Cosmic Link
South Africa

Dec 7 '05 #2

Hi Etienne,
Thanks for the detailed explanation of "2>&1". I should have recognized
that from the windows cmd processor which is about the same.
$msg= exec("perl $scriptPath/insert.pl $d $u $t 2>&1", $returnVal); Second problem, this same line of code when run from the unix command line returns the following error:

sh: /perl: No such file or directory
Just checking, are you saying that the
command works fine with:
$> perl insert.pl

Marty ans: Yes, it works-with args, ie perl insert.pl 28 2
/var/www/....../vehicles.txt
but that it does not work from neither
php php_file_containg_exec.php Marty ans: correct, it fails BTW-the php file is ftpupload.php
nor

$> perl $scriptPath/insert.pl $d $u $t 2>&1 Marty ans: Very interesting, using the $scriptPath in the command line
fails. It starts the script(I have a print statement in it) but it does not
run the entire program.
Did you substitute $scriptPath; $d and $u with
the relevant values before trying this, or
exactly what did you do ? Marty ans: ran it two ways (both from command line, ie php ftpupload.php):
I used variables-failed
I used the real values for all variables and changed the double quotes
to single quotes in the ftpupload.php file-Still failed

Marty ans: Thanks
--
Etienne Marais
Cosmic Link
South Africa


Dec 7 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bob | last post: by
4 posts views Thread by barney | last post: by
5 posts views Thread by martin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.