shell中的heredoc很有用,对于临时的创建一个较长的字符串,比如脚本中要创建一个脚本等,就可以用这个 参考 Here Documents heredoc中的变量,shell命令都会被执行 下面的脚本创建了一串ftp命令,供ftp命令使用
Filename=`basename $1` # Strips pathname out of file name.
Server="ibiblio.org"
Directory="/incoming/Linux"
# These need not be hard-coded into script,
#+ but may instead be changed to command-line argument.
Password="your.e-mail.address" # Change above to suit.
ftp -n $Server << End-Of-Session
# -n option disables auto-logon
user anonymous "$Password"
binary
bell
cd $Directory
put "$Filename.lsm"
put "$Filename.tar.gz"
bye
End-Of-Session
注意,如果想输出原始字符,即不希望‘$’符号被替换为变量,需要把引导字符用引号引起来,或者添加转义符。
cat <<"SpecialCharTest"
Directory listing would follow
if limit string were not quoted.
`ls -l`
Arithmetic expansion would take place
if limit string were not quoted.
$((5 + 3))
A a single backslash would echo
if limit string were not quoted.
\\
SpecialCharTest