Reputation: 9583
I'm running this shell script from within a php script at the command prompt.
<?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.
It looks like the problem is around 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.
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.
Upvotes: 0
Views: 115
Reputation: 59927
Try escaping your $script
with escapeshellcmd
, e.g. proc_open(escapeshellcmd($script), ...)
.
Also, I think $\file
should be \$file
.
And then: your string will be much more readable - and you will spot errors easier - with Heredoc-syntax:
$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;
Upvotes: 1