温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Bash脚本中怎么使用here文档将数据写入文件

发布时间:2021-08-30 17:47:16 来源:亿速云 阅读:181 作者:chen 栏目:系统运维

这篇文章主要介绍“Bash脚本中怎么使用here文档将数据写入文件”,在日常操作中,相信很多人在Bash脚本中怎么使用here文档将数据写入文件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Bash脚本中怎么使用here文档将数据写入文件”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

 Bash脚本中怎么使用here文档将数据写入文件

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

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

here 文档语法

语法是:

command <<EOFcmd1cmd2 arg1EOF

或者允许 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 quotingEOF

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

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

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

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

示例

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

#!/bin/bashOUT=/tmp/output.txt echo "Starting my script..."echo "Doing something..." 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 Thu Nov 16 17:00:21 IST 2017 Backing up files /home/vivek and /etc/

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

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

#!/bin/bashOUT=/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 followscat <<'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 <<EOF >/dev/nullline 1line 2line 3$(cmd)$var on $fooEOF

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

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

这是我更新的脚本:

#!/bin/bashOUT=/tmp/output.txt echo "Starting my script..."echo "Doing something..." tee $OUT <<EOF >/dev/null  Status of backup as on $(date)  Backing up files $HOME and /etc/EOF echo "Starting backup using rsync..."

关于内存 here 文档的使用

这是我更新的脚本:

#!/bin/bashOUT=/tmp/output.txt ## in memory here docs ## thanks https://twitter.com/freebsdfrauexec 9<<EOF  Status of backup as on $(date)  Backing up files $HOME and /etc/EOF ## continueecho "Starting my script..."echo "Doing something..." ## do itcat <&9 >$OUT echo "Starting backup using rsync..."

到此,关于“Bash脚本中怎么使用here文档将数据写入文件”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI