bash脚本添加头注释_如何在Bash脚本中为多行命令添加行内注释

bash脚本添加头注释

In Bash script, it is common that multiple small commands run together connected by pipes (|) and the whole command is quite long. For clarity, we may write the command in multiple lines. How to add comments for these long multi-line commands? In Bash, the content after # in a line is the comment. Using a single comment line before the multi-line command is not convenient as the command may be quite complex. The best way is still inline comments as we do in many other programming languages. In this post, we discuss 2 techniques used in 2 different scenarios.

在Bash 脚本中 ,通常有多个小命令通过管道( | )连接在一起运行,并且整个命令很长。 为了清楚起见,我们可以多行编写命令。 如何为这些长的多行命令添加注释? 在Bash中,一行中#后面的内容是注释。 在多行命令之前使用单个注释行并不方便,因为该命令可能非常复杂。 最好的方法仍然是内联注释,就像我们在许多其他编程语言中所做的一样。 在本文中,我们讨论了在两种不同情况下使用的两种技术。

多行管道连接命令的内联注释 (Inline comments for multi-line pipe-connected commands)

For pipe-connected commands, we use the interesting feature of bash that the pipe | connects 2 subshells and the subshells can have empty lines before it. So we can add a comment after the | and a new line then the next command. One example is as follows.

对于管道连接的命令,我们使用bash的有趣功能,即| 连接2个子Shell,子Shell之前可以有空行。 因此,我们可以在|后添加评论| 和新行,然后是下一个命令。 一个例子如下。

echo -e "Aabcbb\nAabcbD\nAabcbb" |  # generate the content
     tr a-z A-Z |                   # translate to upper case
     sort |                         # sort the text
     uniq                           # remove duplicated lines

Grammatically, the commend at the end of each line after | is actually part of the next command. But it is natural for readers of the script to consider the comment to describe the command before the |. Cool, right?

在语法上,在| 实际上是下一个命令的一部分。 但是,对于脚本的读者来说,在|之前考虑注释来描述命令是很自然的| 。 酷吧?

用\ <newline>分隔的多行命令的行内注释 (Inline comments for multi-line commands separated by \<newline>)

Now, we don’t have the nice grammar help from the |. We need the techniques making use of the command substitution feature. The command of the subshell can include comment too and the comment ends at the end of the subshell command. So, a command as follows is valid and the comment in the substitution command can be considered as the "inline comment" for that line.

现在,我们没有|语法帮助| 。 我们需要使用命令替换功能的技术。 子shell的命令也可以包含注释,并且注释在subshel​​l命令的结尾处结束。 因此,以下命令是有效的,并且替换命令中的注释可以视为该行的“行内注释”。

perl -0777 `# whole input as a block` \
 -p `# loop andprint line also` \
 -e 's|<PRE>[\s{<BR>}{<HR>}]*</PRE>||g' 

But note that this technique is expensive because it creates a subshell for each of such "inline comments" during execution. It is only suitable if the commands performance or cost is not a problem.

但是请注意,此技术非常昂贵,因为它在执行过程中为每个此类“内联注释”创建了一个子外壳。 仅在命令性能或成本没有问题的情况下才适用。

翻译自: https://www.systutorials.com/how-to-add-inline-comments-for-multi-line-command-in-bash-script/

bash脚本添加头注释

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值