【问题标题】:Bash syntax: What is the "<<"?Bash 语法:什么是“<<”?
【发布时间】:2009-12-18 19:23:13
【问题描述】:

有人能解释一下下面代码中的“

mysql test<<E0Q
Select * from signins

我会尝试自己搜索,但符号很难搜索...

谢谢, 丹

【问题讨论】:

    标签: bash syntax


    【解决方案1】:

    这是一个“这里的文件”,见http://www.linuxjournal.com/article/3771

    【讨论】:

      【解决方案2】:

      Here docs,或者一种将大文本块轻松导入程序的方法。

      【讨论】:

        【解决方案3】:

        它不仅用于管道。例如,Linux From Scratch 演练中使用的脚本将 heredocs 与cat 命令和输出重定向运算符 (&gt;) 结合使用。这是一个这样的例子:

        user@domain ~$ cat >test.c <<EOF
        int main(void){return 0;}
        EOF
        user@domain ~$
        

        这会将

        【讨论】:

        • 为什么这比:echo "int main(void){return 0;}">test.c 更好?如果我理解正确,那是一回事。
        • 没有好坏之分;这只是一个例子。但是,假设您想要多行和/或使用带引号的字符串。支持使用 heredocs 的论据主要基于这两件事,因为heredoc 通常用作“超级引用”,以允许您在必要时混合“和”字符。以这种方式使用它特别有利,因为 ' !双引号内的字符似乎会触发有关未找到事件的错误。因此,必须使用单引号,但会丢失诸如变量扩展之类的有用功能。
        • here 文档的主要好处是它比长文档的一系列 echo 命令要简洁得多。作为该优势的一部分,它还避免了每行一个回显进程的开销。
        【解决方案4】:

        它们被称为此处文档。来自手册:

        这里的文档

        This type of redirection instructs the shell to read input from the
        current source until a line containing only word (with no trailing blanks)  is
        seen.   All  of  the lines read up to that point are then used as the standard
        input for a command.
        
        The format of here-documents is:
        
               <<[-]word
                       here-document
               delimiter
        

        【讨论】:

          【解决方案5】:

          例如,如果将echo 通过管道传递到read,则变量赋值会“丢失”,因为读取是在子shell 中执行的。您可以使用 here-doc 或 here-string 使其在当前 shell 中执行:

          read -a var <<< "some text"
          

          这是您可能会看到的另一个构造示例,称为“here-string”,它类似于 here-doc。

          参见thisthis

          【讨论】:

            猜你喜欢
            • 2016-06-19
            • 1970-01-01
            • 2014-09-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-13
            • 2011-11-14
            相关资源
            最近更新 更多