gpt4 book ai didi

php - 错误替换 Bash : how to write a heredoc to a dynamically created file (variable)?

转载 作者:行者123 更新时间:2023-11-29 09:46:53 25 4
gpt4 key购买 nike

我即将在 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命令产生错误消息“Bad substitution”。

我试图通过引用 <<-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

最佳答案

正如 Benjamin W 的评论中所指出的,问题出在本应写入文件的 php 代码中。

在我写的代码中$nextpage=${filecount)+1; 标记括号!

应该是:$nextpage=${filecount}+1;

分析“Bad substitution”错误的策略是从一个空的 bash 脚本开始。之后,我编写了 heredoc 框架逐行测试它。当没有错误返回时,我粘贴了以下行,依此类推,直到到达 '$filecount}+1' 表达式。

问题已解决,感谢您的反馈!

关于php - 错误替换 Bash : how to write a heredoc to a dynamically created file (variable)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34978235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com