cat with here document 不将带引号的字符串写入文件
cat with here document not writing quoted strings to file
我正在编写一个 shell 脚本,它使用 cat
自动编写一个 bash 脚本。但是,当我 运行 脚本时,生成的 bash 脚本不包含在引号中找到的数据。这是我的脚本:
cat <<EOS > script.bash
scriptDir=`dirname "[=10=]"`
scriptName=`basename "[=10=]"`
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=""
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
这里是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
如何让 cat 包含引用的 material?
替换:
cat <<EOS > script.bash
有:
cat <<'EOS' > script.bash
这可以防止 shell 扩展此处文档中的内容。
文档
来自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 `.
其中最重要的部分是:
If word is unquoted, all lines of the here-document are subjected to
parameter expansion, command substitution, and arithmetic expansion.
要停止所有扩展,只需引用 word
,在您的情况下是 EOS
。
我正在编写一个 shell 脚本,它使用 cat
自动编写一个 bash 脚本。但是,当我 运行 脚本时,生成的 bash 脚本不包含在引号中找到的数据。这是我的脚本:
cat <<EOS > script.bash
scriptDir=`dirname "[=10=]"`
scriptName=`basename "[=10=]"`
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=""
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
这里是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
如何让 cat 包含引用的 material?
替换:
cat <<EOS > script.bash
有:
cat <<'EOS' > script.bash
这可以防止 shell 扩展此处文档中的内容。
文档
来自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 `.
其中最重要的部分是:
If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion.
要停止所有扩展,只需引用 word
,在您的情况下是 EOS
。