首頁 繪圖設計 工作閒聊 比賽活動 美術討論 標籤 圖片
PHP: system vs exec vs shell_exec vs passthru
Type(Type) 2021/5/14 20:16 (Since 2013/9/25 11:42)

PHP: system() vs exec() vs shell_exec() vs passthru()






An Example of simple C Code (a.out)


代碼:

<?PHP

$cmd = "a.out";

echo "------------------------------------------------\n";
$err = system($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

echo "------------------------------------------------\n";
$err = exec($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

echo "------------------------------------------------\n";
$err = shell_exec($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

echo "------------------------------------------------\n";
$err = passthru($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

?>


代碼:

$ cat stdout-stderr-fprintf.c
#include <stdio.h>
#include <stdlib.h>

int main(void)
{

        fprintf(stderr, "hello\n");
        fprintf(stderr, "thank\n");
        fprintf(stdout, "you\n");
        fprintf(stdout, "welcome\n");
        fprintf(stderr, "to\n");
        fprintf(stderr, "my home\n");

        return -15;
}

$ gcc stdout-stderr-fprintf.c
$ ls a.out
a.out


PHP: System(): console=STDOUT+STDERR, return last line of STDOUT


代碼:

------------------------------------------------
hello
thank
to
my home
you
welcome
------------------------------------------------
string(7) "welcome"
================================================


PHP: exec(): console=STDERR only, return last line of STDOUT


代碼:

------------------------------------------------
hello
thank
to
my home
------------------------------------------------
string(7) "welcome"
================================================


PHP: shell_exec(): console=STDERR only, return all lines of STDOUT


代碼:

------------------------------------------------
hello
thank
to
my home
------------------------------------------------
string(12) "you
welcome
"
================================================


PHP: passthru(): console=STDOUT+STDERR, return NULL


代碼:

------------------------------------------------
hello
thank
to
my home
you
welcome
------------------------------------------------
NULL
================================================


Example: "head -3 /proc/cpuinfo"



代碼:

<?PHP

$cmd = "head -3 /proc/cpuinfo";

echo "------------------------------------------------\n";
$err = system($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

echo "------------------------------------------------\n";
$err = exec($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

echo "------------------------------------------------\n";
$err = shell_exec($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

echo "------------------------------------------------\n";
$err = passthru($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

?>


@@@ RESULT @@@
------------------------------------------------
processor       : 0
vendor_id       : GenuineIntel
------------------------------------------------
string(24) "vendor_id   : GenuineIntel"
================================================


------------------------------------------------
------------------------------------------------
string(24) "vendor_id   : GenuineIntel"
================================================


------------------------------------------------
------------------------------------------------
string(39) "processor   : 0
vendor_id       : GenuineIntel
"
================================================


------------------------------------------------
processor       : 0
vendor_id       : GenuineIntel
------------------------------------------------
NULL
================================================



Type(Type) 2013/9/25 11:49


Example: "true"



代碼:

<?PHP

$cmd = "true";

echo "------------------------------------------------\n";
$err = system($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

echo "------------------------------------------------\n";
$err = exec($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

echo "------------------------------------------------\n";
$err = shell_exec($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

echo "------------------------------------------------\n";
$err = passthru($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

?>


@@@ RESULT @@@
------------------------------------------------
------------------------------------------------
string(0) ""
================================================


------------------------------------------------
------------------------------------------------
string(0) ""
================================================


------------------------------------------------
------------------------------------------------
NULL
================================================


------------------------------------------------
------------------------------------------------
NULL
================================================




Type(Type) 2013/9/25 11:53


Example: "top -b -c -n 1 | grep mysql"




代碼:

<?PHP

$cmd = "top -b -c -n 1 | grep mysql";

echo "------------------------------------------------\n";
$err = system($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

echo "------------------------------------------------\n";
$err = exec($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

echo "------------------------------------------------\n";
$err = shell_exec($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

echo "------------------------------------------------\n";
$err = passthru($cmd);
echo "------------------------------------------------\n";
var_dump($err);
echo "================================================\n\n\n";

?>


@@@ RESULT @@@
------------------------------------------------
------------------------------------------------
string(0) ""
================================================


------------------------------------------------
------------------------------------------------
string(0) ""
================================================


------------------------------------------------
------------------------------------------------
NULL
================================================


------------------------------------------------
------------------------------------------------
NULL
================================================




(2,745 views)
[更多討論] 討論區 Windows, Linux, Perl, PHP, C/C++, Driver, Web 理論、應用、硬體、軟體

"PHP: system vs exec vs shell_exec vs passthru" 傳統頁面(電腦版)

首頁 繪圖設計 工作閒聊 比賽活動 美術討論 標籤 圖片
傳統桌面版 [ 登入/註冊 ]
© Vovo2000.com Mobile Version 小哈手機版 2024