php的exec在linux中傳回值不能為負數的問題_PHP教程

來源:互聯網
上載者:User
exec函數在windows環境下是沒有任何問題的,但在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
}

http://www.bkjia.com/PHPjc/632187.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632187.htmlTechArticleexec函數在windows環境下是沒有任何問題的,但在linux中傳回值不能為負數。 string exec ( string $command [, array $output [, int $return_var ]] ) 第三個參數...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.