【发布时间】:2016-04-30 21:56:46
【问题描述】:
我即将在 php 中生成大量调查。
我正在使用 Bash 通过 cat 命令添加某种自动化。所以我使用 Bash 来生成.php 文件。
要添加新调查,我的 Bash 脚本会使用 filecount='ls -l ${survey_dir}${survey_category} | wc -l' 计算调查目录中的文件。我想使用filecount 来生成下一个调查的文件名。所以如果survey_category目录有两个文件,那么新的文件名就是3.php。
在启动 heredoc 之前,我发出以下变量和命令:
filecount=$((filecount+1))
newfile="${wd}${catg}/${filecount}.php"
cat > ${newfile} <<-EOF
...
EOF
当我执行脚本时,cat 命令会产生错误消息“替换错误”。
我试图通过引用<<-EOF 来克服这个问题:比如<<-'EOF',但是我在php 中使用了很多变量替换,所以这个解决方案无法实现。
我检查了ls -l /bin/bash 是否实际上链接到bash 而不是dash 或另一个sh shell。
我检查了我的“shebang”是否为#!/bin/bash 而不是#!/bin/sh。检查没问题。
此外,我尝试在变量 ${newfile}: 上使用单引号和双引号:无济于事。
调试信息#!/bin/bash -x:
: bad substitution
+ [[ 1: == \2\: ]]
+ [[ 1: == \3\: ]]
+ [[ 1: == \4\: ]]
我的问题是:如何将 heredoc 写入动态创建的文件?
导致问题的代码:
echo -e "Your question please:"
read -e rvraag
rvraag="${rvraag//\"/\^}"
salt=`date +"%s"`
filecount=`ls -l ${wd}${catg} | wc -l`
filecount=$((filecount+1))
newfile="${wd}${catg}/${filecount}.php"
cat > ${newfile} <<-EOF
<?php
session_start();
\$st_code =\$_SESSION['studentnr'];
\$cursuscode ="${catg}";
\$rdir="/var/www/qqlq.org/www/gen_enq/";
require_once \$rdir.'/inc/error_reporting.inc';
require_once \$rdir.'/src/clsQuestionProgress.class.php';
\$myQuestionProgress = new clsQuestionProgress();
\$myQuestionProgress->fCreateQuestionProgress( \$st_code, \$cursuscode );
require_once \$rdir.'/inc/header.inc';
require_once \$rdir.'/src/clsWriteProgress.class.php';
echo "<div class='container'><hr>
Vraag ${catg} ${filecount}<h4> ${rvraag}</h4><br><br>
<form name='qradio' action =".\$_SERVER['PHP_SELF']." method= 'POST'>
<div class='radio'><label><input type='radio' name='${catg}${salt}'
value='1'>${answ1}<label></div>
<div class='radio'><label><input type='radio' name='${catg}${salt}' value='2'>${answ2}<label></div>
<div class='radio'><label><input type='radio' name='${catg}${salt}' value='3'>${answ3}<label></div>
<div class='radio'><label><input type='radio' name='${catg}${salt}' value='4'>${answ4}<label></div>
<input type='submit' class='btn btn-warning' name='btnCancel${salt}' value='Go Back'>
<input type='submit' class='btn btn-success' name='btn${catg}${salt}' value='Ok'>
</form>
<hr></div>";
if ( \$_POST['${catg}${salt}'] == "${answok}" && isset( $_POST['btn${catg}${salt}'] ) ){
\$myProgress = new clsWriteProgress();
\$tablename = 'q'.\$st_code.\$cursuscode;
\$myProgress->fSetProgress( \$tablename, \$_SESSION["leerjaar"],$st_code,\$cursuscode,"${rvraag}",1 );
\$nextpage=${filecount)+1;
echo '<script>location.replace("'.\${nextpage}.php.'");</script>';
}//end if
if ( \$_POST['${catg}${salt}'] !== "${answok}" && isset( \$_POST['btn${catg}${salt}'] ) ){
\$tablename = 'q'.\$st_code.\$cursuscode;
\$myProgress = new clsWriteProgress();
\$myProgress->fSetProgress( \$tablename, \$_SESSION["leerjaar"],\$st_code,\$cursuscode,"${rvraag}",0 );
\$nextpage=${filecount)+1;
echo '<script>location.replace("'.${nextpage}.php.'");</script>';
}//end if
if ( isset( \$_POST['btnCancel${salt}'] ) ){
echo '<script>location.replace("../../studyoverview.php" );</script>';
}//end if
EOF
【问题讨论】:
-
这是可疑的 -
cat > ${newfile} <<-EOF- 删除破折号 -
@Ed Heal:当我删除破折号时,会出现一条错误消息:
warning: here-document at line 57 delimited by end-of-file (wantedEOF')`。我需要破折号来跳过脚本中的标签.. -
你能发布整个代码吗?删除破折号不应导致此类错误。
-
代码:
wd="/var/www/example.com/www/survey/" filecount='ls -l ${wd}${catg} | wc -l' catg="survey01"; filecount=$((filecount+1)) newfile="${wd}${catg}/${filecount}.php" echo "${newfile}" cat > ${newfile} <<-EOF <?php ..... ?>EOF -
@nsilent22 如果以
EOF结尾的行以制表符开头。