Bash 的内联注释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2524367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inline comments for Bash?
提问by Lajos Nagy
I'd like to be able to comment out a single flag in a one-line command. Bash only seems to have from # till end-of-line
comments. I'm looking at tricks like:
我希望能够在一行命令中注释掉单个标志。Bash 似乎只有from # till end-of-line
评论。我正在研究以下技巧:
ls -l $([ ] && -F is turned off) -a /etc
It's ugly, but better than nothing. Is there a better way?
这很丑陋,但总比没有好。有没有更好的办法?
The following seems to work, but I'm not sure whether it is portable:
以下似乎有效,但我不确定它是否可移植:
ls -l `# -F is turned off` -a /etc
回答by Rafareino
My preferred is:
我的首选是:
This will have some overhead, but technically it does answer your question
echo abc `#put your comment here` \ def `#another chance for a comment` \ xyz etc
And for pipelines specifically, there is a cleaner solution with no overhead
echo abc | # normal comment OK here tr a-z A-Z | # another normal comment OK here sort | # the pipelines are automatically continued uniq # final comment
这会有一些开销,但从技术上讲,它确实可以回答您的问题
echo abc `#put your comment here` \ def `#another chance for a comment` \ xyz etc
特别是对于管道,有一个更干净的解决方案,没有开销
echo abc | # normal comment OK here tr a-z A-Z | # another normal comment OK here sort | # the pipelines are automatically continued uniq # final comment
回答by Dan
I find it easiest (and most readable) to just copy the line and comment out the original version:
我发现复制该行并注释掉原始版本最简单(也是最易读的):
#Old version of ls:
#ls -l $([ ] && -F is turned off) -a /etc
ls -l -a /etc
回答by Ignacio Vazquez-Abrams
$(: ...)
is a little less ugly, but still not good.
$(: ...)
少了一点丑陋,但仍然不好。
回答by KylePDavis
Here's my solution for inline comments in between multiple piped commands.
这是我针对多个管道命令之间的内联注释的解决方案。
Example uncommented code:
未注释代码示例:
#!/bin/sh
cat input.txt \
| grep something \
| sort -r
Solution for a pipe comment (using a helper function):
管道注释的解决方案(使用辅助函数):
#!/bin/sh
pipe_comment() {
cat -
}
cat input.txt \
| pipe_comment "filter down to lines that contain the word: something" \
| grep something \
| pipe_comment "reverse sort what is left" \
| sort -r
Or if you prefer, here's the same solution without the helper function, but it's a little messier:
或者,如果您愿意,这里是没有帮助函数的相同解决方案,但它有点混乱:
#!/bin/sh
cat input.txt \
| cat - `: filter down to lines that contain the word: something` \
| grep something \
| cat - `: reverse sort what is left` \
| sort -r
回答by Karoly Horvath
How about storing it in a variable?
将它存储在变量中怎么样?
#extraargs=-F
ls -l $extraargs -a /etc
回答by leedm777
Most commands allow args to come in any order. Just move the commented flags to the end of the line:
大多数命令允许 args 以任何顺序出现。只需将注释标志移动到行尾:
ls -l -a /etc # -F is turned off
Then to turn it back on, just uncomment and remove the text:
然后重新打开它,只需取消注释并删除文本:
ls -l -a /etc -F
回答by Steven Penny
If you know a variable is empty, you could use it as a comment. Of course if it is not empty it will mess up your command.
如果您知道变量为空,则可以将其用作注释。当然,如果它不是空的,它会弄乱你的命令。
ls -l ${1# -F is turned off} -a /etc
回答by Ondra ?i?ka
For disabling a part of a command like a && b
, I simply created an empty script x
which is on path, so I can do things like:
为了禁用像这样的命令的一部分a && b
,我只是x
在路径上创建了一个空脚本,因此我可以执行以下操作:
mvn install && runProject
when I need to build, and
当我需要构建时,以及
x mvn install && runProject
when not (using Ctrl + Aand Ctrl + Eto move to the beginning and end).
当没有时(使用Ctrl + A和Ctrl + E移动到开头和结尾)。
As noted in comments, another way to do that is Bash built-in :
instead of x
:
如评论中所述,另一种方法是内置 Bash:
而不是x
:
$ : Hello world, how are you? && echo "Fine."
Fine.
回答by Jonathan Leffler
If the comment is worth making, it probably can go at the end of the line, or on a line on its own. I seldom find a need for within-line comments with code before and after the comment in any language.
如果评论值得发表,它可能可以放在行尾,或者单独一行。我很少发现需要在任何语言的注释前后使用代码进行行内注释。
Oh, there's one exception, which is the dialect of SQL I usually use which uses '{comments}'. Occasionally, I will write:
哦,有一个例外,它是我通常使用的 SQL 方言,它使用“{comments}”。偶尔,我会写:
CREATE UNIQUE INDEX u1_table ON Table(...);
CREATE {DUPS} INDEX d1_table ON Table(...);
But even that is a stretch.
但即使是这样也有点牵强。