评论Bash脚本

Bas*_*ozz 152 syntax bash comments

如何从脚本中评论以下几行的每一行?

cat ${MYSQLDUMP} | \
sed '1d' | \
tr ",;" "\n" | \
sed -e 's/[asbi]:[0-9]*[:]*//g' -e '/^[{}]/d' -e 's/""//g' -e '/^"{/d' | \
sed -n -e '/^"/p' -e '/^print_value$/,/^option_id$/p' | \
sed -e '/^option_id/d' -e '/^print_value/d' -e 's/^"\(.*\)"$/\1/' | \
tr "\n" "," | \
sed -e 's/,\([0-9]*-[0-9]*-[0-9]*\)/\n\1/g' -e 's/,$//' | \
sed -e 's/^/"/g' -e 's/$/"/g' -e 's/,/","/g' >> ${CSV}
Run Code Online (Sandbox Code Playgroud)

如果我尝试添加评论说" cat $ {MYSQLDUMP} |\#Output MYSQLDUMP File ",我得到:

删除:未找到

是否有可能在这里发表评论,因为" __CODE__"?

Dig*_*oss 189

这将有一些开销,但从技术上讲它确实回答了你的问题:

echo abc `#Put your comment here` \
     def `#Another chance for a comment` \
     xyz, etc.
Run Code Online (Sandbox Code Playgroud)

特别是对于管道,有一个没有开销的清洁解决方案:

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
Run Code Online (Sandbox Code Playgroud)

请参阅堆栈溢出问题如何为多行命令添加行注释.

  • 看起来比较复杂,有没有更简单的方法呢? (2认同)

mob*_*mob 39

尾部反斜杠必须是该行的最后一个字符,以便将其解释为延续命令.之后不允许任何评论甚至空格.

您应该能够在命令之间添加注释行

# output MYSQLDUMP file
cat ${MYSQLDUMP} | \
# simplify the line
sed '/created_at/d' | \
# create some newlines
tr ",;" "\n" | \
# use some sed magic
sed -e 's/[asbi]:[0-9]*[:]*//g' -e '/^[{}]/d' -e 's/""//g' -e '/^"{/d' | \
# more magic
sed -n -e '/^"/p' -e '/^print_value$/,/^option_id$/p' | \
# even more magic
sed -e '/^option_id/d' -e '/^print_value/d' -e 's/^"\(.*\)"$/\1/' | \
tr "\n" "," | \
# I hate phone numbers in my output
sed -e 's/,\([0-9]*-[0-9]*-[0-9]*\)/\n\1/g' -e 's/,$//' | \ 
# one more sed call and then send it to the CSV file
sed -e 's/^/"/g' -e 's/$/"/g' -e 's/,/","/g' >> ${CSV}
Run Code Online (Sandbox Code Playgroud)

  • 当管道命令组件以|结尾时,不需要\ (11认同)
  • "你应该能够在你的命令之间添加注释行":不,这只是起作用,因为前一行的最后一个解释字符是`|`.如果你试试`cat file1\<newline> #comment <newline> file2`,你会发现你没有得到`cat file1 file2`,而是`cat file1; file2`. (4认同)
  • 但是,正如其他人提到的那样,`cat file1 | #comment <newline> sort`工作正常.`cat file1 &&#comment <newline> echo foo`也是如此.所以注释可以包含在`|`或`&&`或`||`之后,但不能在`\\`之后或在命令的中间. (4认同)
  • DigitalRoss,你是对的,我可以使用管道,而不是反斜杠,然后我的#comments将完美地运作...你可以发布它作为答案,所以我可以接受它. (2认同)

mob*_*mob 6

正如DigitalRoss所指出的那样,当线路结束时,不需要尾随反斜杠|.你可以在以下的一行上添加注释|:

 cat ${MYSQLDUMP} |         # Output MYSQLDUMP file
 sed '1d' |                 # skip the top line
 tr ",;" "\n" | 
 sed -e 's/[asbi]:[0-9]*[:]*//g' -e '/^[{}]/d' -e 's/""//g' -e '/^"{/d' |
 sed -n -e '/^"/p' -e '/^print_value$/,/^option_id$/p' |
 sed -e '/^option_id/d' -e '/^print_value/d' -e 's/^"\(.*\)"$/\1/' |
 tr "\n" "," |
 sed -e 's/,\([0-9]*-[0-9]*-[0-9]*\)/\n\1/g' -e 's/,$//' |   # hate phone numbers
 sed -e 's/^/"/g' -e 's/$/"/g' -e 's/,/","/g' >> ${CSV}
Run Code Online (Sandbox Code Playgroud)


wis*_*cky 6

除了 DigitalRoss 的示例之外,如果您愿意,还可以使用以下另一种形式$()来代替反引号`

echo abc $(: comment) \
     def $(: comment) \
     xyz
Run Code Online (Sandbox Code Playgroud)

当然,您也可以使用带反引号的冒号语法:

echo abc `: comment` \
     def `: comment` \
     xyz
Run Code Online (Sandbox Code Playgroud)

补充笔记

$(#comment)不起作用的原因是因为一旦它看到#,它就会将该行的其余部分视为注释,包括右括号:comment)。所以括号永远不会关闭。

反引号的解析方式不同,即使在#.

  • 这会为每条评论创建一个新的 shell 吗? (4认同)

tob*_*svl 5

反斜杠转义#,将其解释为文字字符而不是注释字符。


Tom*_*ale 5

$IFS 评论黑客

这个 hack 使用参数扩展on $IFS,用于分隔命令中的单词:

$ echo foo${IFS}bar
foo bar
Run Code Online (Sandbox Code Playgroud)

相似地:

$ echo foo${IFS#comment}bar
foo bar
Run Code Online (Sandbox Code Playgroud)

使用它,您可以在命令行上添加注释:

$ echo foo${IFS# Comment here} \
> bar
foo bar
Run Code Online (Sandbox Code Playgroud)

但评论需要在\继续之前。

请注意,参数扩展是在注释中执行的:

$ ls file
ls: cannot access 'file': No such file or directory
$ echo foo${IFS# This command will create file: $(touch file)}bar
foo bar
$ ls file
file
Run Code Online (Sandbox Code Playgroud)

罕见异常

失败的唯一罕见情况是,如果$IFS之前以通过扩展删除的确切文本开头(即,在#字符之后):

$ IFS=x
$ echo foo${IFS#y}bar
foo bar
$ echo foo${IFS#x}bar
foobar
Run Code Online (Sandbox Code Playgroud)

注意 finalfoobar没有空格,说明了这个问题。

由于$IFS默认情况下仅包含空格,因此您不可能遇到此问题。


感谢@ PJH的评论这引发了这样的回答。