在Shell脚本中阻止注释
有没有简单的方法来注释一个shell脚本中的代码块?
在bash中:
#!/bin/bash echo before comment : <<'END' bla bla blurfl END echo after comment   END分隔符周围的'和'是重要的,否则块内部的东西,比如$(command)将被parsing和执行。 
为了解释,请看这个和这个问题。
shell脚本没有阻止注释。
 使用vi (是, vi ),你可以很容易地从第n行到第m行发表评论 
 <ESC> :10,100s/^/#/ 
(从第10行到第100行,用#号replace行开始(^))。
并取消与评论
 <ESC> :10,100s/^#// 
(从第10行读取到第100行replace行开始(^),然后跟着#注意到//)。
 在任何有/bin/sh地方, vi几乎是通用的。 
您可以使用:
 if [ 1 -eq 0 ]; then echo "The code that you want commented out goes here." echo "This echo statement will not be called." fi 
 以下内容适用于sh , bash , ksh和zsh 。 
 要注释的代码块可以放在BEGINCOMMENT和ENDCOMMENT : 
 [ -z $BASH ] || shopt -s expand_aliases alias BEGINCOMMENT="if [ ]; then" alias ENDCOMMENT="fi" BEGINCOMMENT echo "This line appears in a commented block" echo "And this one too!" ENDCOMMENT echo "This is outside the commented block" 
执行上面的代码将导致:
 This is outside the commented block 
为了取消注释这样评论的代码块,说
 alias BEGINCOMMENT="if : ; then" 
代替
 alias BEGINCOMMENT="if [ ]; then" 
在上面的例子中。
在Vim:
- 去你想评论的第一行
-   shift-V(进入可视化模式),向上向下突出显示块中的行
-  执行以下select:s/^/#/
- 
该命令将如下所示: :'<,'>s/^/#
- 
点击进入 
例如
 shift-V jjj :s/^/# <enter> 
如果你能躲开单引号:
 __=' blah blah comment. ' 
你可以用Vi / Vim的Visual Block模式来devise这样的东西:
 Ctrl-V Highlight first element in rows you want commented Shift-i # esc 
取消注释将是:
 Ctrl-V Highlight #'sdl 
这是vi做这种事情的互动方式,而不是计算或读取行号。
最后,在Gvim中,使用Ctrl-q进入可视块模式而不是Ctrl-V(因为这是粘贴的快捷方式)。
这么多的过度工程…
我认为编写用于生成被动代码的主动代码是一个不好的做法。
我的解决scheme:大多数编辑器都有块select模式。 只要使用它来添加#到你想要注释掉的所有行。 有什么大不了的
记事本的例子:
要创build:Alt – mousedrag,请按#。
要删除:Alt-mousedrag下移,右移箭头,删除。
sunny256接受的答案中的here-doc技巧的变体是使用Perl关键字进行评论。 如果您的注释实际上是某种文档,那么您可以在注释块内部使用Perl语法,从而可以将其打印为格式良好的文本,将其转换为手册页等。
 就shell而言,你只需要用'=cut'代替'END' '=cut' 。 
 echo "before comment" : <<'=cut' =pod =head1 NAME podtest.sh - Example shell script with embedded POD documentation etc. =cut echo "after comment" 
(在“ 在shell脚本中embedded文档 ”中find)