Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Line comments for multiline command.

13 views
Skip to first unread message

Hongyi Zhao

unread,
Jun 25, 2017, 7:03:56 PM6/25/17
to
Hi all,

In the following case:

mycommand blabla \ # This is the comment for the 1st line
blabla \ # This is the comment for the 2nd line
blabla # This is the comment for the 3nd line

It seems is difficult for using comments.

I have looked the following discussion:

https://stackoverflow.com/questions/9522631/how-to-put-line-comment-for-a-
multi-line-command

But there is still not a good solution for this issue.

Does we may have a more flexible / graceful solution for this issue?

Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

w...@derp.master

unread,
Jun 25, 2017, 10:41:22 PM6/25/17
to

>In the following case:
>
>mycommand blabla \ # This is the comment for the 1st line
> blabla \ # This is the comment for the 2nd line
> blabla # This is the comment for the 3nd line
>
>It seems is difficult for using comments.

Actually, what you have may well work if 'mycommand' supports '# comments'
as arguments, though you'd likely need to escape the hash characters.

In general though what you're trying to do IMO is bad form. I'd suggest
breaking your command strings down and using descriptive variables if you
need more code clarity.

Kaz Kylheku

unread,
Jun 25, 2017, 10:56:39 PM6/25/17
to
On 2017-06-25, Hongyi Zhao <hongy...@gmail.com> wrote:
> Hi all,
>
> In the following case:
>
> mycommand blabla \ # This is the comment for the 1st line
> blabla \ # This is the comment for the 2nd line
> blabla # This is the comment for the 3nd line

You, or someone using the same name as you, asked almost exactly the
same question in 2015.

There was a useful discussion; it might be useful to re-read it.

Here is the Google Groups archive link:

https://groups.google.com/d/msg/comp.unix.shell/jU7d9QBhyws/dOtt70b5mggJ

Your question then was about commenting *out* the arguments entirely;
but it is closely related, and the solutions can be used or adapted
to the above requirement for documenting.

Lew Pitcher

unread,
Jun 26, 2017, 12:42:19 PM6/26/17
to
w...@derp.master wrote:

>
>>In the following case:
>>
>>mycommand blabla \ # This is the comment for the 1st line
>> blabla \ # This is the comment for the 2nd line
>> blabla # This is the comment for the 3nd line
>>
>>It seems is difficult for using comments.
>
> Actually, what you have may well work if 'mycommand' supports '# comments'
> as arguments, though you'd likely need to escape the hash characters.

But, even with that, the commandline as given likely wouldn't work as there
are no continuation lines. The line continuation sentinal is defined as a
sequence of two characters; the backslash character followed by the newline
character. The OP's post (even with your caveat) shows lines that do not end
with the continuation sentinal.

> In general though what you're trying to do IMO is bad form.

Agreed

> I'd suggest
> breaking your command strings down and using descriptive variables if you
> need more code clarity.

Agreed.

--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request


gerg

unread,
Jun 26, 2017, 5:41:53 PM6/26/17
to
Hongyi Zhao <hongy...@gmail.com> wrote:
>
>But there is still not a good solution for this issue.
>
>Does we may have a more flexible / graceful solution for this issue?
>

I don't think there is a graceful solution for this in the bash/ksh script
language. All computer languages have compromises. In other words, each
language will do many things well, and some things poorly.

There are a number of things shell scripts do poorly, and multi-line comments
mixed together with multi-line commands, are one of these things.

I either add a line comment on each line (as your example) or a multi-line
comment just above the multi-line command it explains.

-Greg
--
::::::::::::: Greg Andrews ::::: ge...@panix.com :::::::::::::
I have a map of the United States that's actual size.
-- Steven Wright

Lew Pitcher

unread,
Jun 26, 2017, 6:23:17 PM6/26/17
to
gerg wrote:

> Hongyi Zhao <hongy...@gmail.com> wrote:
>>
>>But there is still not a good solution for this issue.
>>
>>Does we may have a more flexible / graceful solution for this issue?
>>
>
> I don't think there is a graceful solution for this in the bash/ksh script
> language. All computer languages have compromises. In other words, each
> language will do many things well, and some things poorly.
>
> There are a number of things shell scripts do poorly, and multi-line
> comments mixed together with multi-line commands, are one of these things.
>
> I either add a line comment on each line (as your example)

OK, but how? the OP's question is exactly that.

Note that the OP's solution of placing a shell comment immediately after
each line cannot work. As presented, it
1) prevents the line continuation from working (a continuation is explicitly
a line that ends with a backslash followed by a newline), and
2) presents the invoked command with one (or more) additional arguments
(consisting of the octothorpe and the individual "words" that follow it).

For example:

18:22 $ cat comments.sh
# echo a single command line
# expected output: This is a single line
echo This is a single line

# echo a single command line split across two input lines
# expected output: This is a split line that should echo as a single line
echo This is a split line that should \
echo as a single line

# echo a single command line, with comments as per the OP
# expected output: This is a line that should echo on the next line
echo This is a line that should \ # NOT, with a comment
echo on the next line

18:22 $ sh comments.sh
This is a single line
This is a split line that should echo as a single line
This is a line that should # NOT, with a comment
on the next line


> or a multi-line comment just above the multi-line command it explains
> -Greg

Kaz Kylheku

unread,
Jun 26, 2017, 10:03:48 PM6/26/17
to
On 2017-06-26, gerg <ge...@panix.com> wrote:
> Hongyi Zhao <hongy...@gmail.com> wrote:
>>
>>But there is still not a good solution for this issue.
>>
>>Does we may have a more flexible / graceful solution for this issue?
>>
>
> I don't think there is a graceful solution for this in the bash/ksh script
> language. All computer languages have compromises. In other words, each
> language will do many things well, and some things poorly.

When Hongyi asked a very similar question in 2015, there were some
good suggestions. I made a suggestion involving command substitution,
and Janis immediately improvded on it with the idea of using the shell
: (colon) null command.

The question was about commenting out arguments entirely, but of course
the solution works for adding comments also:

$ echo \
lorem $(: ipsum) \
dolor sit $(: consectetur adispicing) \
elit
lorem dolor sit elit

Hong Yi liked this this solution; it's all in the archives!
(I gave a link elsethread.)

It may be considered an idiom: $(: words ... ) serves as a comment.
Just avoid any tokens that have an effect, like (unescaped)
redirection symbols < > .

Hongyi Zhao

unread,
Jun 26, 2017, 10:26:33 PM6/26/17
to
On Tue, 27 Jun 2017 02:03:42 +0000, Kaz Kylheku wrote:

> It may be considered an idiom: $(: words ... ) serves as a comment.
> Just avoid any tokens that have an effect, like (unescaped) redirection
> symbols < > .

If I want to use tokens characters in the comments with the above method,
i.e., that must be escaped, say, <, >, (, ), `, ', and so on.

May the above solution can be revised as following:

$ echo \
lorem $(: 'ipsum < )') \
dolor sit $(: 'consectetur adispicing ` '\''') \
elit
lorem dolor sit elit

0 new messages