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中国 荣誉推出

相关内容

CentOS Bash S...
CentOS Bash Shell:从入门到高效操作的全面指南 B...
2025-05-09 06:17:17
【售后科普】ubuntu系...
一、问题描述 在Ubuntu系统中,用户成功安装显卡驱动后,通过命...
2025-04-27 10:16:52
在Linux服务器上绑定域...
要把Linux服务器和域名绑定起来,让我们先简单聊聊这个过程。想象...
2025-02-27 13:49:16
不到24小时,开源版Dee...
机器之心报道 机器之心编辑部 OpenAI 被开源包围了。 ...
2025-02-06 08:43:00
Bysortingther...
将记录按升序排序,并仅显示平均工作经验的员工姓名。示例代码如下:S...
2025-01-12 20:02:47
byebug的版本中,wh...
在byebug的版本中,whereami是一个用于显示当前代码执行...
2025-01-12 20:02:42

热门资讯

使用 KRAWL 扫描 Kub... 用 KRAWL 脚本来识别 Kubernetes Pod 和容器中的错误。当你使用 Kubernet...
Helix:高级 Linux ... 说到 基于终端的文本编辑器,通常 Vim、Emacs 和 Nano 受到了关注。这并不意味着没有其他...
通过 SaltStack 管理... 我在搜索Puppet的替代品时,偶然间碰到了Salt。我喜欢puppet,但是我又爱上Salt了:)...
Epic 游戏商店现在可在 S... 现在可以在 Steam Deck 上运行 Epic 游戏商店了,几乎无懈可击! 但是,它是非官方的。...
如何在 Github 上创建一... 学习如何复刻一个仓库,进行更改,并要求维护人员审查并合并它。你知道如何使用 git 了,你有一个 G...
2024 开年,LLUG 和你... Hi,Linuxer,2024 新年伊始,不知道你是否已经准备好迎接新的一年~ 2024 年,Lin...
什么是 KDE Connect... 什么是 KDE Connect?它的主要特性是什么?它应该如何安装?本文提供了基本的使用指南。科技日...
JStock:Linux 上不... 如果你在股票市场做投资,那么你可能非常清楚投资组合管理计划有多重要。管理投资组合的目标是依据你能承受...
Opera 浏览器内置的 VP... 昨天我们报道过 Opera 浏览器内置了 VPN 服务,用户打开它可以防止他们的在线活动被窥视。不过...
Bazzite:专为 Stea... 为 Linux 桌面或者 Steam Deck 做好游戏准备,听起来都很刺激!对于一个专为 Linu...