我最近开始研究shell脚本,我希望能够在shell脚本中注释出一组行。我的意思是,就像在C/Java的情况下:
/* comment1
comment2
comment3
*/`
我怎么能那样做呢?
我最近开始研究shell脚本,我希望能够在shell脚本中注释出一组行。我的意思是,就像在C/Java的情况下:
/* comment1
comment2
comment3
*/`
我怎么能那样做呢?
当前回答
下面是我如何在bash中执行多行注释。
我很欣赏这种机制的两个优点。一是注释可以嵌套。另一种是可以通过简单地注释掉初始行来启用块。
#!/bin/bash
# : <<'####.block.A'
echo "foo {" 1>&2
fn data1
echo "foo }" 1>&2
: <<'####.block.B'
fn data2 || exit
exit 1
####.block.B
echo "can't happen" 1>&2
####.block.A
在上面的例子中,“B”块被注释掉了,但是“A”块中不是“B”块的部分没有被注释掉。
运行该示例将产生以下输出:
foo {
./example: line 5: fn: command not found
foo }
can't happen
其他回答
使用:'打开'和'关闭。
例如:
: '
This is a
very neat comment
in bash
'
在阅读了其他答案后,我想出了以下答案,恕我直言,这是一个评论。特别适合脚本内使用信息:
<< ////
Usage:
This script launches a spaceship to the moon. It's doing so by
leveraging the power of the Fifth Element, AKA Leeloo.
Will only work if you're Bruce Willis or a relative of Milla Jovovich.
////
作为一名程序员,斜杠的序列会立即在我的大脑中注册为注释(尽管斜杠通常用于行注释)。
当然,“////”只是一个字符串;前缀和后缀中的斜杠个数必须相等。
简单的解决方案,不太聪明:
暂时阻塞脚本的一部分:
if false; then
while you respect syntax a bit, please
do write here (almost) whatever you want.
but when you are
done # write
fi
稍微复杂一点的版本:
time_of_debug=false # Let's set this variable at the beginning of a script
if $time_of_debug; then # in a middle of the script
echo I keep this code aside until there is the time of debug!
fi
你对此有什么看法?
function giveitauniquename()
{
so this is a comment
echo "there's no need to further escape apostrophes/etc if you are commenting your code this way"
the drawback is it will be stored in memory as a function as long as your script runs unless you explicitly unset it
only valid-ish bash allowed inside for instance these would not work without the "pound" signs:
1, for #((
2, this #wouldn't work either
function giveitadifferentuniquename()
{
echo nestable
}
}
下面是我如何在bash中执行多行注释。
我很欣赏这种机制的两个优点。一是注释可以嵌套。另一种是可以通过简单地注释掉初始行来启用块。
#!/bin/bash
# : <<'####.block.A'
echo "foo {" 1>&2
fn data1
echo "foo }" 1>&2
: <<'####.block.B'
fn data2 || exit
exit 1
####.block.B
echo "can't happen" 1>&2
####.block.A
在上面的例子中,“B”块被注释掉了,但是“A”块中不是“B”块的部分没有被注释掉。
运行该示例将产生以下输出:
foo {
./example: line 5: fn: command not found
foo }
can't happen