Ste*_*ski 983
阅读Advanced Bash-Scripting Guide 第19章.这里的文档.
这是一个将内容写入文件的示例 /tmp/yourfilehere
cat << EOF > /tmp/yourfilehere
These contents will be written to the file.
This line is indented.
EOF
Run Code Online (Sandbox Code Playgroud)
请注意,最后的'EOF'(The LimitString)不应该在单词前面有任何空格,因为这意味着LimitString不会被识别.
在shell脚本中,您可能希望使用缩进来使代码可读,但是这会使您在此文档中缩进文本产生不良影响.在这种情况下,使用<<-(后跟破折号)禁用前导选项卡(请注意,为了测试它,您需要使用制表符替换前导空格,因为我无法在此处打印实际的制表符.)
#!/usr/bin/env bash
if true ; then
cat <<- EOF > /tmp/yourfilehere
The leading tab is ignored.
EOF
fi
Run Code Online (Sandbox Code Playgroud)
如果您不想解释文本中的变量,请使用单引号:
cat << 'EOF' > /tmp/yourfilehere
The variable $FOO will not be interpreted.
EOF
Run Code Online (Sandbox Code Playgroud)
通过命令管道管道heredoc:
cat <<'EOF' | sed 's/a/b/'
foo
bar
baz
EOF
Run Code Online (Sandbox Code Playgroud)
输出:
foo
bbr
bbz
Run Code Online (Sandbox Code Playgroud)
...或者使用sudo以下方法将heredoc写入文件:
cat <<'EOF' | sed 's/a/b/' | sudo tee /etc/config_file.conf
foo
bar
baz
EOF
Run Code Online (Sandbox Code Playgroud)
Liv*_*ven 142
而不是使用cat和I/O重定向,而是使用它可能是有用的tee:
tee newfile <<EOF
line 1
line 2
line 3
EOF
Run Code Online (Sandbox Code Playgroud)
它更简洁,加上与重定向操作符不同,sudo如果您需要使用root权限写入文件,它可以与之结合使用.
Tom*_*che 55
注意:
问题(如何在bash脚本中将文件(又名heredoc)写入文件?)(至少)有3个主要的独立维度或子问题:
root)是否拥有该文件?(还有其他维度/子问题我认为不重要.请考虑编辑此答案以添加它们!)以下是上面列出的问题维度的一些更重要的组合,具有各种不同的分隔标识符 - 没有什么神圣的EOF,只要确保你的heredoc 中没有出现用作分界标识符的字符串:
要覆盖您拥有的现有文件(或写入新文件),请替换heredoc中的变量引用:
cat << EOF > /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, with the variable contents substituted.
EOF
Run Code Online (Sandbox Code Playgroud)要附加您拥有的现有文件(或写入新文件),请替换heredoc中的变量引用:
cat << FOE >> /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, with the variable contents substituted.
FOE
Run Code Online (Sandbox Code Playgroud)要使用heredoc的文字内容覆盖您拥有的现有文件(或写入新文件):
cat << 'END_OF_FILE' > /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, without the variable contents substituted.
END_OF_FILE
Run Code Online (Sandbox Code Playgroud)要附加您拥有的现有文件(或写入新文件),请使用heredoc的文字内容:
cat << 'eof' >> /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, without the variable contents substituted.
eof
Run Code Online (Sandbox Code Playgroud)要覆盖root拥有的现有文件(或写入新文件),请替换heredoc中的变量引用:
cat << until_it_ends | sudo tee /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, with the variable contents substituted.
until_it_ends
Run Code Online (Sandbox Code Playgroud)要附加user = foo拥有的现有文件(或写入新文件),并使用heredoc的文字内容:
cat << 'Screw_you_Foo' | sudo -u foo tee -a /path/to/your/file
This line will write to the file.
${THIS} will also write to the file, without the variable contents substituted.
Screw_you_Foo
Run Code Online (Sandbox Code Playgroud)go2*_*ull 35
基于@Livven的答案,这里有一些有用的组合.
变量替换,保留前导选项卡,覆盖文件,回显到标准输出
tee /path/to/file <<EOF
${variable}
EOF
Run Code Online (Sandbox Code Playgroud)没有变量替换,保留选项卡保留,覆盖文件,回显到stdout
tee /path/to/file <<'EOF'
${variable}
EOF
Run Code Online (Sandbox Code Playgroud)变量替换,删除前导标签,覆盖文件,回显到标准输出
tee /path/to/file <<-EOF
${variable}
EOF
Run Code Online (Sandbox Code Playgroud)变量替换,保留前导选项卡,附加到文件,回显到stdout
tee -a /path/to/file <<EOF
${variable}
EOF
Run Code Online (Sandbox Code Playgroud)变量替换,保留前导选项卡,覆盖文件,没有回显到stdout
tee /path/to/file <<EOF >/dev/null
${variable}
EOF
Run Code Online (Sandbox Code Playgroud)以上可以结合sudo以及
sudo -u USER tee /path/to/file <<EOF
${variable}
EOF
Run Code Online (Sandbox Code Playgroud)Ser*_*ndt 16
如果目标文件需要root权限,请使用|sudo tee而不是>:
cat << 'EOF' |sudo tee /tmp/yourprotectedfilehere
The variable $FOO will *not* be interpreted.
EOF
Run Code Online (Sandbox Code Playgroud)
Jos*_*eld 11
对于可能遇到此问题的未来人员,以下格式有效:
(cat <<- _EOF_
LogFile /var/log/clamd.log
LogTime yes
DatabaseDirectory /var/lib/clamav
LocalSocket /tmp/clamd.socket
TCPAddr 127.0.0.1
SelfCheck 1020
ScanPDF yes
_EOF_
) > /etc/clamd.conf
Run Code Online (Sandbox Code Playgroud)
对于那些寻找纯 bash 解决方案(或需要速度)的人来说,这是一个不带 cat 的简单解决方案:
# here-doc tab indented
{ read -r -d '' || printf >file '%s' "$REPLY"; } <<-EOF
foo bar
EOF
Run Code Online (Sandbox Code Playgroud)
或用于简单的“mycat”功能(并避免将 REPLY 留在环境中):
mycat() {
local REPLY
read -r -d '' || printf '%s' "$REPLY"
}
mycat >file <<-EOF
foo bar
EOF
Run Code Online (Sandbox Code Playgroud)
“mycat”与 OS cat 的快速速度比较(在我的 OSX 笔记本电脑上循环 >/dev/null 1000 次):
mycat:
real 0m1.507s
user 0m0.108s
sys 0m0.488s
OS cat:
real 0m4.082s
user 0m0.716s
sys 0m1.808s
Run Code Online (Sandbox Code Playgroud)
注意:mycat 不处理文件参数,它只处理“将定界符写入文件”的问题