如何在 bash 脚本中回显大量内容?

如何在 bash 脚本中回显大量内容?

我想要做

echo "[this thing]"

这东西是

\documentclass{article}
\usepackage{rotating}
\usepackage{pdfpages}
\usepackage{verbatim}
\usepackage{amsmath, amsfonts, amssymb, textcomp, mathtools, xparse}
\usepackage[T4, OT1]{fontenc}
\usepackage{graphicx}
\graphicspath{{/Users/Masi/Dropbox/Physiology/images/}}
% Animations cannot be included here
% \addmediapath{ {/Users/Masi/Dropbox/Physiology/animations/} }
\usepackage{newunicodechar}
\usepackage{multirow}
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert}
\DeclarePairedDelimiter{\norm}{\lVert}{\rVert}
\usepackage{color}
\usepackage{hyperref}
\usepackage{media9} % animations swf
\usepackage{Tabbing}
\usepackage{doi, natbib}
\hypersetup{
colorlinks=true,
linkcolor=blue,
citecolor=blue,
allcolors=blue
}
\usepackage[affil-it]{authblk}
\usepackage{import}
\usepackage{color}
\usepackage[normalem]{ulem}
\usepackage{titling} % Two titles in one document
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert}


%%%%%%%%%%%%%%%%%%%%%%%%%%% Question and Answer %%%%%%%%%%%%%%%%%

\usepackage[framemethod=tikz]{mdframed}

\mdfdefinestyle{ans}{
  linecolor=cyan,
  backgroundcolor=yellow!20,
    frametitlebackgroundcolor=green!40,
    frametitlerule=true
}
\newcounter{question}[section]%
\setcounter{question}{0}

\newenvironment{question}[1]{%
\refstepcounter{question}%
    \begin{mdframed}[style=ans,frametitle={Question: #1}]
}{%
    \end{mdframed}%
}%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%% Smaller things

\newtheorem{case}{Case logic}
\mdfdefinestyle{que}{
  linecolor=cyan,
  backgroundcolor=cyan!20,
}
\surroundwithmdframed[style=que]{case}

\newtheorem{sidenote}{Sidenote}
\mdfdefinestyle{que}{
  linecolor=cyan,
  backgroundcolor=cyan!20,
}
\surroundwithmdframed[style=que]{sidenote}


\newtheorem{citation}{Citation}
\mdfdefinestyle{que}{
  linecolor=cyan,
  backgroundcolor=yellow!20,
}
\surroundwithmdframed[style=que]{citation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




\newtheorem{theorem}{Theorem}
\newtheorem{proposition}[theorem]{Proposition}
\newenvironment{definition}[1][Definition]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}] \emph}{\end{trivlist}}
\providecommand{\keywords}[1]{\textbf{Keywords:} #1}


%%%%%%%%%%%%%%%%%%%%%%%%% Counter Section %%%%%%%%%%%%%%%%%%%%%%
\makeatletter
  \def\@part[#1]#2{%
    \ifnum \c@secnumdepth >\m@ne
      \refstepcounter{part}%
    \fi
    \addcontentsline{toc}{part}{#1}%
    {\parindent \z@ \raggedright
     \interlinepenalty \@M
     \normalfont
     \LARGE \bfseries #2%
     \markboth{}{}\par}%
    \nobreak
    \vskip 3ex
    \@afterheading}
\@addtoreset{section}{part}    
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

如何在 Bash 文件中很好地回显如此大的内容?

答案1

用一个这里的文档:

cat <<'EOF'
Data...
EOF

EOF注意:如果数据包含类似 或 反斜杠的内容,最好引用上面的定界词 ( ) 以避免扩展$foo,当然除非您想要扩展。例子:

$ cat <<EOF
$SHLVL \\
EOF

给出类似的东西:

3 \

尽管

$ cat <<'EOF'
$SHLVL \\
EOF

给出:

$SHLVL \\

答案2

@vinc17 的答案是正确的但不完整。 HEREDOC 方法很棒,但不能天真地使用它。看https://stackoverflow.com/a/11379627/763269了解更多需要注意的事情。

嵌入的空格和制表符不会像您在 HEREDOC 块中想象的那样被保留。 HEREDOC 标记(通常是EOF)应该用单引号引起来,以防止 shell 扩展。包含另一个 HEREDOC 块的 HEREDOC 块(即,如果您正在生成脚本)有很多问题。通过对 HEREDOC 块的内容进行 Base64 编码并对其进行扩展,可以避免所有此类问题。

答案3

我的乳胶不太好,你在文本中设置任何变量吗?否则,我会将乳胶放入额外的模板文件中,然后使用 cat 来打印它。如果您有一天想更换模板,这将使其更易于维护。

相关内容