[php的exec在linux中返回值]php的exec在linux中返回值不能为负数的问题

时间:2017-09-08  来源:linux  阅读:

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

第三个参数, 怎么不能接收负数??
这里的&$return_var就是程序返回值,起初我的回答是可以为负数。
一般在C语言里我们会这样写

 代码如下

#include
#include
int main()
{
    printf("^_^n");
    return -5;
}

这个-5就是返回值,但习惯上是写成0或者1的。
注意:很多人的C代码里把main函数写成 void main() 这样实际上是不对的,详细的就不说了。
把上面的代码编译后,到CMD下运行,然后就能看到输出结果了。接着,输入“echo %ERRORLEVEL%”,回车,就可以看到程序的返回值了。这个%ERRORLEVEL%就代表了程序的返回状态。在WIN下确实是可以为负数的。如图所示:

,php调用也是正常的。

 代码如下 E:devphp535>php -r "exec("return.exe",$out,$a);var_dump($a);"
int(-2)

但是到了linux下,始终为正数,刚开始怀疑是权限问题,用了chmod +x后,排除了权限问题。

 代码如下 exec("/home/wwwroot/test/rtest.out 2>&1",$out,$a);
var_dump($out,$a);
array(1) { [0]=> string(3) "^_^" } int(251)

看起来成了256+return val,可以看到实际上返回了负数,只不过被转换成正数了。
接着看了下standard/exec.c里的源代码,没发现啥端倪,干到很奇怪,突然想到自己忘了一步。忘了看程序返回给OS的值了.
可以使用echo $? 显示最后命令的推出状况。

 代码如下 -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 www 4096 May  4  2011 2011
drwxr-xr-x  6 www www 4096 Jun 23  2011 eoc
-rwxr-xr-x  1 www www 7131 Feb  1 12:47 hello
-rw-r--r--  1 www www    3 Feb  1 12:51 hello.c
-rw-r--r--  1 www www   99 Feb  1 12:50 main.c
-rwxr-xr-x  1 www www 4714 Feb  1 12:51 mm
drwxr-xr-x  3 www www 4096 Jun 24  2011 test
-bash-3.00$ ./mm
^_^
-bash-3.00$ echo $?
251
-bash-3.00$

这样就可以看看exec返换给OS的值是多少。
linux下,这个返回值就是无符号类型,返回的是一个正数,所以传给php也是正数了,php实际上也是调用的exec所返回的值。

exec目录操作

 
2down 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:

 代码如下

$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 should close 
// our ends of any pipes to signal that we"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 

[php的exec在linux中返回值]php的exec在linux中返回值不能为负数的问题

http://m.bbyears.com/caozuoxitong/35423.html

推荐访问:
相关阅读 猜你喜欢
本类排行 本类最新