如何在 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 來列印它。如果您有一天想更換模板,這將使其更易於維護。

相關內容