简体   繁体   English

从PHP shell_exec启动时,Bash脚本具有不同的行为

[英]Bash script has different behavior when launched from PHP shell_exec

script.sh script.sh

cd ~/scripts
acname=$(cat acname.txt)
days=$(cat days.txt)
nbmessages=`ls -l message_*.txt | wc -l`
today=`date +%Y%m%d`
#
echo $acname >> ./log.txt
echo $days >> ./log.txt
echo $nbmessages >> ./log.txt
echo $today >> ./log.txt

if I run script.sh in a shell prompt, log.txt shows: 如果我在shell提示符下运行script.sh,则log.txt显示:

alex8
4
5
20140124

which is correct 哪个是对的

If I run script.sh using shell_exec function in PHP, log.txt shows: 如果我在PHP中使用shell_exec函数运行script.sh,则log.txt显示:

log.txt: 


0
20140124

Therefore getting the content of a file doesn't work, nor counting the files with a specific pattern (got 0 when it should be 5), while the date command still works. 因此,在date命令仍然有效的情况下,获取文件内容无效,也无法对具有特定模式的文件计数(应该为5时为0)。 Any idea ? 任何想法 ? Many thanks in advance ! 提前谢谢了 !

~/scripts goes to the scripts subdir of YOUR home dir. ~/scripts转到您的主目录的scripts子目录。 This may be a different place than the home dir of the web server process running php. 这可能与运行php的Web服务器进程的主目录不同。

In general using full paths can help alleviate issues like this, but you will still have to contend with ensuring the web server process has the rights to the files in question. 通常,使用完整路径可以帮助减轻此类问题,但是您仍将不得不确保Web服务器进程具有对所涉及文件的权限。

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

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