我怎么能写一个文档在这里在bash脚本文件? [英] How can I write a here doc to a file in Bash script?

查看:204
本文介绍了我怎么能写一个文档在这里在bash脚本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能写在这里的文档在bash脚本文件?

How can I write a here document to a file in Bash script?

推荐答案

阅读高级Bash脚本编程指南第19章。在这里,文件的。

Read the Advanced Bash-Scripting Guide Chapter 19. Here Documents.

下面是这将在的/ tmp / yourfilehere

cat << EOF > /tmp/yourfilehere
These contents will be written to the file.
        This line is indented.
EOF

请注意,最终的'EOF'(即 LimitString )不应该在这个词的前面任何空白,因为它意味着 LimitString 将不被认可。

Note that the final 'EOF' (The LimitString) should not have any whitespace in front of the word, because it means that the LimitString will not be recognized.

在一个shell脚本,你可能想使用缩进,使code可读,但是这可以让您在这里文档中缩进文本的不良影响。在这种情况下,使用&LT;&LT; - (后面跟一个破折号)禁用领先的标签(注意,为了测试这个,你需要替换制表符的前导空格,因为我无法在这里打的实际制表符。)

In a shell script, you may want to use indentation to make the code readable, however this can have the undesirable effect of indenting the text within your here document. In this case, use <<- (followed by a dash) to disable leading tabs (Note that to test this you will need to replace the leading whitespace with a tab character, since I cannot print actual tab characters here.)

#!/usr/bin/env bash

if true ; then
    cat <<- EOF > /tmp/yourfilehere
    The leading tab is ignored.
    EOF
fi

如果您不想在文字间preT变量,然后用单引号:

If you don't want to interpret variables in the text, then use single quotes:

cat << 'EOF' > /tmp/yourfilehere
The variable $FOO will not be interpreted.
EOF

要管通过命令管道定界符:

To pipe the heredoc through a command pipeline:

cat <<'EOF' |  sed 's/a/b/'
foo
bar
baz
EOF

输出:

foo
bbr
bbz

...或者写使用的定界符到文件须藤

cat <<'EOF' |  sed 's/a/b/' | sudo tee /etc/config_file.conf
foo
bar
baz
EOF

这篇关于我怎么能写一个文档在这里在bash脚本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆