php - Bad substitution Bash: how to write a heredoc to a dynamically created file (variable)? -


i generate lot of surveys in php.

i'm using bash add kind of automation cat command. use bash generate .php files.

to add new survey, bash script counts files in survey directory filecount='ls -l ${survey_dir}${survey_category} | wc -l'. want use filecount generate filename of next survey. if survey_category directory has 2 files, new filename 3.php.

before starting heredoc, issue following variables , commands:

filecount=$((filecount+1)) newfile="${wd}${catg}/${filecount}.php" cat > ${newfile} <<-eof ... eof 

when execute script cat command yields error message 'bad substitution'.

i tried overcome quoting <<-eof: <<-'eof', use lot of variable substitutions in php, solution can't implemented.

i checked if ls -l /bin/bash links bash , not dash or sh shell.

i checked if 'shebang' #!/bin/bash , not #!/bin/sh. checked ok.

furthermore tried use single , double quotes on variable ${newfile}: no avail.

debug info #!/bin/bash -x:

: bad substitution + [[ 1: == \2\: ]] + [[ 1: == \3\: ]] + [[ 1: == \4\: ]] 

my question is: how write heredoc dynamically created file?

code causing problem:

    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 

i believe syntax heredoc << not <<-


Comments