简体   繁体   English

为什么此Shell脚本无法按预期从PHP运行?

[英]Why is this shell script not running as expected from PHP?

I have this PHP program in /var/www folder to run a shell script and load the HTML file created by that shell script. 我在/ var / www文件夹中有此PHP程序,可以运行外壳程序脚本并加载该外壳程序脚本创建的HTML文件。 If I run the shell script manually from command line then it works like charm but when run via PHP program from browser then it only creates an empty file. 如果我从命令行手动运行shell脚本,那么它的工作原理就像魅力,但是当通过浏览器通过PHP程序运行时,它只会创建一个空文件。

PHP CODE PHP代码

$a = './script.sh '.$foo.' '.$bar;
$b = shell_exec($a);
include '/var/tmp/reports/r.html';

SHELL SCRIPT 外壳脚本

cat file.ext | awk <something with $foo and $bar> | command > /var/tmp/reports/r.html

(Edit ^ "file.ext" is actually a .log file. It iss located in /var/log/.. And "command" is an other program that creates an .html file from that log file) (编辑^“ file.ext”实际上是一个.log文件。它位于/ var / log /中。“ command”是另一个从该日志文件创建.html文件的程序)

Permissions of my files are : 我的文件权限是:

-rw-r--r-- 1 abc    www-data   848 Feb 13 10:43 php.php
-rwxr-xr-x 1 ubuntu www-data   230 Feb 13 10:51 script.sh*

And /var/tmp/reports/r.html doesn't exist before execution of PHP. 并且/var/tmp/reports/r.html在执行PHP之前不存在。 After execution of script directly via command line it creates r.html file like : 直接通过命令行执行脚本后,它会创建r.html文件,例如:

-rw-rw-r-- 1 ubuntu ubuntu 121884 Feb 13 11:42 r.html

But when script is executed via PHP from browser it creates an empty file like this 但是,当脚本从浏览器通过PHP执行时,它会创建一个空文件,如下所示

-rw-r--r-- 1 www-data www-data      0 Feb 13 11:43 r.html

Edit1 : On @lurker's suggestio I tried changing script.sh to Edit1:在@lurker的建议上,我尝试将script.sh更改为

#!/bin/sh

cat file.log | awk '{if(substr($5,2)>="'$1'" && substr($5,2)<="'$2'")print $0}' | awk 'gsub(",", "", $1);' | /usr/bin/command > /var/tmp/reports/r.html

It also generated an empty file only. 它还仅生成一个空文件。

Edit2 : I changed the script to -> Edit2:我将脚本更改为->

#!/bin/bash
sudo echo "sdfsf" > /var/tmp/reports/r.html

Even it wont work. 即使它不会工作。

For the command cat file.ext | awk <something with $foo and $bar> | command > /var/tmp/reports/r.html 对于命令cat file.ext | awk <something with $foo and $bar> | command > /var/tmp/reports/r.html cat file.ext | awk <something with $foo and $bar> | command > /var/tmp/reports/r.html cat file.ext | awk <something with $foo and $bar> | command > /var/tmp/reports/r.html , when I executed it from script.sh directly then it approached the file.ext as ubuntu user. cat file.ext | awk <something with $foo and $bar> | command > /var/tmp/reports/r.html ,当我直接从script.sh执行它时,它以ubuntu用户身份进入file.ext ubuntu user was authorized to access file.ext , since it belonged to the group that could read it. ubuntu用户被授权访问file.ext ,因为它属于可以读取它的组。 But when I executed the command form php.php script via browser then it accessed file.ext as abc user ( abc was actually www-data user of apache2). 但是当我通过浏览器执行命令形式php.php脚本时,它以abc用户身份访问了file.extabc实际上是apache2的www-data用户)。 Neither the abc user (I mean www-data user) belong to the group which could access the file nor did the file allowed other users to read it. abc用户(我的意思是www-data用户)都不属于可以访问该文件的组,该文件也不允许other users读取该文件。 Hence the php script failed to produce the proper output. 因此,PHP脚本无法产生正确的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM