是否有一种简单的方法来注释掉shell脚本中的代码块?
当前回答
让我们把这些最好的想法和建议结合起来。
alias _CommentBegin_=": <<'_CommentEnd_'"
如前所述,单引号是非常重要的,因为没有它们 $(commandName)和${varName}将被计算。
你可以这样使用它:
_CommentBegin_
echo "bash code"
or
none code can be in here
_CommentEnd_
别名使使用更加明显和更好看。
其他回答
让我们把这些最好的想法和建议结合起来。
alias _CommentBegin_=": <<'_CommentEnd_'"
如前所述,单引号是非常重要的,因为没有它们 $(commandName)和${varName}将被计算。
你可以这样使用它:
_CommentBegin_
echo "bash code"
or
none code can be in here
_CommentEnd_
别名使使用更加明显和更好看。
以下命令适用于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"
在上面的例子中。
在bash中:
#!/bin/bash
echo before comment
: <<'END'
bla bla
blurfl
END
echo after comment
END分隔符周围的“和”很重要,否则块内的内容(例如$(命令))将被解析并执行。
关于解释,请看这个和这个问题。
老实说,为什么要这么过度设计……
我认为编写主动代码来生成被动代码是一种糟糕的做法。
我的解决方案是:大多数编辑器都有块选择模式。只需使用它添加#到所有你想注释掉的行。 有什么大不了的…
记事本的例子:
创建方法:Alt -鼠标向下拖动,按#。
删除:alt -鼠标向下拖动,右移箭头,删除。
如果你能避开单引号:
__='
blah blah comment.
'