自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Bash腳本中如何使用here文檔將數(shù)據(jù)寫入文件

系統(tǒng) Linux
here 文檔 (LCTT 譯注:here 文檔又稱作 heredoc )不是什么特殊的東西,只是一種 I/O 重定向方式,它告訴 bash shell 從當(dāng)前源讀取輸入,直到讀取到只有分隔符的行。

 

here document (LCTT 譯注:here 文檔又稱作 heredoc )不是什么特殊的東西,只是一種 I/O 重定向方式,它告訴 bash shell 從當(dāng)前源讀取輸入,直到讀取到只有分隔符的行。

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

here 文檔語(yǔ)法

語(yǔ)法是:

  1. command <<EOF
  2. cmd1
  3. cmd2 arg1
  4. EOF

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

  1. command <<-EOF
  2. msg1
  3. msg2
  4. $var on line
  5. EOF

或者

  1. command <<'EOF'
  2. cmd1
  3. cmd2 arg1
  4. $var won't expand as parameter substitution turned off
  5. by single quoting
  6. EOF

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

  1. command <<EOF > my_output_file.txt
  2. mesg1
  3. msg2
  4. msg3
  5. $var on $foo
  6. EOF

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

  1. command <<EOF >> my_output_file.txt
  2. mesg1
  3. msg2
  4. msg3
  5. $var on $foo
  6. EOF

示例

以下腳本將所需內(nèi)容寫入名為 /tmp/output.txt 的文件中:

  1. #!/bin/bash
  2. OUT=/tmp/output.txt
  3.  
  4. echo "Starting my script..."
  5. echo "Doing something..."
  6.  
  7. cat <<EOF >$OUT
  8. Status of backup as on $(date)
  9. Backing up files $HOME and /etc/
  10. EOF
  11.  
  12. echo "Starting backup using rsync..."

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

  1. $ cat /tmp/output.txt

示例輸出:

  1. Status of backup as on Thu Nov 16 17:00:21 IST 2017
  2. Backing up files /home/vivek and /etc/

禁用路徑名/參數(shù)/變量擴(kuò)展、命令替換、算術(shù)擴(kuò)展

$HOME 這類變量和像 $(date) 這類命令在腳本中會(huì)被解釋為替換。 要禁用它,請(qǐng)使用帶有 'EOF' 這樣帶有單引號(hào)的形式,如下所示:

  1. #!/bin/bash
  2. OUT=/tmp/output.txt
  3.  
  4. echo "Starting my script..."
  5. echo "Doing something..."
  6. # No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word.
  7. # If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document
  8. # are not expanded. So EOF is quoted as follows
  9. cat <<'EOF' >$OUT
  10. Status of backup as on $(date)
  11. Backing up files $HOME and /etc/
  12. EOF
  13.  
  14. echo "Starting backup using rsync..."

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

  1. $ cat /tmp/output.txt

示例輸出:

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

關(guān)于 tee 命令的使用

語(yǔ)法是:

  1. tee /tmp/filename <<EOF >/dev/null
  2. line 1
  3. line 2
  4. line 3
  5. $(cmd)
  6. $var on $foo
  7. EOF

或者通過(guò)在單引號(hào)中引用 EOF 來(lái)禁用變量替換和命令替換:

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

這是我更新的腳本:

  1. #!/bin/bash
  2. OUT=/tmp/output.txt
  3.  
  4. echo "Starting my script..."
  5. echo "Doing something..."
  6.  
  7. tee $OUT <<EOF >/dev/null
  8. Status of backup as on $(date)
  9. Backing up files $HOME and /etc/
  10. EOF
  11.  
  12. echo "Starting backup using rsync..."

關(guān)于內(nèi)存 here 文檔的使用

這是我更新的腳本:

  1. #!/bin/bash
  2. OUT=/tmp/output.txt
  3.  
  4. ## in memory here docs
  5. ## thanks https://twitter.com/freebsdfrau
  6. exec 9<<EOF
  7. Status of backup as on $(date)
  8. Backing up files $HOME and /etc/
  9. EOF
  10.  
  11. ## continue
  12. echo "Starting my script..."
  13. echo "Doing something..."
  14.  
  15. ## do it
  16. cat <&9 >$OUT
  17.  
  18. echo "Starting backup using rsync..."

 

責(zé)任編輯:龐桂玉 來(lái)源: Linux中國(guó)
相關(guān)推薦

2021-01-18 10:15:40

tee命令BashLinux

2021-05-11 07:50:31

BashShell腳本

2022-11-23 08:14:42

bash 腳本test 命令

2023-10-19 14:52:27

2022-11-25 07:53:26

bash腳本字符串

2023-06-12 12:43:52

Bash腳本

2019-09-17 12:13:05

BashLinuxCPU

2020-10-13 19:04:58

Bash信號(hào)捕獲Shell腳本

2022-03-30 09:32:32

BashtestLinux

2021-08-30 07:50:42

腳本語(yǔ)言命令行

2021-04-02 06:35:49

Bash讀寫文件Linux

2017-05-05 15:20:03

VimBash腳本bash-suppor

2020-06-24 15:30:39

Bashhistory命令Linux

2019-10-22 17:33:57

LinuxBash腳本

2021-01-14 08:00:00

服務(wù)器數(shù)據(jù)中心DokuWiki

2022-11-03 08:13:52

echo 命令Linux

2023-01-15 17:11:44

Rust

2023-08-23 12:12:45

BashLinux

2022-11-30 07:47:00

Bash腳本

2021-08-02 15:02:37

Go Excelize 開(kāi)發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)