Linux中國

Bash 腳本中如何使用 here 文檔將數據寫入文件

這對於向 ftp、cat、echo、ssh 和許多其他有用的 Linux/Unix 命令提供指令很有用。 此功能適用於 bash 也適用於 Bourne、Korn、POSIX 這三種 shell。

here 文檔語法

語法是:

command <<EOF
cmd1
cmd2 arg1
EOF

或者允許 shell 腳本中的 here 文檔使用 EOF<<- 以自然的方式縮進:

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

或者

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

或者 重定向並將其覆蓋 到名為 my_output_file.txt 的文件中:

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

重定向並將其追加到名為 my_output_file.txt 的文件中:

command <<EOF >> 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 <<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) 這類命令在腳本中會被解釋為替換。 要禁用它,請使用帶有 &apos;EOF&apos; 這樣帶有單引號的形式,如下所示:

#!/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 <<&apos;EOF&apos; >$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/null
line 1
line 2
line 3
$(cmd)
$var on $foo
EOF

或者通過在單引號中引用 EOF 來禁用變數替換和命令替換:

tee /tmp/filename <<&apos;EOF&apos; >/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 <<EOF >/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<<EOF
  Status of backup as on $(date)
  Backing up files $HOME and /etc/
EOF

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

## do it
cat <&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中國 榮譽推出


本文轉載來自 Linux 中國: https://github.com/Linux-CN/archive

對這篇文章感覺如何?

太棒了
0
不錯
0
愛死了
0
不太好
0
感覺很糟
0
雨落清風。心向陽

    You may also like

    Leave a reply

    您的郵箱地址不會被公開。 必填項已用 * 標註

    此站點使用Akismet來減少垃圾評論。了解我們如何處理您的評論數據

    More in:Linux中國