如何:其中;<“END”在bash工作创造一个多行注释块? [英] How does : <<'END' work in bash to create a multi-line comment block?

查看:130
本文介绍了如何:其中;<“END”在bash工作创造一个多行注释块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了如何 href=\"http://stackoverflow.com/a/947936/1093607\">注释(由@sunny256 ):


 #!/斌/庆典
评论呼应前
:其中;<END
喇嘛喇嘛
blurfl
结束
评论后回应

围绕 END 分隔符是重要的,否则像块里面的东西,例如 $(命令)将被解析和执行。


这可能是丑陋,但它的作品,我渴望知道这意味着什么。任何人都可以简单地解释一下吗?我已经找到说明:,这是空操作或真。但它没有任何意义,我反正调用无操作或真....


解决方案

注释的目标是成为文本是的的跨preTED或code执行。

本来,UNIX外壳没有一个注释语法的本身的。它没有,但是,有什么也不做的命令,它忽略它的参数和返回真。所以,你可以把这样的线在你的脚本:

 :这是一个注释

外壳将运行命令,但不会有事的。任务完成了,对吧?

的问题是,该线路仍被视为一个命令。词法分析,参数替换,字分裂,并且这样仍然发生在那些注定将要被忽略的参数。这意味着处理运行语法错误的风险,在意见轰然你的整个脚本:

 :现在让我们看看接下来会发生什么
 回声你好,世界!
 #=> hello.sh:第1行:意外的EOF同时寻找匹配`''

哎呀。

这问题导致引入了真正的注释语法:现在熟悉的。从一切该行的结尾完全shell忽略,所以你可以把你喜欢的东西有没有担心语法的有效性:

 #现在让我们看看接下来会发生什么
 回声你好,世界!
 #=>你好,世界!

但是,你正在寻找一个多行(块)评论,通过介绍那种 / * C或Java的。通常情况下,在shell脚本,你只要把一个中的每一行的前面,这就是我的建议。但无可否认是不是很多行。

您发现该解决方案采用所谓的这里文档 。语法部分-命令<<凡是导致文本的下列行 - 从行的命令,但不包括从下一行后立即任何 - 读取并且以标准输入一些命令。这里的你好,世界的另一种外壳实现,采用了这个特征的优点是:

 猫<< EOF
你好,世界
EOF

如果您替换与我们的朋友,你会发现,它忽略了不仅是它的论点,但同时它的输入。你可以喂任何你想它,它仍然会像它在锡说,什么也不做。

有仍然是一个潜在的问题,在于这里文档的内容进行一些分析和处理:

 猫<< EOF
2 + 2 = $((2 + 2))
EOF
#=> 2 + 2 = 4

这意味着我们又回到了有我们的多行注释里面的语法错误的可能性。 (;<'EOF'例如&LT),但你可以通过引用最终文件的最前哨避免这样的问题。该报价原因在此文档的整个身体就好像它是引号里面来对待 - 没有任何参数扩展或其他任何东西做它。取而代之的是,文本被送到就像它正在从文件中读取,从字面上看,与由壳任何处理的命令。只要行不以终止字符串开头( EOF END 或其他),它可以包含在所有的任何字符:

 猫<<'EOF'
2 + 2 = $((2 + 2))
EOF
#=> 2 + 2 = $((2 + 2))

所以你去 - 报价定点,使用的命令,有效的有一个多行注释。尽管如此,我强烈建议你,而不是只是把在要注释掉每一行的前面。任何像样的code编辑器将令该操作容易 - 即使普通的老式 - 而好处是,没有人读你的code将不得不花精力搞清楚这是怎么回事的东西,是的,毕竟,拟以文档为他们的利益。

I found a great answer for how to comment in bash script (by @sunny256):

#!/bin/bash
echo before comment
: <<'END'
bla bla
blurfl
END
echo after comment

The ' and ' around the END delimiter are important, otherwise things inside the block like for example $(command) will be parsed and executed.

This may be ugly, but it works and I'm keen to know what it means. Can anybody explain it simply? I did already find an explanation for : that it is no-op or true. But it does not make sense to me to call no-op or true anyway....

解决方案

The goal of a comment is to be text that is not interpreted or executed as code.

Originally, the UNIX shell did not have a comment syntax per se. It did, however, have the do-nothing command :, which ignored its arguments and returned true. So you could put a line like this in your script:

: This is a comment

The shell would run the command, but nothing would happen. Mission accomplished, right?

The problem is that the line is still treated as a command. Lexical analysis, parameter substitution, word splitting, and such still take place on those destined-to-be-ignored arguments. That processing means you run the risk of a syntax error in a "comment" crashing your whole script:

 : Now let's see what happens next
 echo "Hello, world!"
 #=> hello.sh: line 1: unexpected EOF while looking for matching `''

Oops.

That problem led to the introduction of a genuine comment syntax: the now-familiar #. Everything from # to the end of the line is completely ignored by the shell, so you can put anything you like there without worrying about syntactic validity:

 # Now let's see what happens next
 echo "Hello, world!"
 #=> Hello, world!

However, you were looking for a multi-line (block) comment, of the sort introduced by /* in C or Java. Normally, in shell scripts, you just put a # in front of each line, and that is my recommendation. But it is admittedly not very "multi-line".

The solution you found uses what is called a here-document. The syntax some-command <<whatever causes the following lines of text - from the line immediately after the command up to but not including the next line starting with whatever - to be read and fed as standard input to some-command. Here's an alternate shell implementation of "Hello, world" that takes advantage of this feature:

cat <<EOF
Hello, world
EOF

If you replace cat with our friend :, you'll find that it ignores not only its arguments but also its input. You can feed whatever you want to it and it will still, like it says on the tin, do nothing.

There's still a potential problem, in that the contents of a here-document undergo some parsing and processing:

cat <<EOF
2 + 2 = $(( 2 + 2 ))
EOF
#=> 2 + 2 = 4

That means we're back to having the potential for syntax errors inside our "multi-line comment". But you can avoid that problem by quoting the end-of-document sentinel (e.g. <<'EOF'). The quoting causes the entire body of the here-document to be treated as if it were inside the quotes - no parameter expansion or anything else is done to it. Instead, the text is fed to the command just as if it were being read from a file, literally, with no processing by the shell. As long as a line doesn't start with the terminator string (EOF or END or whatever), it can contain any characters at all:

cat <<'EOF'
2 + 2 = $(( 2 + 2 ))
EOF
#=> 2 + 2 = $(( 2 + 2 ))

So there you go - quote the sentinel, use : as the command, and you effectively have a multi-line comment. Nonetheless, I strongly recommend that you instead just put a # at the front of each line you want to comment out. Any decent code editor will make that operation easy - even plain old vi - and the benefit is that nobody reading your code will have to spend energy figuring out what's going on with something that is, after all, intended to be documentation for their benefit.

这篇关于如何:其中;&LT;“END”在bash工作创造一个多行注释块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆