linux文件中如何写入文本,技术|Bash 脚本中如何使用 here 文档将数据写入文件

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

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

here 文档语法

语法是:

command <

cmd1

cmd2 arg1

EOF

或者允许 shell 脚本中的 here 文档使用 EOF<

command <

msg1

msg2

$var on line

EOF

或者

command <

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 <$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 </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<

Status of backup as on $(date)

Backing up files $HOME and /etc/

EOF

## continue

echo "Starting my script..."

echo "Doing something..."

## do it

cat $OUT

echo "Starting backup using rsync..."

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值