bash 如何为多行命令添加行注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9522631/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:43:43  来源:igfitidea点击:

How to put a line comment for a multi-line command

bashshellcomments

提问by Peter Lee

I know how to write a multi-line command in a Bash script, but how can I add a comment for each line in a multiline command?

我知道如何在 Bash 脚本中编写多行命令,但是如何为多行命令中的每一行添加注释?

CommandName InputFiles      \ # This is the comment for the 1st line
            --option1 arg1  \ # This is the comment for the 2nd line
            --option2 arg2    # This is the comment for the 3nd line

But unfortunately, the comment after continuation character \will break the command.

但不幸的是,连续字符后的注释\会破坏命令。

采纳答案by Perry

I'm afraid that, in general, you can't do what you're asking for. The best you can do is a comment on the lines before the command, or one single comment at the end of the command line, or a comment after the command.

恐怕一般来说,你不能做你所要求的。您能做的最好的事情是在命令之前的行中添加注释,或者在命令行末尾添加一个注释,或者在命令之后添加注释。

You can't manage to intersperse comments inside a command this way. The \s express an intent to merge lines, so for all intents and purposes you're trying to intersperse comments in a single line, which doesn't work anyway because a \has to be at the end of the line to have that effect.

您无法以这种方式在命令中散布注释。在\小号表达的意图,合并线路,所以对于所有意图和目的你想在一个单一的线点缀的意见,不反正工作,因为\必须在该行的末尾有这种效果。

回答by Marwan Alsabbagh

This is how I do it. Essentially by using Bash's backtick command substitutionone can place these comments anywhere along a long command line even if it is split across lines. I have put the echo command in front of your example so that you can execute the example and see how it works:

我就是这样做的。本质上,通过使用 Bash 的反引号命令替换,即使跨行拆分,也可以将这些注释放置在长命令行的任何位置。我已将 echo 命令放在您的示例前面,以便您可以执行该示例并查看它是如何工作的:

echo CommandName InputFiles `#1st comment` \
             --option1 arg1 `#2nd comment` \
             --option2 arg2 `#3rd comment`

Another example where you can put multiple comments at different points on one line:

另一个示例,您可以在一行的不同点放置多个注释:

some_cmd --opt1 `#1st comment` --opt2 `#2nd comment` --opt3 `#3rd comment`

回答by Philipp

You could store the arguments in an array:

您可以将参数存储在数组中:

args=(InputFiles      # This is the comment for the 1st line
      # You can have whole lines of comments in between, useful for:
      #--deprecated-option # This isn't use any more
      --option1 arg1  # This is the comment for the 2nd line

      # And even blank lines in between for readability
      --option2 arg2  # This is the comment for the 3nd line
     )
CommandName "${args[@]}"

However I think this looks a bit hackish if it is only for the purpose of allowing comments for each argument. Therefore I'd just rewrite the comment so that it refers the the individual arguments, and put it above the whole command.

但是,如果只是为了允许对每个论点进行评论,我认为这看起来有点骇人听闻。因此,我只是重写注释,以便它引用各个参数,并将其放在整个命令之上。

回答by chepner

Based on pjh's comment to another answer to this question, replacing IFSwith a variable known to contain no non-whitespace characters.

基于 pjh 对此问题的另一个答案的评论,替换IFS为已知不包含非空白字符的变量。

comment=
who ${comment# This is the command} \
    -u ${comment# This is the argument}