在BASH脚本中进行注释

我怎样才能从脚本中对以下各行进行评论:

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} 

如果我尝试添加一个评论,说“ 猫$ {MYSQLDUMP} | \#输出MYSQLDUMP文件

我得到“# 删除:找不到 ”? 是否可以在这里发表评论,因为“| \”

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

 echo abc `#put your comment here` \ def `#another chance for a comment` \ xyz etc 

对于pipe道来说,有一个干净的解决scheme,没有开销

 echo abc | # normal comment OK here tr az AZ | # another normal comment OK here sort | # the pipelines are automatically continued uniq # final comment 

如何为多线命令放置线条评论

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

你应该可以在你的命令之间加注释行

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

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

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