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)
请参阅堆栈溢出问题如何为多行命令添加行注释.
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)
正如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)
除了 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)。所以括号永远不会关闭。
反引号的解析方式不同,即使在#.
$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的评论这引发了这样的回答。