"期待<< - 完成"是什么意思?

Al3*_*l3n 2 bash

这个用法是什么意思:

expect <<- DONE
...
DONE
Run Code Online (Sandbox Code Playgroud)

如在Exof Script中未被识别的那样

特别是那个<<-部分.

Sto*_*ica 6

man bash,如果你搜索<<-(通过输入:/<<-Enter),你会发现:

   If the redirection operator is <<-, then all leading tab characters are
   stripped from input lines and  the  line  containing  delimiter.   This
   allows  here-documents within shell scripts to be indented in a natural
   fashion.
Run Code Online (Sandbox Code Playgroud)

例如:

$ cat << EOF
>       hello
> there
> EOF
    hello
there
Run Code Online (Sandbox Code Playgroud)

同样的事情,但使用<<-而不是<<

$ cat <<- EOF
>       hello
> there
> EOF
hello
there
Run Code Online (Sandbox Code Playgroud)

"hello"行上的前导TAB字符被剥离.

正如man页面引用的那样,这在shell脚本中很有用,例如:

if cond; then
    cat <<- EOF
    hello
    there
    EOF
fi
Run Code Online (Sandbox Code Playgroud)

if为了更好的可读性,习惯上在代码块中缩进行,如此语句中所示.如果没有<<-运算符语法,我们将被迫编写上面的代码,如下所示:

if cond; then
    cat << EOF
hello
there
EOF
fi
Run Code Online (Sandbox Code Playgroud)

这是非常不愉快的阅读,并且在更复杂的现实脚本中变得更糟.

请记住,正如@ glenn-jackman指出的那样:

请注意,只删除制表符,而不是任意空格.请注意,文本编辑器不会将制表符转换为空格.