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)
© Vovo2000.com Mobile Version 小哈手機版 2024