Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何为多行命令放置行注释_Bash_Shell_Comments - Fatal编程技术网

Bash 如何为多行命令放置行注释

Bash 如何为多行命令放置行注释,bash,shell,comments,Bash,Shell,Comments,我知道如何在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 但不幸的

我知道如何在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

但不幸的是,连续字符
\
后的注释将中断命令。

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

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[@]}"

不过,如果只是为了允许对每个参数进行注释,我认为这看起来有点不太妥当。因此,我只需重写注释,使其引用单个参数,并将其置于整个命令之上。

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

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[@]}"

不过,如果只是为了允许对每个参数进行注释,我认为这看起来有点不太妥当。因此,我只需重写注释,使其引用单个参数,并将其置于整个命令之上。

我担心,一般来说,您无法满足您的要求。最好是在命令前的行上添加注释,或在命令行末尾添加一条注释,或在命令后添加注释


您无法以这种方式在命令中散布注释。
\
表达了合并行的意图,因此无论出于何种意图和目的,您都试图在一行中插入注释,但这无论如何都不起作用,因为
\
必须位于行的末尾才能产生这种效果。

我担心,通常情况下,您无法满足自己的要求。最好是在命令前的行上添加注释,或在命令行末尾添加一条注释,或在命令后添加注释


您无法以这种方式在命令中散布注释。
\
表达了合并行的意图,因此出于所有意图和目的,您都试图将注释散布在一行中,但这无论如何都不起作用,因为
\
必须位于行的末尾才能产生这种效果。

我就是这样做的。基本上,通过使用Bash的backtick,可以将这些注释放置在长命令行的任意位置,即使它是跨行分割的。我已将echo命令放在您的示例前面,以便您可以执行示例并了解其工作原理:

echo CommandName InputFiles `#1st comment` \
             --option1 arg1 `#2nd comment` \
             --option2 arg2 `#3rd comment`
另一个示例,您可以在一行的不同位置放置多个注释:

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

我就是这样做的。基本上,通过使用Bash的backtick,可以将这些注释放置在长命令行的任意位置,即使它是跨行分割的。我已将echo命令放在您的示例前面,以便您可以执行示例并了解其工作原理:

echo CommandName InputFiles `#1st comment` \
             --option1 arg1 `#2nd comment` \
             --option2 arg2 `#3rd comment`
另一个示例,您可以在一行的不同位置放置多个注释:

some_cmd --opt1 `#1st comment` --opt2 `#2nd comment` --opt3 `#3rd comment`
根据pjh对的注释,将
IFS
替换为已知不包含非空白字符的变量

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

为什么不引用参数展开式?使用空字符串初始化变量。当参数展开时,
#
操作符(与shell注释字符
#
无关,但用于相似性)尝试从参数值中去除实际注释。当然,结果仍然是一个空字符串

未加引号的参数展开将进行分词和路径名生成。在这种情况下,两个进程都不会从空字符串中创建任何其他单词,因此结果仍然是空字符串。这样的空字符串被简单地丢弃,而不影响它出现的命令。上述情况正好相当于

who \
  -u
根据pjh对的注释,将
IFS
替换为已知不包含非空白字符的变量

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

为什么不引用参数展开式?使用空字符串初始化变量。当参数展开时,
#
操作符(与shell注释字符
#
无关,但用于相似性)尝试从参数值中去除实际注释。当然,结果仍然是一个空字符串

未加引号的参数展开将进行分词和路径名生成。在这种情况下,两个进程都不会从空字符串中创建任何其他单词,因此结果仍然是空字符串。这样的空字符串被简单地丢弃,而不影响它出现的命令。上述情况正好相当于

who \
  -u

这对OP的例子这样简单的东西有效,但它不支持
@Philipp-Hmmm,谢谢。这是一个很好的解决办法。但是,如果我的
--选项arg
同时包含
'
@PeterLee:你也可以在数组中使用
'
。不那么麻烦的是只在数组中存储参数,然后像这样使用它们:
CommandName“${args[@}”
。如果希望能够注释掉参数列表中的整行,则此格式优于所有其他格式<代码>命令“${args[@]}”
ftw。这适用于一些简单的操作,比如OP的示例,但是它不支持
@Philipp-Hmmm,谢谢。这是一个很好的解决办法。但是,如果我的
--选项arg
同时包含
'
@PeterLee:你也可以在数组中使用
'
。不那么麻烦的是只在数组中存储参数,然后像这样使用它们:
CommandName“${args[@}”
。如果希望能够注释掉参数列表中的整行,则此格式优于所有其他格式<代码>命令“${args[@]}”
ftw。它甚至可以在管道子命令中工作:“echo`#1`foo\(换行符)| perl-ne`#2`'print'。。。正是我需要的!这是我见过的最巧妙的替代滥用!这种技术为每个内联注释创建一个子shell,因此这些注释非常昂贵。它们只适用于不经常执行的行