Bash 脚本中如何使用 here 文档将数据写入文件
创始人
2024-03-02 00:56:21
0

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

redirect output of here document to a text file

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

here 文档语法

语法是:

command <

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

command <<-EOF
  msg1
  msg2 
  $var on line 
EOF

或者

command <<'EOF'
 cmd1
 cmd2 arg1
 $var won't expand as parameter substitution turned off
 by single quoting
EOF

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

command < my_output_file.txt
 mesg1
 msg2
 msg3
 $var on $foo
EOF

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

command <> my_output_file.txt
 mesg1
 msg2
 msg3
 $var on $foo
EOF

示例

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

#!/bin/bash
OUT=/tmp/output.txt

echo "Starting my script..."
echo "Doing something..."

cat <$OUT
  Status of backup as on $(date)
  Backing up files $HOME and /etc/
EOF

echo "Starting backup using rsync..."

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

$ 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' 这样带有单引号的形式,如下所示:

#!/bin/bash
OUT=/tmp/output.txt

echo "Starting my script..."
echo "Doing something..."
# No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word.  
# If any part of word is quoted, the delimiter  is  the  result  of  quote removal  on word, and the lines in the here-document 
# are not expanded. So EOF is quoted as follows
cat <<'EOF' >$OUT
  Status of backup as on $(date)
  Backing up files $HOME and /etc/
EOF

echo "Starting backup using rsync..."

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

$ cat /tmp/output.txt

示例输出:

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

关于 tee 命令的使用

语法是:

tee /tmp/filename </dev/null
line 1
line 2
line 3
$(cmd)
$var on $foo
EOF

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

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

这是我更新的脚本:

#!/bin/bash
OUT=/tmp/output.txt

echo "Starting my script..."
echo "Doing something..."

tee $OUT </dev/null
  Status of backup as on $(date)
  Backing up files $HOME and /etc/
EOF

echo "Starting backup using rsync..."

关于内存 here 文档的使用

这是我更新的脚本:

#!/bin/bash
OUT=/tmp/output.txt

## in memory here docs 
## thanks https://twitter.com/freebsdfrau
exec 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 译者:Flowsnow 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关内容

AI辅助Linux系统管理...
我并非所有场景都推崇使用AI。在创意类工作中,我坚持认为AI介入空...
2026-06-20 12:53:31
基础Ubuntu网络代理的...
Ubuntu系统的网络代理配置是许多用户日常使用中的必备技能,从基...
2025-08-24 16:46:06
HEREWEGO!拜仁签利...
北京时间7月28日,多家媒体报道,拜仁接近签下利物浦的哥伦比亚前锋...
2025-07-28 08:42:16
编写CentOS系统下的B...
面对重复繁琐的服务器操作,手动输入命令效率低下且易出错。为了解决这...
2025-06-15 08:43:24
「SAP ABAP」OPE...
💂作者简介: THUNDER王&#x...
2025-05-30 12:47:37
在 Linux/Unix ...
在 Linux/Unix 系统中,动态库(.so 文件)的搜索路径...
2025-05-21 10:17:25

热门资讯

“Linux 中国” 开源社区... 这是一个艰难的决定 —— 这不是调侃,而是当你真正做出改变了你十几年生活轨迹的决定时,你就会明白这个...
硬核观察 #1263 德国铁路... #1 德国铁路公司招聘 Windows for Workgroups 3.11 管理员该职位要求了解...
人工智能教程(七):Sciki... 在本系列的 上一篇文章 中,我们用 TensorFlow 构建了第一个神经网络,然后还通过 Kera...
关闭 WSL 中正在运行的 L... 以下是使用 WSL 关闭在 Windows 内运行的 Linux 系统的不同方法。你使用 WSL 在...
MX Linux 23.2 “... 最新的 MX Linux 23.2 小版本更新带来了基本的改进和新增功能。MX Linux 推出了 ...
硬核观察 #1262 网络上最... #1 网络上最古老的软件存档库之一即将关闭美国新墨西哥州立大学(NMSU)最近宣布,即将于 2024...
如何在 RHEL 上通过绑定配... 在 Linux 中,你可以根据自己的需要创建高级网络设置,如 网络绑定 Network Bondin...
Linux Matrix 消息... Matrix 用户可以升级 Fractal 6 了!Fractal 是目前 最好的 Matrix 客...
如何通过 VLC 使用字幕 使用 VLC 媒体播放器播放和管理字幕的新手指南。我是一个超级动漫迷,如果你想观看最新的剧集,那么你...
硬核观察 #1261 X 计划... #1 X 计划在奥斯汀建立内容审核总部据报道,该部门将招聘 100 名全职员工,将主要关注 CSE ...