blog:shell:04_shell_heredoc

Shell Here Document

Here Document是一种特殊的代码块, 通常作为一种I/O重定向的方式给命令提供输入 格式如下:

COMMAND <<InputComesFromHERE
...
...
...
InputComesFromHERE

以“« delimiter”的形式标识多行字符串的开始,之后新起一行包含相应的文本, 最后以相同的delimiter独占一行,标识多行字符串的结束。

定界符(delimiter)可以是任意的字符串

例1: 给tr命令提供输入

$ tr a-z A-Z << EOF
> one two three
> uno ods tres
> EOF
ONE TWO THREE
UNO ODS TRES

例2:非交互式地使用vim 模拟sed的用法,在输入文件中插入两行文本

#/bin/sh

# Noninteractive use of 'vi' to edit a file.
# Emulates 'sed'.

E_BADARGS=85

if [ -z "$1" ]
then
  echo "Usage: `basename $0` filename"
  exit $E_BADARGS
fi

TARGETFILE=$1

# Insert 2 lines in file, then save.
#--------Begin here document-----------#
vim $TARGETFILE <<x23LimitStringx23
i
This is line 1 of the example file.
This is line 2 of the example file.

ZZ
x23LimitStringx23
#----------End here document-----------#

#  Note that ^[ above is a literal escape
#+ typed by Control-V <Esc>.

#  Bram Moolenaar points out that this may not work with 'vim'
#+ because of possible problems with terminal interaction.

exit

例3:非交互式地使用ex 将所有txt文件中的Smith替换为Jones

#  Replace all instances of "Smith" with "Jones"
#+ in files with a ".txt" filename suffix. 

ORIGINAL=Smith
REPLACEMENT=Jones

for file in $(fgrep -l $ORIGINAL *.txt)
do
  # -------------------------------------
  ex $file <<EOF
  %s/$ORIGINAL/$REPLACEMENT/g
  wq
EOF
  # %s is the "ex" substitution command.
  # wq is write-and-quit.
  # -------------------------------------
done

例4:使用cat输出多行信息

cat <<End-of-message
-------------------------------------
This is line 1 of the message.
This is line 2 of the message.
This is line 3 of the message.
This is line 4 of the message.
This is the last line of the message.
-------------------------------------
End-of-message

如果将cat << End-of-message替换为 cat > $filename << End-of-message 那么cat将会把输出写入文件中,而不会输出到标准输出

格式如下:

COMMAND <<-delimiter
...
...
...
delimiter

例:

#!/bin/sh

cat <<-ENDOFMESSAGE
	This is line 1 of the message.
	This is line 2 of the message.
	This is line 3 of the message.
	This is line 4 of the message.
	This is the last line of the message.
ENDOFMESSAGE

exit 0

delimiter以 - 开头, 将会去除行首的tab符号,但不包括空格符号 注意: 对非行首的tab符号无效

默认情况下, HereDocuments支持变量替换

例1:

cat << EOF
current dir=${PWD}
EOF
命令输出结果如下:
current dir=/data/repo/blog

可以通过对delimiter的任何部分添加引号标签,取消变量替换

例2:

cat << "EOF"
current dir=${PWD}
EOF
命令输出结果如下:
current dir=${PWD}

例3:

cat << "E"OF
current dir=${PWD}
EOF
命令输出结果如下:
current dir=${PWD}

使用Anonymous Here Documents对代码块进行多行注释

格式如下:


: <<COMMENTBLOCK
  echo "This line will not echo."
  ......
  ......
COMMENTBLOCK

Here Document 将会创建一个临时文件,这些文件在打开就被删除,其他进程无法访问该文件

$ bash -c 'lsof -a -p $$ -d0' << EOF
EOF
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF  NODE NAME
lsof    4399 caodan    0r   REG   0,36        0 64130 /tmp/sh-thd-1008598812 (deleted)

[0] The Linux Documentation Project __ Advanced Bash-Scripting Guide

  Chapter19. Here Documents  --Aldous Huxley, Island 
  `http://www.tldp.org/LDP/abs/html/here-docs.html`
  • blog/shell/04_shell_heredoc.txt
  • 最后更改: 2022/01/09 17:23
  • 127.0.0.1