【发布时间】:2016-08-24 22:24:52
【问题描述】:
我在 php 脚本中有类似的东西:
<?php
...
function log() {
// saving the log into a file.
exec(<<<BASH
cat >> $logFile <<EOF
$log
EOF
BASH
);
}
...
您可以看到两个 heredocs(BASH 是 php,EOF 是 shell)以人们认为是正确的方式结束,但是当我阅读创建的日志时,日志有这样的内容:
...
my logged string of an important event
EOF
my logged string of another important event
EOF
...
我检查了 apache 日志,它有以下条目:
sh: line 1: warning: here-document at line 0 delimited by end-of-file (wanted `EOF')
我做错了什么?
请,我知道还有许多其他实现,例如使用 php 函数或使用引号而不是 heredocs。但我很好奇为什么在这种特殊情况下这不起作用。
编辑。 我澄清了代码,所以更清楚我说的是 php 运行 shell 命令。
【问题讨论】:
-
<<<BASH是这里的字符串,而不是here-doc。Here-string不需要分隔符来标记其结尾。