The return value of php exec in linux cannot be negative.

Source: Internet
Author: User

Let's take a look at how to use the exec function.

Exec ()
Prototype:

The code is as follows: Copy code
String exec (string command [, string array [, int return_var])

The exec () function is similar to system (). It also executes the given command, but does not output the result, but returns the last line of the result. Although it only returns the last line of the command result, the complete result can be obtained using the second parameter array by appending the result row by row to the end of the array. Therefore, if the array is not empty, we recommend that you use unset () to clear it before calling it. Only when the second parameter is specified can the third parameter be used to obtain the command execution status code.
Example:

The code is as follows: Copy code
Exec ("/bin/ls-l ");
Exec ("/bin/ls-l", $ res );
Exec ("/bin/ls-l", $ res, $ rc );

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. In WIN, it can be a negative number.

, 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.

OUT, maybe this is what linux requires. In the program, the Thai return value is 1 ~ 255, but you do not know this rule. However, it is not determined whether this is the rule in linux. Although the problem is very painful and of little significance, it can be left here for reference by people who encounter similar problems. However, there is no need to go further here. If you really want to figure it out, you can look at shell.

 

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.