From c1906032d3d70fdf913dfaaa68a632af1daffd33 Mon Sep 17 00:00:00 2001 From: "Xingyu.Wang" Date: Mon, 12 Nov 2018 09:51:47 +0800 Subject: [PATCH] PRF:20171116 How to use a here documents to write data to a file in bash script.md @Flowsnow --- ... to write data to a file in bash script.md | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/translated/tech/20171116 How to use a here documents to write data to a file in bash script.md b/translated/tech/20171116 How to use a here documents to write data to a file in bash script.md index f5dadc540d..3342e554e1 100644 --- a/translated/tech/20171116 How to use a here documents to write data to a file in bash script.md +++ b/translated/tech/20171116 How to use a here documents to write data to a file in bash script.md @@ -1,13 +1,16 @@ -Bash脚本中如何使用 here 文档将数据写入文件 +Bash 脚本中如何使用 here 文档将数据写入文件 ====== -here 文档here document (LCTT译注:here文档又称作 heredoc )不是什么特殊的东西,只是一种 I/O 重定向方式,它告诉bash shell从当前源读取输入,直到读取到只有分隔符的行。 -![redirect output of here document to a text file][1] -这对于向 ftp,cat,echo,ssh 和许多其他有用的 Linux/Unix 命令提供指令很有用。 此功能适用于 bash也适用于 Bourne,Korn,POSIX 这三种 shell。 +here 文档here document (LCTT 译注:here 文档又称作 heredoc )不是什么特殊的东西,只是一种 I/O 重定向方式,它告诉 bash shell 从当前源读取输入,直到读取到只有分隔符的行。 -## here 文档语法 +![redirect output of here document to a text file][1] + +这对于向 ftp、cat、echo、ssh 和许多其他有用的 Linux/Unix 命令提供指令很有用。 此功能适用于 bash 也适用于 Bourne、Korn、POSIX 这三种 shell。 + +### here 文档语法 语法是: + ``` command < my_output_file.txt +command < my_output_file.txt mesg1 msg2 msg3 @@ -45,10 +50,10 @@ command << EOF > my_output_file.txt EOF ``` -或**重定向并将其追加**到名为my_output_file.txt的文件中: +或**重定向并将其追加**到名为 `my_output_file.txt` 的文件中: ``` -command << EOF >> my_output_file.txt +command <> my_output_file.txt mesg1 msg2 msg3 @@ -56,9 +61,9 @@ command << EOF >> my_output_file.txt EOF ``` -## 示例 +### 示例 -以下脚本将所需内容写入名为/tmp/output.txt的文件中: +以下脚本将所需内容写入名为 `/tmp/output.txt` 的文件中: ``` #!/bin/bash @@ -86,12 +91,11 @@ $ cat /tmp/output.txt ``` Status of backup as on Thu Nov 16 17:00:21 IST 2017 Backing up files /home/vivek and /etc/ - ``` -### 禁用路径名/参数/变量扩展,命令替换,算术扩展 +### 禁用路径名/参数/变量扩展、命令替换、算术扩展 -像 $HOME 这类变量和像 $(date) 这类命令在脚本中会被解释为替换。 要禁用它,请使用带有 'EOF' 这样带有单引号的形式,如下所示: +像 `$HOME` 这类变量和像 `$(date)` 这类命令在脚本中会被解释为替换。 要禁用它,请使用带有 `'EOF'` 这样带有单引号的形式,如下所示: ``` #!/bin/bash @@ -110,7 +114,7 @@ EOF echo "Starting backup using rsync..." ``` -你可以使用[cat命令][3]查看/tmp/output.txt文件: +你可以使用 [cat 命令][3]查看 `/tmp/output.txt` 文件: ``` $ cat /tmp/output.txt @@ -124,9 +128,10 @@ $ cat /tmp/output.txt ``` -## 关于 tee 命令的使用 +### 关于 tee 命令的使用 语法是: + ``` tee /tmp/filename </dev/null line 1 @@ -137,7 +142,7 @@ $var on $foo EOF ``` -或者通过在单引号中引用EOF来禁用变量替换和命令替换: +或者通过在单引号中引用 `EOF` 来禁用变量替换和命令替换: ``` tee /tmp/filename <<'EOF' >/dev/null @@ -150,6 +155,7 @@ EOF ``` 这是我更新的脚本: + ``` #!/bin/bash OUT=/tmp/output.txt @@ -165,9 +171,10 @@ EOF echo "Starting backup using rsync..." ``` -## 关于内存 here 文档的使用 +### 关于内存 here 文档的使用 这是我更新的脚本: + ``` #!/bin/bash OUT=/tmp/output.txt @@ -189,19 +196,17 @@ cat <&9 >$OUT echo "Starting backup using rsync..." ``` - - -------------------------------------------------------------------------------- via: https://www.cyberciti.biz/faq/using-heredoc-rediection-in-bash-shell-script-to-write-to-file/ 作者:[Vivek Gite][a] 译者:[Flowsnow](https://github.com/Flowsnow) -校对:[校对者ID](https://github.com/校对者ID) +校对:[wxy](https://github.com/wxy) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 [a]: https://www.cyberciti.biz [1]: https://www.cyberciti.biz/media/new/faq/2017/11/redirect-output-of-here-document-to-a-text-file.jpg [2]: https://bash.cyberciti.biz/guide/Here_documents -[3]: https//www.cyberciti.biz/faq/linux-unix-appleosx-bsd-cat-command-examples/ "See Linux/Unix cat command examples for more info" \ No newline at end of file +[3]: https//www.cyberciti.biz/faq/linux-unix-appleosx-bsd-cat-command-examples/