0

I'm writing a shell script that automatically writes a bash script using cat. However, when I run the script, the produced bash script does not include data found in quotation marks. Here's my script:

cat <<EOS > script.bash

scriptDir=`dirname "$0"`
scriptName=`basename "$0"`
singleLine=0
function process ()
{
for file
do
if [ "${file}" = "-singleLine" ]
then
singleLine=1
else
if [ "${file}" = "-" ]
then
processStdIn
else
processFile "${file}"
fi
fi
done
}
function processFile ()
{
file="$1"
if [ -f "${file}" ]
then
printf "<table border=\"1\">"
if [ ${singleLine} -eq 0 ]
then
echo
fi
cat "${file}" | sed 's|,|</td><td>|g'  |
while read line
do
printf "<tr><td>${line}</td></tr>"
if [ ${singleLine} -eq 0 ]
then
echo
fi
done
printf "</table>"
echo
else
echo "${file} does not exist!" >&2
fi
}
function processStdIn ()
{
tempFile=`GetTempPathName.ksh "${scriptName}"`
while read line
do
echo "${line}" >> "${tempFile}"
done
processFile "${tempFile}"
rm "${tempFile}"
}
function usage ()
{
echo "  Usage:   ${scriptName} [ -singleLine ] \"file 1\" [ . . . \"file N\" ]"
echo
echo "             You may substitute '-' in place of a file to read from STDIN."
echo
echo "             Use the -singleLine flag if you want to generate the HTML on"
echo "             a single line. This flag may show up anywhere on the command-"
echo "             line. But only data specified after this flag will be"
echo "             generated in this manner."
}
if [ $# -gt 0 ]
then
process "$@"
else
usage
fi
EOS

Here's script.bash:

scriptDir=.
scriptName=test.sh
singleLine=0
function process ()
{
for file
do
if [ "" = "-singleLine" ]
then
singleLine=1
else
if [ "" = "-" ]
then
processStdIn
else
processFile ""
fi
fi
done
}
function processFile ()
{
file=""
if [ -f "" ]
then
printf "<table border=\"1\">"
if [  -eq 0 ]
then
echo
fi
cat "" | sed 's|,|</td><td>|g'  |
while read line
do
printf "<tr><td></td></tr>"
if [  -eq 0 ]
then
echo
fi
done
printf "</table>"
echo
else
echo " does not exist!" >&2
fi
}
function processStdIn ()
{
tempFile=
while read line
do
echo "" >> ""
done
processFile ""
rm ""
}
function usage ()
{
echo "  Usage:    [ -singleLine ] \"file 1\" [ . . . \"file N\" ]"
echo
echo "             You may substitute '-' in place of a file to read from STDIN."
echo
echo "             Use the -singleLine flag if you want to generate the HTML on"
echo "             a single line. This flag may show up anywhere on the command-"
echo "             line. But only data specified after this flag will be"
echo "             generated in this manner."
}
if [ 0 -gt 0 ]
then
process ""
else
usage
fi

How can I get cat to include the quoted material?

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
user1599051
  • 43
  • 1
  • 1
  • 7

1 Answers1

2

Replace:

cat <<EOS > script.bash

With:

cat <<'EOS' > script.bash

This prevents shell expansion of what is in the here document.

Documentation

From man bash:

   The format of here-documents is:

          <<[-]word
                  here-document
          delimiter

   No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on  word.   If  any
   characters  in  word  are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document
   are not expanded.  If word is unquoted, all lines of the here-document are subjected to parameter expansion, command  sub‐
   stitution, and arithmetic expansion.  In the latter case, the character sequence \<newline> is ignored, and \ must be used
   to quote the characters \, $, and `.

The most important part of that is:

If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion.

To stop all that expansion, just quote word which in your case is EOS.

John1024
  • 109,961
  • 14
  • 137
  • 171