如何在system,exec或shell_exec中运行多个命令? [英] How to run multiple commands in system, exec or shell_exec?
本文介绍了如何在system,exec或shell_exec中运行多个命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从php运行这样的shell命令:
I'm trying to run shell command like this from php:
ls -a | grep mydir
但是php仅使用第一个命令。有什么方法可以迫使php将整个字符串传递给shell?
But php only uses the first command. Is there any way to force php to pass the whole string to the shell?
(我不在乎输出)
推荐答案
http://www.php.net/manual/zh-CN/function.proc-open.php
首次打开 ls- a
读取输出,将其存储在var中,然后打开 grep mydir
写入您从 ls存储的输出-a
然后再次读取新的输出。
First open ls -a
read the output, store it in a var, then open grep mydir
write the output that you have stored from ls -a
then read again the new output.
LE:
<?php
//ls -a | grep mydir
$proc_ls = proc_open("ls -a",
array(
array("pipe","r"), //stdin
array("pipe","w"), //stdout
array("pipe","w") //stderr
),
$pipes);
$output_ls = stream_get_contents($pipes[1]);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
$return_value_ls = proc_close($proc_ls);
$proc_grep = proc_open("grep mydir",
array(
array("pipe","r"), //stdin
array("pipe","w"), //stdout
array("pipe","w") //stderr
),
$pipes);
fwrite($pipes[0], $output_ls);
fclose($pipes[0]);
$output_grep = stream_get_contents($pipes[1]);
fclose($pipes[1]);
fclose($pipes[2]);
$return_value_grep = proc_close($proc_grep);
print $output_grep;
?>
这篇关于如何在system,exec或shell_exec中运行多个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文