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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7122742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:03:55  来源:igfitidea点击:

execute two shell commands in single exec php statement

phpexec

提问by Warrior

I want to display all the files that are modified after a specified date

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

the commands are

命令是

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

How i can execute this two commands in single exec statement.Thanks in advance

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

回答by Marc B

You can use either a ;or a &&to separate the comands. The ;runs both commands unconditionally. If the first one fails, the second one still runs. Using &&makes the second command depend on the first. If the first command fails, the second will NOT run.

您可以使用 a;或 a&&来分隔命令。该;无条件地同时运行命令。如果第一个失败,第二个仍然运行。使用&&使第二个命令依赖于第一个。如果第一个命令失败,第二个将不会运行。

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

回答by PoorBoy

This is how I did it simultaneously encode thumbs, and then flv video..I need to generate 2 thumbs from avi file. After the thumbs I need to convert the same avi to flv or whatever. So, here is the code I normally used.

这就是我如何同时编码拇指,然后是 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 &");

You can also run array of commands with exec, if you want :)

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

foreach($crapatoids as $crap){

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

回答by Rijk

Seperate them with a semicolon (;). Example:

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

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

回答by JJ.

The semicolon separator allows you to run multiple commands on one line.

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

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

回答by Nguyen Tan Dat

Actually, my problem came from execution python file in a virtual environment of Python. Generally, Python website instructs us to go through command lines: create a virtual env --> activate it --> call Python file (e.g: python3 yourPyFile.py). However, when I was trying to adapt these steps through calling in php with exec()method, it didn't work. Finally, I found out you don't need to activate env at all, what you only need to use a python which already generated when you creating virtual env by path/to/virtual/env/bin/python3 yourPyFile.py.

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