如何在脚本的以下各行中注释每一行?
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
可以在这里发表评论吗?
这会有一些开销,但是从技术上讲,它确实回答了您的问题:
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
尾部反斜杠必须是该行上的最后一个字符,才能将其解释为连续命令。其后不允许添加注释,甚至不允许使用空格。
您应该能够在命令之间插入注释行
# 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}
反斜杠转义#,将其解释为文字字符而不是注释字符。
$IFS
条评论]此hack在parameter expansion上使用$IFS
,用于分隔命令中的单词:
$ echo foo${IFS}bar
foo bar
类似地:
$ echo foo${IFS#comment}bar
foo bar
使用此功能,您可以在命令行上以连续符添加注释:
$ 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
仅包含空格,因此[[extremely不太可能会遇到此问题。
$()
而不是反引号`
,这是您可以使用的另一种形式>echo abc $(: comment) \
def $(: comment) \
xyz
当然,您也可以在反引号中使用冒号语法:
echo abc `: comment` \
def `: comment` \
xyz
附加说明
$(#comment)
不起作用的原因是,一旦看到#
,它将把该行的其余部分视为注释,包括右括号:comment)
。因此括号永远不会关闭。反引号解析方式不同,即使在#
之后,反引号也会检测到。
${__+ <comment text>}
的嵌入式注释。