here document

深入解析heredocument在Shell脚本中的应用与特性
本文详细介绍了heredocument的概念、原理、语法、使用场景及注意事项,包括如何在Shell脚本中利用heredocument实现用户交互、字符串变量的替换与命令重定向,并探讨了其与标准输入重定向之间的冲突。通过具体实例展示了heredocument在实践中的应用,以及在不同语言环境下其功能的差异。

1 摘自wikipedia的介绍

In computer science, a here document (here-document, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also used for a form of multiline string literals that use similar syntax, preserving line breaks and other whitespace (including indentation) in the text.

Here documents originate in the Unix shell, and are found in sh, csh, ksh, Bash and zsh, among others. Here document-style string literals are found in various high-level languages, notably the Perl programming language (syntax inspired by Unix shell) and languages influenced by Perl, such as PHP and Ruby. Other high-level languages such as Python and Tcl have other facilities for multiline strings.

For here documents, whether treated as files or strings, some languages treat it as a format string, allow variable substitution and command substitution inside the literal.

The most common syntax for here documents, originating in Unix shells, is << followed by a delimiting identifier (often EOF or END), followed, starting on the next line, by the text to be quoted, and then closed by the same delimiting identifier on its own line. This syntax is because here documents are formally stream literals, and the content of the document is redirected to stdin (standard input) of the preceding command; the here document syntax is by analogy with the syntax for input redirection, which is < "take input from the output of the following command".

Other languages often use substantially similar syntax, but details of syntax and actual functionality can vary significantly. When used simply for string literals, the << does not indicate indirection, but is simply a starting delimiter convention. In some languages, such as Ruby, << is also used for input redirection, thus resulting in << being used twice if one wishes to redirect from a here document string literal.

2 shell的here document

2.1 原理与作用

        如wikipedia所介绍,here document就是声明相关部分是独立于脚本的一个document。其与改脚本的关系为,document的内容为改脚本中与它所在一行的preceding command的标准输入。
        经常用户交互命令的输入。

2.2 格式

shell <<定界符   重定向和管道相关
line1
line2
……
定界符

        两个定界符需要一致。定界符之间的部分被视为document,除了重定向相关的内容。document中可以使用变量。

2.3 例子

        这里举一个pstack中使用的here document作为例子。
$GDB --quiet $readnever -nx /proc/$1/exe $1 <<EOF 2>&1 |
$backtrace
EOF
sed -n \
    -e 's/^(gdb) //' \
    -e '/^#/p' \
    -e '/^Thread/p'

2.4 与标准输入重定向冲突

        前面介绍here document的原理是将document作为command的标准输入,那么当here document与标准输入重定向共同作用的时候会出现什么情况呢?
        答案是标准输入重定向会覆盖here document。可以理解为here document将document输入到“屏幕输入”,默认的“屏幕输入”就是标准输入。当对标准输入进行重定向的时候,那么here document就不会输入到标准输入了。
[~]$ cat <<EOF >hh 
> a
> b
> c
> EOF
[~]$ cat hh
a
b
c
[~]$ cat <<EOF >hh </dev/null
> a
> b
> c
> EOF
[~]$ cat hh





### 如何在 Bash 脚本中使用 Here Document 实现向文件写入带换行的内容 Here Document 是一种强大的 I/O 重定向方式,允许用户将多行文本传递给命令或脚本。以下是如何使用 Here Document 向文件写入带换行内容的详细说明和示例。 #### 使用 Here Document 写入带换行的内容到文件 Here Document 的基本语法如下: ```bash cat << 标识符 > 文件名 多行文本内容 标识符 ``` - `<<` 表示 Here Document 的开始。 - `标识符` 是一个字符串,用于标记 Here Document 的结束位置。它必须与结束标记完全一致[^1]。 - `>` 是重定向符号,表示将输出内容写入指定文件。如果需要追加内容,可以使用 `>>`。 #### 示例:向文件写入多行内容 以下是一个简单的示例,演示了如何使用 Here Document 将多行文本写入文件: ```bash cat << EOF > example.txt 这是第一行内容。 这是第二行内容。 这是第三行内容。 EOF ``` 在这个例子中,`EOF` 是 Here Document 的标识符。所有位于 `EOF` 和 `EOF` 之间的文本都会被写入 `example.txt` 文件,并且每行文本会自动保留换行符[^1]。 #### 示例:动态生成配置文件 Here Document 还可以结合变量和命令来动态生成配置文件。以下是一个示例: ```bash username="myuser" password="mypassword" cat << CONFIG > config.conf [database] host = localhost username = $username password = $password CONFIG ``` 在这个例子中,变量 `$username` 和 `$password` 的值会被替换为实际的值,并写入到 `config.conf` 文件中[^1]。 #### 示例:创建临时文件 Here Document 常用于创建临时文件。以下是一个示例: ```bash cat << EOF > /tmp/tempfile.txt 这是一个临时文件的内容。 可以在这里写入任意多行的文本。 EOF ``` 这段代码会将多行文本写入 `/tmp/tempfile.txt` 文件,并确保每一行都带有换行符[^2]。 #### 注意事项 - 如果需要避免变量被替换,可以在标识符前添加单引号,例如 `<'EOF'`[^3]。 - 标识符必须单独位于一行,且前后不能有任何空格或缩进[^1]。 - 如果需要追加内容而不是覆盖文件,可以使用 `>>` 替代 `>`。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值