Php exec cannot return negative values in linux

Source: Internet
Author: User

String exec (string $ command [, array & $ output [, int & $ return_var])

In the third parameter, how can I not receive negative numbers ??
Here & $ return_var is the return value of the program. At first, my answer is a negative number.
We usually write this in c.

The code is as follows: Copy code

# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Printf ("^_^ n ");
Return-5;
}

This-5 is the return value, but it is written as 0 or 1.
Note: Many people write the main function as void main () in C code. This is actually not correct, so I won't go into details.
Compile the above code and run it in CMD. Then the output result is displayed. Enter "echo % ERRORLEVEL %" and press enter to view the program return value. This % ERRORLEVEL % indicates the return status of the program. It can be a negative number in WIN. As shown in the following figure:

, Php call is also normal.

The code is as follows: Copy code
E: devphp535> php-r "exec('return.exe ', $ out, $ a); var_dump ($ );"
Int (-2)

However, in linux, it is always a positive number. It was initially suspected that it was a Permission problem. After chmod + x was used, the permission problem was ruled out.

The code is as follows: Copy code
Exec ("/home/wwwroot/test/rtest. out 2> & 1", $ out, $ );
Var_dump ($ out, $ );
Array (1) {[0] => string (3) "^_^"} int (251)

It seems to be 256 + return val. We can see that a negative number is actually returned, but it is converted to a positive number.
Then I read the source code in standard/exec. c, but I didn't find any clues. It was very strange. I suddenly thought I forgot a step. I forgot to read the OS value returned by the program.
Echo $? Displays the launch status of the last command.

The code is as follows: Copy code
-Bash-3.00 $ vi main. c
-Bash-3.00 $ gcc-o./mm main. c
-Bash-3.00 $ ll
Total 48
Drwxr-xr-x 3 www 4096 May 4 2011 2011
Drwxr-xr-x 6 www 4096 Jun 23 2011 eoc
-Rwxr-xr-x 1 www 7131 Feb 1 hello
-Rw-r -- 1 www 3 Feb 1 :51 hello. c
-Rw-r -- 1 www 99 Feb 1 :50 main. c
-Rwxr-xr-x 1 www 4714 Feb 1mm
Drwxr-xr-x 3 www 4096 Jun 24 2011 test
-Bash-3.00 $./mm
Pai_^
-Bash-3.00 $ echo $?
251
-Bash-3.00 $

In this way, you can see the value of exec returned to OS.
In linux, the returned value is of the unsigned type and returns a positive value. Therefore, it is also a positive value to pass to php. php is actually the value returned by the called exec.

Exec directory operations

 
2 down vote For greater control over how the child process will be executed, you can use the proc_open () function:

2 down vote

For greater control over how the child process will be executed, you can use the proc_open () function:

The code is as follows: Copy code

$ Cmd = 'Scripts/script. Sh ';
$ Cwd = 'scripts ';
 
$ Spec = array (
// Can something more portable be passed here instead of/dev/null?
0 => array ('file', '/dev/Null', 'r '),
1 => array ('file', '/dev/Null', 'w '),
2 => array ('file', '/dev/Null', 'w '),
);
 
$ Ph = proc_open ($ cmd, $ spec, $ pipes, $ cwd );
If ($ ph = FALSE ){
// Open error
} 
 
// If we are not passing/dev/null like above, we shoshould close
// Our ends of any pipes to signal that we're re done. Otherwise
// The call to proc_close below may block indefinitely.
Foreach ($ pipes as $ pipe ){
@ Fclose ($ pipe );
} 
 
// Will wait for the process to terminate
$ Exit_code = proc_close ($ ph );
If ($ exit_code! = 0 ){
// Child error
} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.