首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >坏的替代Bash:如何将一个本地文档写入一个动态创建的文件(变量)?

问坏的替代Bash:如何将一个本地文档写入一个动态创建的文件(变量)?
EN

Stack Overflow用户
提问于 2016-01-24 16:24:06
回答 4查看 2K关注 0票数 3

我将在php中生成大量的调查。

我使用Bash来使用cat命令添加某种自动化。因此,我使用Bash生成.php文件。

要添加新的调查,我的Bash脚本使用filecount='ls -l ${survey_dir}${survey_category} | wc -l'对调查目录中的文件进行计数。我想使用filecount来生成下一个调查的文件名。因此,如果survey_category目录有两个文件,那么新的文件名将是3.php。

在启动heredoc之前,我会发出以下变量和命令:

代码语言:javascript
复制
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

代码语言:javascript
复制
: bad substitution
+ [[ 1: == \2\: ]]
+ [[ 1: == \3\: ]]
+ [[ 1: == \4\: ]]

我的问题是:如何将一个本地文档写入一个动态创建的文件?

引起问题的代码:

代码语言:javascript
复制
    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
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-01-24 22:37:11

正如Benjamin W的评论中指出的那样,这个问题是在应该写入文件的php代码中引起的。

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

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

分析“坏替代”错误的策略是从空bash脚本开始。在那之后,我写了一行一行地测试它的本地文档框架。当没有返回错误时,我粘贴了下面的一行,以此类推,直到到达'$filecount}+1‘表达式。

问题解决了,谢谢你的反馈!

票数 2
EN

Stack Overflow用户

发布于 2016-01-24 16:29:01

我相信本地文档的语法是<<,而不是<-

票数 1
EN

Stack Overflow用户

发布于 2016-01-24 18:52:49

@kzpm:基于您为我提供的代码:

代码语言:javascript
复制
!/bin/bash

wd="/tmp/workdir/"
catg="survey01"
mkdir -p ${wd}${catg}
filecount=`ls ${wd}${catg} | wc -l`
filecount=$((filecount+1))
newfile="${wd}${catg}/${filecount}.php"
echo "${newfile}"
cat > ${newfile} <<-EOF
<?php ..... ?>
EOF

我所做的改变:

  1. ls的Backticks (代码中的单引号,可能就是这种情况)
  2. 在开始时移动设置catg值(在filecount之后声明它)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34978235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档