“expect<< - DONE”是什么意思?

时间:2016-08-05 08:27:38

标签: bash

这是什么意思:

expect <<- DONE
...
DONE

,如eof not recognized in Expect Script

尤其是<<-部分。

1 个答案:

答案 0 :(得分: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.

例如:

$ cat << EOF
>       hello
> there
> EOF
    hello
there

同样的事情,但使用<<-代替<<

$ cat <<- EOF
>       hello
> there
> EOF
hello
there

“hello”行上的前导TAB字符被剥离。

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

if cond; then
    cat <<- EOF
    hello
    there
    EOF
fi

习惯上在代码块中缩进行,如同if语句中一样,以提高可读性。 如果没有<<-运算符语法,我们将被迫编写上面的代码:

if cond; then
    cat << EOF
hello
there
EOF
fi

阅读起来非常不愉快,而且在更复杂的现实剧本中它会变得更糟。

请记住,@glenn-jackman指出:

  

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

相关问题