Bash脚本中如何使用here文档将数据写入文件

副标题#e#

 Bash脚本中如何使用here文档将数据写入文件

here document (LCTT 译注:here 文档又称作 heredoc )不是什么特殊的东西,,只是一种 I/O 重定向方式,它告诉 bash shell 从当前源读取输入,直到读取到只有分隔符的行。

这对于向 ftp、cat、echo、ssh 和许多其他有用的 Linux/Unix 命令提供指令很有用。 此功能适用于 bash 也适用于 Bourne、Korn、POSIX 这三种 shell。

here 文档语法

语法是:

  1. command <<EOF
  2. cmd1
  3. cmd2 arg1
  4. EOF

或者允许 shell 脚本中的 here 文档使用 EOF<<- 以自然的方式缩进:

  1. command <<-EOF
  2. msg1
  3. msg2
  4. $var on line
  5. EOF

或者

  1. command <<'EOF'
  2. cmd1
  3. cmd2 arg1
  4. $var won't expand as parameter substitution turned off
  5. by single quoting
  6. EOF

或者 重定向并将其覆盖 到名为 my_output_file.txt 的文件中:

  1. command <<EOF > my_output_file.txt
  2. mesg1
  3. msg2
  4. msg3
  5. $var on $foo
  6. EOF

或重定向并将其追加到名为 my_output_file.txt 的文件中:

  1. command <<EOF >> my_output_file.txt
  2. mesg1
  3. msg2
  4. msg3
  5. $var on $foo
  6. EOF

示例

以下脚本将所需内容写入名为 /tmp/output.txt 的文件中:

  1. #!/bin/bash
  2. OUT=/tmp/output.txt
  3.  
  4. echo "Starting my script..."
  5. echo "Doing something..."
  6.  
  7. cat <<EOF >$OUT
  8. Status of backup as on $(date)
  9. Backing up files $HOME and /etc/
  10. EOF
  11.  
  12. echo "Starting backup using rsync..."

你可以使用cat命令查看/tmp/output.txt文件:

  1. $ cat /tmp/output.txt

示例输出:

  1. Status of backup as on Thu Nov 16 17:00:21 IST 2017
  2. Backing up files /home/vivek and /etc/

禁用路径名/参数/变量扩展、命令替换、算术扩展

$HOME 这类变量和像 $(date) 这类命令在脚本中会被解释为替换。 要禁用它,请使用带有 'EOF' 这样带有单引号的形式,如下所示:

  1. #!/bin/bash
  2. OUT=/tmp/output.txt
  3.  
  4. echo "Starting my script..."
  5. echo "Doing something..."
  6. # No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word.
  7. # If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document
  8. # are not expanded. So EOF is quoted as follows
  9. cat <<'EOF' >$OUT
  10. Status of backup as on $(date)
  11. Backing up files $HOME and /etc/
  12. EOF
  13.  
  14. echo "Starting backup using rsync..."

你可以使用 cat 命令查看 /tmp/output.txt 文件:

  1. $ cat /tmp/output.txt

#p#副标题#e#

示例输出:

  1. Status of backup as on $(date)
  2. Backing up files $HOME and /etc/

关于 tee 命令的使用

语法是:

  1. tee /tmp/filename <<EOF >/dev/null
  2. line 1
  3. line 2
  4. line 3
  5. $(cmd)
  6. $var on $foo
  7. EOF

或者通过在单引号中引用 EOF 来禁用变量替换和命令替换:

  1. tee /tmp/filename <<'EOF' >/dev/null
  2. line 1
  3. line 2
  4. line 3
  5. $(cmd)
  6. $var on $foo
  7. EOF

这是我更新的脚本:

  1. #!/bin/bash
  2. OUT=/tmp/output.txt
  3.  
  4. echo "Starting my script..."
  5. echo "Doing something..."
  6.  
  7. tee $OUT <<EOF >/dev/null
  8. Status of backup as on $(date)
  9. Backing up files $HOME and /etc/
  10. EOF
  11.  
  12. echo "Starting backup using rsync..."

关于内存 here 文档的使用

这是我更新的脚本:

  1. #!/bin/bash
  2. OUT=/tmp/output.txt
  3.  
  4. ## in memory here docs
  5. ## thanks https://twitter.com/freebsdfrau
  6. exec 9<<EOF
  7. Status of backup as on $(date)
  8. Backing up files $HOME and /etc/
  9. EOF
  10.  
  11. ## continue
  12. echo "Starting my script..."
  13. echo "Doing something..."
  14.  
  15. ## do it
  16. cat <&9 >$OUT
  17.  
  18. echo "Starting backup using rsync..."

#p#副标题#e##p#分页标题#e#

【编辑推荐】

  1. Linux 常用命令:系统状态篇
  2. 理解Linux的硬链接与软链接(一)
  3. 理解Linux的硬链接与软链接(二)
  4. 如何使用Linux命令或文件管理器来移动文件?
  5. 运维必备!Linux 远程数据同步工具详解

【责任编辑:庞桂玉 TEL:(010)68476606】

点赞 0

dawei

【声明】:嘉兴站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。