[英]running shell script from php not working properly
I'm running this shell script from within a php script at the command prompt. 我正在命令提示符下从php脚本中运行此Shell脚本。
<?php
$monitorDir = 'logs';
$script = "" .
" inotifywait -mqr --format '%w %f %e' $monitorDir | " .
" while read dir file event;" .
" do" .
" if [ \"\$event\" == \"CLOSE_WRITE,CLOSE\" ];" .
" then" .
" echo finished writing \$file; ".
" fi;" .
" done";
$proc = proc_open($script, $descriptors, $pipes);
When I run it I end up with output that looks like this: 当我运行它时,最终输出如下所示:
sh: 1: [: MODIFY: unexpected operator
sh: 1: [: CLOSE_WRITE,CLOSE: unexpected operator
sh: 1: [: MODIFY: unexpected operator
sh: 1: [: OPEN: unexpected operator
sh: 1: [: MODIFY: unexpected operator
The strange thing is, when I echo out $script
in the php and paste the resulting output in to the command shell it runs fine. 奇怪的是,当我在php中回显
$script
并将结果输出粘贴到命令外壳中时,它运行良好。
It looks like the problem is around if [ \\"\\$event\\" ==
. if [ \\"\\$event\\" ==
,问题似乎就解决了。
Anyone see what I'm missing here? 有人看到我在这里想念的吗?
Edit 编辑
Below is the exact output rendered by the php, Appologies for the formatting but I thought I'd leave it 'as is' to demonstrate what is being produced. 以下是由php,Appologies进行格式设置的确切输出,但我认为我会“按原样”进行演示以演示正在生成的内容。
inotifywait -mqr --format '%w %f %e' logs | while read dir file event; do if [ "$event" == "CLOSE_WRITE,CLOSE" ]; then echo finished writing $file; fi; done
As I say, when pasted in to the console it runs fine, it just fails when opened with proc open. 就像我说的那样,当粘贴到控制台中时,它运行良好,但在使用proc open打开时会失败。
Try escaping your $script
with escapeshellcmd
, eg proc_open(escapeshellcmd($script), ...)
. 尝试使用
escapeshellcmd
转义$script
,例如proc_open(escapeshellcmd($script), ...)
。
Also, I think $\\file
should be \\$file
. 另外,我认为
$\\file
应该是\\$file
。
And then: your string will be much more readable - and you will spot errors easier - with Heredoc-syntax : 然后:使用Heredoc语法,您的字符串将更具可读性-并且您将更容易发现错误:
$script = <<<EOD
inotifywait -mqr --format '%w %f %e' $monitorDir |
while read dir file event;
do
if [ "\$event" == "CLOSE_WRITE,CLOSE" ];
then
echo finished writing \$file
fi;
done
EOD;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.