在单个exec php语句中执行两个shell命令

时间:2011-08-19 14:03:55

标签: php exec

我想显示在指定日期之后修改的所有文件

命令是

touch --date '2011-09-19 /home/  , find /home/

我如何在单个exec语句中执行这两个命令。提前谢谢

5 个答案:

答案 0 :(得分:40)

您可以使用;&&来分隔命令。 ;无条件地运行这两个命令。如果第一个失败,第二个仍然运行。使用&&使第二个命令依赖于第一个命令。如果第一个命令失败,则第二个命令不会运行。

command1 ; command2     (run both uncondtionally)
command1 && command2     (run command2 only if command1 succeeds)

答案 1 :(得分:4)

这是我如何同时编码拇指,然后flv视频..我需要从avi文件生成2个拇指。在拇指之后,我需要将相同的avi转换为flv或其他任何东西。所以,这是我通常使用的代码。

$command_one = "do whatever the script does best";
$command_two = "do whatever the script does second best";
//execute and redirect standard stream ..
@exec($command_one."&& ".$command_two.">/dev/null 1>/dev/null 2>/dev/null &");

如果需要,还可以使用exec运行命令数组:)

foreach($crapatoids as $crap){

$command_name = $crap;
//exec the crap below
@exec ($command_name." >/dev/null 1>/dev/null 2>/dev/null &");
}

答案 2 :(得分:2)

用分号(;)分隔它们。例如:

exec("touch --date '2011-09-19' /home/; find /home/");

答案 3 :(得分:2)

分号分隔符允许您在一行上运行多个命令。

<?php
    $output = shell_exec("touch --date '2011-09-19' /home/; find /home/");
    echo "<pre>" . $output . "</pre>";
?>

答案 4 :(得分:1)

实际上,我的问题来自在Python虚拟环境中执行python文件。通常,Python网站会指示我们执行命令行:创建一个虚拟环境->激活它->调用Python文件(例如:python3 yourPyFile.py)。但是,当我尝试通过使用exec()方法调用php来调整这些步骤时,它没有用。最后,我发现您根本不需要激活环境,只需要使用path/to/virtual/env/bin/python3 yourPyFile.py创建虚拟环境时已经生成的python。

相关问题