评论Bash脚本

问题描述 投票:139回答:6

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

   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}

如果我尝试添加评论,例如:

cat ${MYSQLDUMP} | \ # Output MYSQLDUMP File

我明白了:

#: not found

可以在这里发表评论吗?

bash syntax comments
6个回答
178
投票

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

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

请参阅Stack Overflow问题How to Put Line Comment for a Multi-line Command


37
投票

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

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

# 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}

5
投票

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


5
投票

正如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}

4
投票

$IFS comment hacks

这个hack在parameter expansion上使用$IFS,它用于分隔命令中的单词:

$ echo foo${IFS}bar
foo bar

同理:

$ echo foo${IFS#comment}bar
foo bar

使用它,您可以在带有contination的命令行上发表评论:

$ echo foo${IFS# Comment here} \
> bar
foo bar

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

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

$ 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

罕见的例外

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

$ IFS=x
$ echo foo${IFS#y}bar
foo bar
$ echo foo${IFS#x}bar
foobar

请注意,最终的foobar没有空格,说明了问题。

由于$IFS默认只包含空格,因此您不太可能遇到此问题。


感谢@pjh's comment引发了这个答案。


0
投票

这是一个bash脚本,它结合了之前几条评论的想法和习惯用法,提供了例子,内联注释的一般形式为${__+ <comment text>}

特别是

  • <comment text>可以是多线的
  • <comment text>不是参数扩展的
  • 没有产生子进程(因此注释有效)

<comment text>有一个限制,即不平衡的大括号'}'和括号')'必须受到保护(即'\}''\)')。

本地bash环境有一个要求:

  • 必须取消设置参数名称__

任何其他语法上有效的bash参数名称将代替__,前提是该名称没有设置值。

下面是一个示例脚本

# provide bash inline comments having the form
#     <code> ${__+ <comment>} <code> 
#     <code> ${__+ <multiline
#                   comment>} <code>

# utility routines that obviate "useless use of cat"
function bashcat { printf '%s\n' "$(</dev/stdin)"; }
function scat { 1>&2 bashcat; exit 1; }

# ensure that '__' is unset && remains unset
[[ -z ${__+x} ]] &&  # if '__' is unset
  declare -r __ ||   # then ensure that '__' remains unset 
  scat <<EOF         # else exit with an error
Error: the parameter __='${__}' is set, hence the
  comment-idiom '\${__+ <comment text>}' will fail
EOF

${__+ (example of inline comments)
------------------------------------------------
the following inline comment-idiom is supported
    <code> ${__+ <comment>} <code> 
    <code> ${__+ <multiline
                  comment>} <code> 
(advisory) the parameter '__' must NOT be set;
  even the null declaration __='' will fail
(advisory) protect unbalanced delimiters \} and \) 
(advisory) NO parameter-expansion of <comment> 
(advisory) NO subprocesses are spawned
(advisory) a functionally equivalent idiom is 
    <code> `# <comment>` <code> 
    <code> `# <multiline
               comment>` <code>
however each comment spawns a bash subprocess
that inelegantly requires ~1ms of computation 
------------------------------------------------}
© www.soinside.com 2019 - 2024. All rights reserved.