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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marty Meyers

    #1

    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


  • Etienne Marais

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

    Marty Meyers wrote:
    [color=blue]
    > 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?[/color]

    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_erro rs.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_erro rs.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.
    [color=blue]
    >
    > 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[/color]

    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_contai ng_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

    Comment

    • Marty Meyers

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


      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.
      [color=blue][color=green]
      > > $msg= exec("perl $scriptPath/insert.pl $d $u $t 2>&1", $returnVal);[/color][/color]
      [color=blue][color=green]
      > > Second problem, this same line of code when run from the unix command[/color][/color]
      line[color=blue][color=green]
      > > returns the following error:
      > >
      > > sh: /perl: No such file or directory[/color]
      >
      > Just checking, are you saying that the
      > command works fine with:
      > $> perl insert.pl[/color]
      Marty ans: Yes, it works-with args, ie perl insert.pl 28 2
      /var/www/....../vehicles.txt[color=blue]
      >
      > but that it does not work from neither
      > php php_file_contai ng_exec.php[/color]
      Marty ans: correct, it fails BTW-the php file is ftpupload.php[color=blue]
      >
      > nor
      >
      > $> perl $scriptPath/insert.pl $d $u $t 2>&1[/color]
      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.[color=blue]
      >
      > Did you substitute $scriptPath; $d and $u with
      > the relevant values before trying this, or
      > exactly what did you do ?[/color]
      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[color=blue]
      >
      > --
      > Etienne Marais
      > Cosmic Link
      > South Africa[/color]



      Comment

      Working...