How can I comment on each line of the following lines from a script?
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}
If I try and add a comment say "cat ${MYSQLDUMP} | \ #Output MYSQLDUMP File", I get:
Delete: not found
Is it possible to comment here or not because of "| \
"?
As DigitalRoss pointed out, the trailing backslash is not necessary when the line woud end in
|
. And you can put comments on a line following a|
:The backslash escapes the #, interpreting it as its literal character instead of a comment character.
Here is a bash script that combines the ideas and idioms of several previous comments to provide, with examples, inline comments having the general form
${__+ <comment text>}
.In particular
<comment text>
can be multi-line<comment text>
is not parameter-expandedThere is one restriction on the
<comment text>
, namely, unbalanced braces'}'
and parentheses')'
must be protected (i.e.,'\}'
and'\)'
).There is one requirement on the local bash environment:
__
must be unsetAny other syntactically valid bash parameter-name will serve in place of
__
, provided that the name has no set value.An example script follows
$IFS
comment hacksThis hack uses parameter expansion on
$IFS
, which is used to separate words in commands:Similarly:
Using this, you can put a comment on a command line with contination:
but the comment will need to be before the
\
continuation.Note that parameter expansion is performed inside the comment:
Rare exception
The only rare case this fails is if
$IFS
previously started with the exact text which is removed via the expansion (ie, after the#
character):Note the final
foobar
has no space, illustrating the issue.Since
$IFS
contains only whitespace by default, it's extremely unlikely you'll run into this problem.Credit to @pjh's comment which sparked off this answer.
The trailing backslash must be the last character on the line for it to be interpreted as a continuation command. No comments or even whitespace are allowed after it.
You should be able to put comment lines in between your commands
This will have some overhead, but technically it does answer your question:
And for pipelines specifically, there is a clean solution with no overhead:
See Stack Overflow question How to Put Line Comment for a Multi-line Command.