Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

In space, no one can hear you fart.


devel / comp.unix.shell / Use comments on the next line of the line continuation symbol (\).

SubjectAuthor
* Use comments on the next line of the line continuation symbol (\).hongy...@gmail.com
+- Re: Use comments on the next line of the line continuation symbol (\).Kenny McCormack
+* Re: Use comments on the next line of the line continuation symbol (\).Dan Espen
|+* Re: Use comments on the next line of the line continuation symbol (\).Keith Thompson
||`- Re: Use comments on the next line of the line continuation symbol (\).Dan Espen
|`* Re:Helmut Waitzmann
| `* Re: Use comments on the next line of the line continuation symbol (\).Dan Espen
|  `* Re: Use comments on the next line of the line continuation symbol (\).hongy...@gmail.com
|   +- Re: Use comments on the next line of the line continuation symbol (\).hongy...@gmail.com
|   `* Re:Helmut Waitzmann
|    `* Re: Use comments on the next line of the line continuation symbol (\).hongy...@gmail.com
|     `* Re:Helmut Waitzmann
|      `* Re: Use comments on the next line of the line continuation symbol (\).hongy...@gmail.com
|       `* How to simulate array variables in the POSIX shHelmut Waitzmann
|        `* Re: How to simulate array variables in the POSIX shell (was: Usehongy...@gmail.com
|         `- Re: How to simulate array variables in the POSIX shellJanis Papanagnou
+- Re:Helmut Waitzmann
+* Re: Use comments on the next line of the line continuation symbolTavis Ormandy
|`- Re: Use comments on the next line of the line continuation symbolJanis Papanagnou
`- Re: Use comments on the next line of the line continuation symbolhymie!

1
Use comments on the next line of the line continuation symbol (\).

<8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4808&group=comp.unix.shell#4808

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:6214:20ec:: with SMTP id 12mr8786656qvk.0.1640442496887;
Sat, 25 Dec 2021 06:28:16 -0800 (PST)
X-Received: by 2002:a37:5a43:: with SMTP id o64mr7441428qkb.375.1640442496746;
Sat, 25 Dec 2021 06:28:16 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sat, 25 Dec 2021 06:28:16 -0800 (PST)
Injection-Info: google-groups.googlegroups.com; posting-host=172.104.91.163; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 172.104.91.163
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
Subject: Use comments on the next line of the line continuation symbol (\).
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Sat, 25 Dec 2021 14:28:16 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 12
 by: hongy...@gmail.com - Sat, 25 Dec 2021 14:28 UTC

See the following example bash script:

```
#!/usr/bin/env bash

sudo apt install -y pkg_1 pkg_2 ... pkg_N \
# There are some comments here.
pkg_N+1 ...
```
The above script will fail due to the there are comments on the next line of the line continuation symbol (\). Is there a workaround/trick which can let me write bash script this way?

Regards,
HZ

Re: Use comments on the next line of the line continuation symbol (\).

<sq7alt$2a73v$1@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4809&group=comp.unix.shell#4809

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: Use comments on the next line of the line continuation symbol (\).
Date: Sat, 25 Dec 2021 14:43:09 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <sq7alt$2a73v$1@news.xmission.com>
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
Injection-Date: Sat, 25 Dec 2021 14:43:09 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2432127"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sat, 25 Dec 2021 14:43 UTC

In article <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>,
hongy...@gmail.com <hongyi.zhao@gmail.com> wrote:
>See the following example bash script:
>
>```
>#!/usr/bin/env bash
>
>sudo apt install -y pkg_1 pkg_2 ... pkg_N \
> # There are some comments here.
> pkg_N+1 ...
>```
>The above script will fail due to the there are comments on the next
>line of the line continuation symbol (\). Is there a workaround/trick
>which can let me write bash script this way?

Yes. Don't do that.

--
"Women should not be enlightened or educated in any way. They should be
segregated because they are the cause of unholy erections in holy men.

-- Saint Augustine (354-430) --

Re: Use comments on the next line of the line continuation symbol (\).

<sq7io1$ii1$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4810&group=comp.unix.shell#4810

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: dan1es...@gmail.com (Dan Espen)
Newsgroups: comp.unix.shell
Subject: Re: Use comments on the next line of the line continuation symbol (\).
Date: Sat, 25 Dec 2021 12:00:49 -0500
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <sq7io1$ii1$1@dont-email.me>
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="05ef93f3ea01b248654d1c7a9c0a53ed";
logging-data="19009"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bsGyhJqoJRvmYgBmIAGfK3qZWZEdIPLs="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:cB643yLeWjV61xBBoUj5OpBUrHU=
 by: Dan Espen - Sat, 25 Dec 2021 17:00 UTC

"hongy...@gmail.com" <hongyi.zhao@gmail.com> writes:

> See the following example bash script:
>
> ```
> #!/usr/bin/env bash
>
> sudo apt install -y pkg_1 pkg_2 ... pkg_N \
> # There are some comments here.
> pkg_N+1 ...
> ```
> The above script will fail due to the there are comments on the next
> line of the line continuation symbol (\). Is there a workaround/trick
> which can let me write bash script this way?

Backslash the end of line on the comment.

--
Dan Espen

Re: Use comments on the next line of the line continuation symbol (\).

<83h7awirdl.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4811&group=comp.unix.shell#4811

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!7zGLCApApP6vCVPk6xgP0w.user.46.165.242.75.POSTED!not-for-mail
From: nn.throt...@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re:
Use comments on the next line of the line conti
nuation symbol (\).
Date: Sat, 25 Dec 2021 17:25:10 +0100
Organization: Aioe.org NNTP Server
Message-ID: <83h7awirdl.fsf@helmutwaitzmann.news.arcor.de>
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="2663"; posting-host="7zGLCApApP6vCVPk6xgP0w.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Mail-Copies-To: nobody
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Cancel-Lock: sha1:qIBlFYGRgW7kPVbTdRunysGbD6k=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Helmut Waitzmann - Sat, 25 Dec 2021 16:25 UTC

"hongy...@gmail.com" <hongyi.zhao@gmail.com>:
>See the following example bash script:
>

>#!/usr/bin/env bash
>
>sudo apt install -y pkg_1 pkg_2 ... pkg_N \
> # There are some comments here.
> pkg_N+1 ...

>The above script will fail due to the there are comments on the
>next line of the line continuation symbol (\). Is there a
>workaround/trick which can let me write bash script this way?

The backslash at the end of the line (also known as line
continuation) will tell the shell to remove it and the line feed
following it prior to further parsing the command line.  That is,
the shell parser will see

sudo apt ... pkg_N # There are some comments here.
pkg_N+1 ...

You could do

(
set -- sudo apt install -y pkg_1 pkg_2 ... pkg_N &&
# There are some comments here.
set -- "$@" pkg_N+1 ... &&
exec "$@"
)

though I don't know if it's worth while doing that.  I would place
the comment before the "sudo" invocation.

Note the difference:  The "&&" operator is not disturbed by a
(sequence of) following newline characters, including comment lines.

Re: Use comments on the next line of the line continuation symbol (\).

<875yrc461q.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4812&group=comp.unix.shell#4812

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.unix.shell
Subject: Re: Use comments on the next line of the line continuation symbol (\).
Date: Sat, 25 Dec 2021 15:29:37 -0800
Organization: None to speak of
Lines: 51
Message-ID: <875yrc461q.fsf@nosuchdomain.example.com>
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
<sq7io1$ii1$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="9799c5b66c60baf3193d79f5b19f947d";
logging-data="17367"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/iaUJDsq6qmkDapT1bXX1D"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:rLGaxJKMmTGxdd+3Aezffr764AI=
sha1:ezwWdsh4smX0Xjm1ZsxqL/nSeiI=
 by: Keith Thompson - Sat, 25 Dec 2021 23:29 UTC

Dan Espen <dan1espen@gmail.com> writes:
> "hongy...@gmail.com" <hongyi.zhao@gmail.com> writes:
>
>> See the following example bash script:
>>
>> ```
>> #!/usr/bin/env bash
>>
>> sudo apt install -y pkg_1 pkg_2 ... pkg_N \
>> # There are some comments here.
>> pkg_N+1 ...
>> ```
>> The above script will fail due to the there are comments on the next
>> line of the line continuation symbol (\). Is there a workaround/trick
>> which can let me write bash script this way?
>
> Backslash the end of line on the comment.

I wasn't able to get that to work. Were you?

$ cat zero.bash
#!/bin/bash

echo foo \
bar
$ ./zero.bash
foo bar
$ cat one.bash
#!/bin/bash

echo foo \
# comment
bar
$ ./one.bash
foo
../one.bash: line 5: bar: command not found
$ cat two.bash
#!/bin/bash

echo foo \
# comment \
bar
$ ./two.bash
foo
../two.bash: line 5: bar: command not found
$

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: Use comments on the next line of the line continuation symbol (\).

<sq8cih$84o$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4813&group=comp.unix.shell#4813

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: dan1es...@gmail.com (Dan Espen)
Newsgroups: comp.unix.shell
Subject: Re: Use comments on the next line of the line continuation symbol (\).
Date: Sat, 25 Dec 2021 19:21:37 -0500
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <sq8cih$84o$1@dont-email.me>
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
<sq7io1$ii1$1@dont-email.me> <875yrc461q.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="6dda07a53faf49521a8fb2b9ff947fba";
logging-data="8344"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX196ADdppEgVZm5uLqn5JMJNgIonFX0ihYA="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:rhf30Wnm4UeJT6eGVNJt08+ufBw=
 by: Dan Espen - Sun, 26 Dec 2021 00:21 UTC

Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

> Dan Espen <dan1espen@gmail.com> writes:
>> "hongy...@gmail.com" <hongyi.zhao@gmail.com> writes:
>>
>>> See the following example bash script:
>>>
>>> ```
>>> #!/usr/bin/env bash
>>>
>>> sudo apt install -y pkg_1 pkg_2 ... pkg_N \
>>> # There are some comments here.
>>> pkg_N+1 ...
>>> ```
>>> The above script will fail due to the there are comments on the next
>>> line of the line continuation symbol (\). Is there a workaround/trick
>>> which can let me write bash script this way?
>>
>> Backslash the end of line on the comment.
>
> I wasn't able to get that to work. Were you?

Nope, my mistake.

--
Dan Espen

Re: Use comments on the next line of the line continuation symbol (\).

<83tuewgsih.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4814&group=comp.unix.shell#4814

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!7zGLCApApP6vCVPk6xgP0w.user.46.165.242.75.POSTED!not-for-mail
From: nn.throt...@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re:
Use comments on the next line of the line conti
nuation symbol (\).
Date: Sun, 26 Dec 2021 00:43:34 +0100
Organization: Aioe.org NNTP Server
Message-ID: <83tuewgsih.fsf@helmutwaitzmann.news.arcor.de>
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
<sq7io1$ii1$1@dont-email.me>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="31185"; posting-host="7zGLCApApP6vCVPk6xgP0w.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Cancel-Lock: sha1:+Cxjrn+87V+ULaPymvTvYNs6KHc=
Mail-Copies-To: nobody
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
X-Notice: Filtered by postfilter v. 0.9.2
 by: Helmut Waitzmann - Sat, 25 Dec 2021 23:43 UTC

Dan Espen <dan1espen@gmail.com>:
> "hongy...@gmail.com" <hongyi.zhao@gmail.com> writes:
>
>> See the following example bash script:
>>
>> ```
>> #!/usr/bin/env bash
>>
>> sudo apt install -y pkg_1 pkg_2 ... pkg_N \
>> # There are some comments here.
>> pkg_N+1 ...
>> ```
>> The above script will fail due to the there are comments on the
>> next line of the line continuation symbol (\). Is there a
>> workaround/trick which can let me write bash script this way?
>
> Backslash the end of line on the comment.
>

I don't think so.

POSIX
(<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03>)
says:

“If the current character is a '#', it and all subsequent
characters up to, but excluding, the next <newline> shall
be discarded as a comment. The <newline> that ends the
line is not considered part of the comment.”

=> The backslash character at the end of the line on the comment
will be treated and discarded as part of the comment.  No line
continuation to the next line will occur.  That won't join
"pkg_N+1" to the argument list ending with "pkg_N".

Re: Use comments on the next line of the line continuation symbol (\).

<sq8nde$kij$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4815&group=comp.unix.shell#4815

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: dan1es...@gmail.com (Dan Espen)
Newsgroups: comp.unix.shell
Subject: Re: Use comments on the next line of the line continuation symbol (\).
Date: Sat, 25 Dec 2021 22:26:38 -0500
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <sq8nde$kij$1@dont-email.me>
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
<sq7io1$ii1$1@dont-email.me>
<83tuewgsih.fsf@helmutwaitzmann.news.arcor.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="6dda07a53faf49521a8fb2b9ff947fba";
logging-data="21075"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/BDsSelcLj38LSc0nlcheOOuWoNFOu3yY="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:Mt1dETdoG6AxRkApG6C3yx7KpQA=
 by: Dan Espen - Sun, 26 Dec 2021 03:26 UTC

Helmut Waitzmann <nn.throttle@xoxy.net> writes:

> Dan Espen <dan1espen@gmail.com>:
>> "hongy...@gmail.com" <hongyi.zhao@gmail.com> writes:
>>
>>> See the following example bash script:
>>> ```
>>> #!/usr/bin/env bash
>>>
>>> sudo apt install -y pkg_1 pkg_2 ... pkg_N \
>>> # There are some comments here.
>>> pkg_N+1 ...
>>> ```
>>> The above script will fail due to the there are comments on the
>>> next line of the line continuation symbol (\). Is there a
>>> workaround/trick which can let me write bash script this way?
>>
>> Backslash the end of line on the comment.
>
> I don't think so.
>
> POSIX
> (<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03>)
> says:
>
> “If the current character is a '#', it and all subsequent
> characters up to, but excluding, the next <newline> shall
> be discarded as a comment. The <newline> that ends the
> line is not considered part of the comment.”
>
> => The backslash character at the end of the line on the comment will
> be treated and discarded as part of the comment.  No line
> continuation to the next line will occur.  That won't join
> "pkg_N+1" to the argument list ending with "pkg_N".

Agree.

If I had to have those comments, I suppose this would work:

a='pkg_1'
# Here's that comment...
b='pkg_2'
sudo ... "$a" "$b"

--
Dan Espen

Re: Use comments on the next line of the line continuation symbol (\).

<4b50e439-b736-4947-9b85-f2a3ec665f33n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4816&group=comp.unix.shell#4816

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:6214:2583:: with SMTP id fq3mr10797752qvb.94.1640494417409;
Sat, 25 Dec 2021 20:53:37 -0800 (PST)
X-Received: by 2002:a37:aa0f:: with SMTP id t15mr8395581qke.233.1640494417249;
Sat, 25 Dec 2021 20:53:37 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sat, 25 Dec 2021 20:53:37 -0800 (PST)
In-Reply-To: <sq8nde$kij$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=172.104.91.163; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 172.104.91.163
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
<sq7io1$ii1$1@dont-email.me> <83tuewgsih.fsf@helmutwaitzmann.news.arcor.de> <sq8nde$kij$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4b50e439-b736-4947-9b85-f2a3ec665f33n@googlegroups.com>
Subject: Re: Use comments on the next line of the line continuation symbol (\).
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Sun, 26 Dec 2021 04:53:37 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 62
 by: hongy...@gmail.com - Sun, 26 Dec 2021 04:53 UTC

On Sunday, December 26, 2021 at 11:26:44 AM UTC+8, Dan Espen wrote:
> Helmut Waitzmann <nn.th...@xoxy.net> writes:
>
> > Dan Espen <dan1...@gmail.com>:
> >> "hongy...@gmail.com" <hongy...@gmail.com> writes:
> >>
> >>> See the following example bash script:
> >>> ```
> >>> #!/usr/bin/env bash
> >>>
> >>> sudo apt install -y pkg_1 pkg_2 ... pkg_N \
> >>> # There are some comments here.
> >>> pkg_N+1 ...
> >>> ```
> >>> The above script will fail due to the there are comments on the
> >>> next line of the line continuation symbol (\). Is there a
> >>> workaround/trick which can let me write bash script this way?
> >>
> >> Backslash the end of line on the comment.
> >
> > I don't think so.
> >
> > POSIX
> > (<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03>)
> > says:
> >
> > “If the current character is a '#', it and all subsequent
> > characters up to, but excluding, the next <newline> shall
> > be discarded as a comment. The <newline> that ends the
> > line is not considered part of the comment.”
> >
> > => The backslash character at the end of the line on the comment will
> > be treated and discarded as part of the comment. No line
> > continuation to the next line will occur. That won't join
> > "pkg_N+1" to the argument list ending with "pkg_N".
> Agree.
>
> If I had to have those comments, I suppose this would work:
>
> a='pkg_1'
> # Here's that comment...
> b='pkg_2'
> sudo ... "$a" "$b"

Thank you and all others for the explanation given here. I figured out the following solution based on your above suggestion:

------------------= begin =--------------
#!/usr/bin/env bash

# Here's that comment for packages related to a.
a="pkga_1 pkga_2 ... pkga_n \
pkga_n+1"
# Here's that comment for packages related to b.
b="pkgb_1 pkgb_2 ... pkgb_n \
pkgb_n+1"

sudo apt install -y $a $b
------------------= end =--------------

HZ

Re: Use comments on the next line of the line continuation symbol (\).

<5bcc16aa-f297-411a-b226-c3036523e155n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4817&group=comp.unix.shell#4817

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:622a:5ca:: with SMTP id d10mr10544903qtb.600.1640494736061;
Sat, 25 Dec 2021 20:58:56 -0800 (PST)
X-Received: by 2002:a05:6214:27ee:: with SMTP id jt14mr10941033qvb.2.1640494735900;
Sat, 25 Dec 2021 20:58:55 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sat, 25 Dec 2021 20:58:55 -0800 (PST)
In-Reply-To: <4b50e439-b736-4947-9b85-f2a3ec665f33n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=172.104.91.163; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 172.104.91.163
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
<sq7io1$ii1$1@dont-email.me> <83tuewgsih.fsf@helmutwaitzmann.news.arcor.de>
<sq8nde$kij$1@dont-email.me> <4b50e439-b736-4947-9b85-f2a3ec665f33n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5bcc16aa-f297-411a-b226-c3036523e155n@googlegroups.com>
Subject: Re: Use comments on the next line of the line continuation symbol (\).
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Sun, 26 Dec 2021 04:58:56 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 69
 by: hongy...@gmail.com - Sun, 26 Dec 2021 04:58 UTC

On Sunday, December 26, 2021 at 12:53:39 PM UTC+8, hongy...@gmail.com wrote:
> On Sunday, December 26, 2021 at 11:26:44 AM UTC+8, Dan Espen wrote:
> > Helmut Waitzmann <nn.th...@xoxy.net> writes:
> >
> > > Dan Espen <dan1...@gmail.com>:
> > >> "hongy...@gmail.com" <hongy...@gmail.com> writes:
> > >>
> > >>> See the following example bash script:
> > >>> ```
> > >>> #!/usr/bin/env bash
> > >>>
> > >>> sudo apt install -y pkg_1 pkg_2 ... pkg_N \
> > >>> # There are some comments here.
> > >>> pkg_N+1 ...
> > >>> ```
> > >>> The above script will fail due to the there are comments on the
> > >>> next line of the line continuation symbol (\). Is there a
> > >>> workaround/trick which can let me write bash script this way?
> > >>
> > >> Backslash the end of line on the comment.
> > >
> > > I don't think so.
> > >
> > > POSIX
> > > (<https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03>)
> > > says:
> > >
> > > “If the current character is a '#', it and all subsequent
> > > characters up to, but excluding, the next <newline> shall
> > > be discarded as a comment. The <newline> that ends the
> > > line is not considered part of the comment.”
> > >
> > > => The backslash character at the end of the line on the comment will
> > > be treated and discarded as part of the comment. No line
> > > continuation to the next line will occur. That won't join
> > > "pkg_N+1" to the argument list ending with "pkg_N".
> > Agree.
> >
> > If I had to have those comments, I suppose this would work:
> >
> > a='pkg_1'
> > # Here's that comment...
> > b='pkg_2'
> > sudo ... "$a" "$b"
> Thank you and all others for the explanation given here. I figured out the following solution based on your above suggestion:
>
> ------------------= begin =--------------
> #!/usr/bin/env bash
>
> # Here's that comment for packages related to a.
> a="pkga_1 pkga_2 ... pkga_n \
> pkga_n+1"
> # Here's that comment for packages related to b.
> b="pkgb_1 pkgb_2 ... pkgb_n \
> pkgb_n+1"
>
> sudo apt install -y $a $b

The following form is also valid:

sudo apt install -y $a \
$b

> ------------------= end =--------------
>
> HZ

Re: Use comments on the next line of the line continuation symbol (\).

<83h7avg8c1.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4818&group=comp.unix.shell#4818

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!7zGLCApApP6vCVPk6xgP0w.user.46.165.242.75.POSTED!not-for-mail
From: nn.throt...@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re:
Use comments on the next line of the line conti
nuation symbol (\).
Date: Sun, 26 Dec 2021 07:59:26 +0100
Organization: Aioe.org NNTP Server
Message-ID: <83h7avg8c1.fsf@helmutwaitzmann.news.arcor.de>
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
<sq7io1$ii1$1@dont-email.me>
<83tuewgsih.fsf@helmutwaitzmann.news.arcor.de>
<sq8nde$kij$1@dont-email.me>
<4b50e439-b736-4947-9b85-f2a3ec665f33n@googlegroups.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="23181"; posting-host="7zGLCApApP6vCVPk6xgP0w.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
X-Notice: Filtered by postfilter v. 0.9.2
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Cancel-Lock: sha1:6rlTyZHoQfiC6QcG2MPab4bcUfc=
Mail-Copies-To: nobody
 by: Helmut Waitzmann - Sun, 26 Dec 2021 06:59 UTC

"hongy...@gmail.com" <hongyi.zhao@gmail.com>:
>Thank you and all others for the explanation given here. I figured
>out the following solution based on your above suggestion:
>
>------------------= begin =--------------
>#!/usr/bin/env bash
>
># Here's that comment for packages related to a.
>a="pkga_1 pkga_2 ... pkga_n \
> pkga_n+1"
># Here's that comment for packages related to b.
>b="pkgb_1 pkgb_2 ... pkgb_n \
> pkgb_n+1"
>
>sudo apt install -y $a $b
>------------------= end =--------------

It only works if neither of the package names contains white space.

To be not dependent on that condition I used in
<83h7awirdl.fsf@helmutwaitzmann.news.arcor.de> the array‐like
positional parameter list "@" of the shell to set up the command to
be invoked.

Imagine, what would happen if the names of the packages contained
white space like

"pkg a 1", "pkg a 2", … "pkg a n+1",
"pkg b 1", "pkg b 2", … "pkg b n+1".

Then your example would invoke the command

sudo apt install -y pkg a 1 pkg a 2 … pkg a n+1 … \
pkg b 1 pkg b 2 … pkg b n+1

(i. e. the package names would have been split at their internal
blank characters) rather than the intended command

sudo apt install -y 'pkg a 1' 'pkg a 2' … 'pkg a n+1' \
'pkg b 1' 'pkg b 2' … 'pkg b n+1'

because of the reference to the unquoted variables $a and $b.

If one the other hand I do

(
set -- sudo apt install -y &&
# Here's that comment for packages related to a.
set -- "$@" 'pkg a 1' 'pkg a 2' … 'pkg a n+1' &&
# Here's that comment for packages related to b.
set -- "$@" 'pkg b 1' 'pkg b 2' … 'pkg b n+1' &&
exec "$@"
)

then the command invoked will effectively be

sudo apt install -y 'pkg a 1' 'pkg a 2' … 'pkg a n+1' \
'pkg b 1' 'pkg b 2' … 'pkg b n+1'

as intended.

Re: Use comments on the next line of the line continuation symbol (\).

<c81dfbad-6ff1-4b24-bdfb-0d5627c392cdn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4819&group=comp.unix.shell#4819

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:622a:64c:: with SMTP id a12mr11214882qtb.312.1640519334773;
Sun, 26 Dec 2021 03:48:54 -0800 (PST)
X-Received: by 2002:ac8:5a0b:: with SMTP id n11mr11362149qta.372.1640519334597;
Sun, 26 Dec 2021 03:48:54 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Sun, 26 Dec 2021 03:48:54 -0800 (PST)
In-Reply-To: <83h7avg8c1.fsf@helmutwaitzmann.news.arcor.de>
Injection-Info: google-groups.googlegroups.com; posting-host=203.175.13.154; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 203.175.13.154
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
<sq7io1$ii1$1@dont-email.me> <83tuewgsih.fsf@helmutwaitzmann.news.arcor.de>
<sq8nde$kij$1@dont-email.me> <4b50e439-b736-4947-9b85-f2a3ec665f33n@googlegroups.com>
<83h7avg8c1.fsf@helmutwaitzmann.news.arcor.de>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <c81dfbad-6ff1-4b24-bdfb-0d5627c392cdn@googlegroups.com>
Subject: Re: Use comments on the next line of the line continuation symbol (\).
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Sun, 26 Dec 2021 11:48:54 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 89
 by: hongy...@gmail.com - Sun, 26 Dec 2021 11:48 UTC

On Sunday, December 26, 2021 at 2:59:36 PM UTC+8, Helmut Waitzmann wrote:
> "hongy...@gmail.com" <hongyi.zhao@gmail.com>:
> >Thank you and all others for the explanation given here. I figured
> >out the following solution based on your above suggestion:
> >
> >------------------= begin =--------------
> >#!/usr/bin/env bash
> >
> ># Here's that comment for packages related to a.
> >a="pkga_1 pkga_2 ... pkga_n \
> > pkga_n+1"
> ># Here's that comment for packages related to b.
> >b="pkgb_1 pkgb_2 ... pkgb_n \
> > pkgb_n+1"
> >
> >sudo apt install -y $a $b
> >------------------= end =--------------
> It only works if neither of the package names contains white space.

For Debian derivative systems, the package name specification is described here [1]:

--------
This manual page documents the dpkg-name program which provides an easy way to rename Debian packages into their full package names. A full package name consists of package_version_architecture.package-type as specified in the control file of the package. The version part of the filename consists of the upstream version information optionally followed by a hyphen and the revision information. The package-type part comes from that field if present or fallbacks to deb.
--------

As you can see, the package names don't contain white space. As for other operating systems, I'm not sure, but I believe it's not a good practice to use whitespace in the naming of software packages on a system.

[1] https://manpages.debian.org/bullseye/dpkg-dev/dpkg-name.1.en.html

> To be not dependent on that condition I used in
> <83h7awirdl.fsf@helmutwaitzmann.news.arcor.de> the array‐like
> positional parameter list "@" of the shell to set up the command to
> be invoked.
>
> Imagine, what would happen if the names of the packages contained
> white space like
>
> "pkg a 1", "pkg a 2", … "pkg a n+1",
> "pkg b 1", "pkg b 2", … "pkg b n+1".
>
>
> Then your example would invoke the command
>
>
> sudo apt install -y pkg a 1 pkg a 2 … pkg a n+1 … \
> pkg b 1 pkg b 2 … pkg b n+1
>
> (i. e. the package names would have been split at their internal
> blank characters) rather than the intended command
>
> sudo apt install -y 'pkg a 1' 'pkg a 2' … 'pkg a n+1' \
> 'pkg b 1' 'pkg b 2' … 'pkg b n+1'
>
> because of the reference to the unquoted variables $a and $b.
>
>
> If one the other hand I do
>
>
> (
> set -- sudo apt install -y &&
> # Here's that comment for packages related to a.
> set -- "$@" 'pkg a 1' 'pkg a 2' … 'pkg a n+1' &&
> # Here's that comment for packages related to b.
> set -- "$@" 'pkg b 1' 'pkg b 2' … 'pkg b n+1' &&
> exec "$@"
> )
>
> then the command invoked will effectively be
>
>
> sudo apt install -y 'pkg a 1' 'pkg a 2' … 'pkg a n+1' \
> 'pkg b 1' 'pkg b 2' … 'pkg b n+1'
>
> as intended.

Thank you for pointing this out.

HZ

Re: Use comments on the next line of the line continuation symbol (\).

<j2rmhuFbkg5U1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4820&group=comp.unix.shell#4820

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: tav...@gmail.com (Tavis Ormandy)
Newsgroups: comp.unix.shell
Subject: Re: Use comments on the next line of the line continuation symbol
(\).
Date: 26 Dec 2021 17:19:58 GMT
Lines: 34
Message-ID: <j2rmhuFbkg5U1@mid.individual.net>
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
X-Trace: individual.net EOZV5fvIEd51twZqxxioIgq0yZXvG7+llWeFVjn50nR8MiajE5
Cancel-Lock: sha1:EflWmWU1CIwC9IVLv5yEHtozUBo=
User-Agent: slrn/pre1.0.4-5 (Linux)
 by: Tavis Ormandy - Sun, 26 Dec 2021 17:19 UTC

On 2021-12-25, hongy...@gmail.com wrote:
> See the following example bash script:
>
> ```
> #!/usr/bin/env bash
>
> sudo apt install -y pkg_1 pkg_2 ... pkg_N \
> # There are some comments here.
> pkg_N+1 ...
> ```
> The above script will fail due to the there are comments on the next line of the line continuation symbol (\). Is there a workaround/trick which can let me write bash script this way?
>

I kinda like this style:

pkgs=(
# This is package foo
pkg_1
# This is package bar
pkg_2
pkg_3
# This is package baz
pkg_4
pkg_5 # Added on 19-Jan-22
)

apt install ${pkgs[@]}

Tavis.

--
_o) $ lynx lock.cmpxchg8b.com
/\\ _o) _o) $ finger taviso@sdf.org
_\_V _( ) _( ) @taviso

Re: Use comments on the next line of the line continuation symbol (\).

<sqarvc$1iv$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4821&group=comp.unix.shell#4821

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Use comments on the next line of the line continuation symbol
(\).
Date: Sun, 26 Dec 2021 23:56:44 +0100
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <sqarvc$1iv$1@dont-email.me>
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
<j2rmhuFbkg5U1@mid.individual.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 26 Dec 2021 22:56:44 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="db5c026b2d01603cba7142da382c6ad5";
logging-data="1631"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19640JoveXyodfz44kpOctX"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:4yOS3cAyyw4MF4aKBzEF82NCZWU=
In-Reply-To: <j2rmhuFbkg5U1@mid.individual.net>
 by: Janis Papanagnou - Sun, 26 Dec 2021 22:56 UTC

On 26.12.2021 18:19, Tavis Ormandy wrote:
> On 2021-12-25, hongy...@gmail.com wrote:
>> See the following example bash script:
>>
>> ```
>> #!/usr/bin/env bash
>>
>> sudo apt install -y pkg_1 pkg_2 ... pkg_N \
>> # There are some comments here.
>> pkg_N+1 ...
>> ```
>> The above script will fail due to the there are comments on the next line of the line continuation symbol (\). Is there a workaround/trick which can let me write bash script this way?
>>
>
> I kinda like this style:
>
> pkgs=(
> # This is package foo
> pkg_1
> # This is package bar
> pkg_2
> pkg_3
> # This is package baz
> pkg_4
> pkg_5 # Added on 19-Jan-22
> )
>
> apt install ${pkgs[@]}

As already suggested upthread; quote that expression

apt install "${pkgs[@]}"

Janis

> Tavis.
>

Re: Use comments on the next line of the line continuation symbol (\).

<slrnssjfqc.af7.hymie@nasalinux.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4823&group=comp.unix.shell#4823

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.de!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx42.iad.POSTED!not-for-mail
Newsgroups: comp.unix.shell
From: hym...@nasalinux.net (hymie!)
Subject: Re: Use comments on the next line of the line continuation symbol
(\).
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
Organization: Eric Conspiracy Secret Labs
User-Agent: slrn/pre1.0.4-5 (Linux)
Message-ID: <slrnssjfqc.af7.hymie@nasalinux.net>
Lines: 26
X-Complaints-To: abuse@usenet-news.net
NNTP-Posting-Date: Mon, 27 Dec 2021 13:27:40 UTC
Date: Mon, 27 Dec 2021 13:27:40 GMT
X-Received-Bytes: 1507
 by: hymie! - Mon, 27 Dec 2021 13:27 UTC

Short answer, no.

(A) A backslash at the end of a line does not, to the shell, designate
a "new line". The next line is a continuation of the current line,
as though the newline character(s) simply wasn't there.

(B) A comment character is absolute -- anything on a line after the
comment character is ignored by the shell.

--hymie!

In our last episode, the evil Dr. Lacto had captured our hero,
hongy...@gmail.com <hongyi.zhao@gmail.com>, who said:
> See the following example bash script:
>
> ```
> #!/usr/bin/env bash
>
> sudo apt install -y pkg_1 pkg_2 ... pkg_N \
> # There are some comments here.
> pkg_N+1 ...
> ```
> The above script will fail due to the there are comments on the next line of the line continuation symbol (\). Is there a workaround/trick which can let me write bash script this way?
>
> Regards,
> HZ

Re: Use comments on the next line of the line continuation symbol (\).

<83v8z9d4op.fsf@helmutwaitzmann.news.arcor.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4828&group=comp.unix.shell#4828

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!JFeXAyC1wMOaB+1Hh8cU9w.user.46.165.242.75.POSTED!not-for-mail
From: nn.throt...@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re:
Use comments on the next line of the line conti
nuation symbol (\).
Date: Tue, 28 Dec 2021 00:11:02 +0100
Organization: Aioe.org NNTP Server
Message-ID: <83v8z9d4op.fsf@helmutwaitzmann.news.arcor.de>
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
<sq7io1$ii1$1@dont-email.me>
<83tuewgsih.fsf@helmutwaitzmann.news.arcor.de>
<sq8nde$kij$1@dont-email.me>
<4b50e439-b736-4947-9b85-f2a3ec665f33n@googlegroups.com>
<83h7avg8c1.fsf@helmutwaitzmann.news.arcor.de>
<c81dfbad-6ff1-4b24-bdfb-0d5627c392cdn@googlegroups.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="15251"; posting-host="JFeXAyC1wMOaB+1Hh8cU9w.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Mail-Copies-To: nobody
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:CkcaZyVT6U4wAf9F2srDYq+vOZg=
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
 by: Helmut Waitzmann - Mon, 27 Dec 2021 23:11 UTC

"hongy...@gmail.com" <hongyi.zhao@gmail.com>:
> On Sunday, December 26, 2021 at 2:59:36 PM UTC+8, Helmut Waitzmann
> wrote:
>> It only works if neither of the package names contains white
>> space.
>
> For Debian derivative systems, the package name specification is
> described here [1]:
>
> --------
> This manual page documents the dpkg-name program which provides an
> easy way to rename Debian packages into their full package names. A
> full package name consists of
> package_version_architecture.package-type as specified in the
> control file of the package. The version part of the filename
> consists of the upstream version information optionally followed by
> a hyphen and the revision information. The package-type part comes
> from that field if present or fallbacks to deb.
> --------
>
> [1]
> https://manpages.debian.org/bullseye/dpkg-dev/dpkg-name.1.en.html
>
> As you can see, the package names don't contain white space.
>

As far as I understand, the cited text does not say anything about
the package, version, architecture, and package-type components of a
full package name other than that they are specified in the control
file (of unknown contents) of the package, and that the version part
is a concatenation of the upstream version information (of unknown
contents) followed by a hyphen and the revision information (of
unknown contents).  And finally these parts are concatenated by
underscores.

If any of these parts contains some white space, then that white
space will be in the full package name, as well.

> As for other operating systems, I'm not sure, but I believe it's
> not a good practice to use whitespace in the naming of software
> packages on a system.

I agree, but I believe, it's good practice to carefully design
software (here: the shell script) to be as robust as possible.

The best practice with regard to shell programming:  Don't ever
misuse a (non‐array) shell variable v as an array variable by merely
gluing together the array elements with white space and then using
the value of the variable by unquoted references ($v) to have it be
split at white space.

With Bash and other modern shells you have got even true array
variables.  So you could use them in this case.  (But as you might
know, I prefer (if at all possible) to use solutions which work with
any shell conforming to the POSIX standard.)

What's the cause of the log4shell exploit and many other exploits? 
It's always that disastrous "Strange variable values? – That won't
happen!" ignorance.

Re: Use comments on the next line of the line continuation symbol (\).

<7e7e5d71-26a6-40f0-8ec1-4aecf7f3f6a5n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4830&group=comp.unix.shell#4830

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:622a:64c:: with SMTP id a12mr17594765qtb.312.1640677602151;
Mon, 27 Dec 2021 23:46:42 -0800 (PST)
X-Received: by 2002:a05:6214:23c8:: with SMTP id hr8mr18161277qvb.79.1640677602011;
Mon, 27 Dec 2021 23:46:42 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Mon, 27 Dec 2021 23:46:41 -0800 (PST)
In-Reply-To: <83v8z9d4op.fsf@helmutwaitzmann.news.arcor.de>
Injection-Info: google-groups.googlegroups.com; posting-host=203.175.13.138; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 203.175.13.138
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
<sq7io1$ii1$1@dont-email.me> <83tuewgsih.fsf@helmutwaitzmann.news.arcor.de>
<sq8nde$kij$1@dont-email.me> <4b50e439-b736-4947-9b85-f2a3ec665f33n@googlegroups.com>
<83h7avg8c1.fsf@helmutwaitzmann.news.arcor.de> <c81dfbad-6ff1-4b24-bdfb-0d5627c392cdn@googlegroups.com>
<83v8z9d4op.fsf@helmutwaitzmann.news.arcor.de>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7e7e5d71-26a6-40f0-8ec1-4aecf7f3f6a5n@googlegroups.com>
Subject: Re: Use comments on the next line of the line continuation symbol (\).
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Tue, 28 Dec 2021 07:46:42 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 82
 by: hongy...@gmail.com - Tue, 28 Dec 2021 07:46 UTC

On Tuesday, December 28, 2021 at 8:55:13 AM UTC+8, Helmut Waitzmann wrote:
> "hongy...@gmail.com" <hongy...@gmail.com>:
> > On Sunday, December 26, 2021 at 2:59:36 PM UTC+8, Helmut Waitzmann
> > wrote:
> >> It only works if neither of the package names contains white
> >> space.
> >
> > For Debian derivative systems, the package name specification is
> > described here [1]:
> >
> > --------
> > This manual page documents the dpkg-name program which provides an
> > easy way to rename Debian packages into their full package names. A
> > full package name consists of
> > package_version_architecture.package-type as specified in the
> > control file of the package. The version part of the filename
> > consists of the upstream version information optionally followed by
> > a hyphen and the revision information. The package-type part comes
> > from that field if present or fallbacks to deb.
> > --------
> >
> > [1]
> > https://manpages.debian.org/bullseye/dpkg-dev/dpkg-name.1.en.html
> >
> > As you can see, the package names don't contain white space.
> >
> As far as I understand, the cited text does not say anything about
> the package, version, architecture, and package-type components of a
> full package name other than that they are specified in the control
> file (of unknown contents) of the package, and that the version part
> is a concatenation of the upstream version information (of unknown
> contents) followed by a hyphen and the revision information (of
> unknown contents). And finally these parts are concatenated by
> underscores.
>
> If any of these parts contains some white space, then that white
> space will be in the full package name, as well.

Sorry. I should have shown you, but I haven't found it till now, the following description given in "Debian RFC822 control data format" [1]:

I should have shown you, but I didn't find the following description given in "Debian RFC822 Control Data Formats" until now:

$ man deb822 | egrep -A3 -i 'whitespace must not'
Whitespace must not appear inside names (of packages, architectures,
files or anything else) or version numbers, or between the characters of
multi-character version relationships.

[1] https://man7.org/linux/man-pages/man5/deb822.5.html

> > As for other operating systems, I'm not sure, but I believe it's
> > not a good practice to use whitespace in the naming of software
> > packages on a system.
> I agree, but I believe, it's good practice to carefully design

Whether you should use the following wording:

.... it's a good practice ...

> software (here: the shell script) to be as robust as possible.
>
> The best practice with regard to shell programming: Don't ever
> misuse a (non‐array) shell variable v as an array variable by merely
> gluing together the array elements with white space and then using
> the value of the variable by unquoted references ($v) to have it be
> split at white space.
>
> With Bash and other modern shells you have got even true array
> variables. So you could use them in this case. (But as you might
> know, I prefer (if at all possible) to use solutions which work with
> any shell conforming to the POSIX standard.)

But this may lead to the loss of some convenience. There is no best of both worlds in the world.
> What's the cause of the log4shell exploit and many other exploits?
> It's always that disastrous "Strange variable values? – That won't
> happen!" ignorance.

How to simulate array variables in the POSIX shell (was: Use comments on the next line of the line continuation symbol (\).)

<838rw4bed3.fsf_-_@helmutwaitzmann.news.arcor.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4831&group=comp.unix.shell#4831

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!59PvdCMQvhsY/NU6IWYmZA.user.46.165.242.75.POSTED!not-for-mail
From: nn.throt...@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: How to simulate array variables in the POSIX sh
ell (was: Use comments on the next line of the
line continuation symbol (\).)
Date: Tue, 28 Dec 2021 22:37:12 +0100
Organization: Aioe.org NNTP Server
Message-ID: <838rw4bed3.fsf_-_@helmutwaitzmann.news.arcor.de>
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
<sq7io1$ii1$1@dont-email.me>
<83tuewgsih.fsf@helmutwaitzmann.news.arcor.de>
<sq8nde$kij$1@dont-email.me>
<4b50e439-b736-4947-9b85-f2a3ec665f33n@googlegroups.com>
<83h7avg8c1.fsf@helmutwaitzmann.news.arcor.de>
<c81dfbad-6ff1-4b24-bdfb-0d5627c392cdn@googlegroups.com>
<83v8z9d4op.fsf@helmutwaitzmann.news.arcor.de>
<7e7e5d71-26a6-40f0-8ec1-4aecf7f3f6a5n@googlegroups.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="22169"; posting-host="59PvdCMQvhsY/NU6IWYmZA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Cancel-Lock: sha1:c3dFbac6FhmFUQYf5pPJrLk4NeM=
X-Notice: Filtered by postfilter v. 0.9.2
Mail-Copies-To: nobody
 by: Helmut Waitzmann - Tue, 28 Dec 2021 21:37 UTC

"hongy...@gmail.com" <hongyi.zhao@gmail.com>:
> On Tuesday, December 28, 2021 at 8:55:13 AM UTC+8, Helmut Waitzmann wrote:
>> "hongy...@gmail.com" <hongy...@gmail.com>:
>> As far as I understand, the cited text does not say anything
>> about the package, version, architecture, and package-type
>> components of a full package name other than that they are
>> specified in the control file (of unknown contents) of the
>> package, and that the version part is a concatenation of the
>> upstream version information (of unknown contents) followed by a
>> hyphen and the revision information (of unknown contents). And
>> finally these parts are concatenated by underscores.
>>
>> If any of these parts contains some white space, then that white
>> space will be in the full package name, as well.
>
> Sorry. I should have shown you, but I haven't found it till now,
> the following description given in "Debian RFC822 control data
> format" [1]:
>
> I should have shown you, but I didn't find the following
> description given in "Debian RFC822 Control Data Formats" until
> now:
>
> $ man deb822 | egrep -A3 -i 'whitespace must not'
> Whitespace must not appear inside names (of packages,
> architectures, files or anything else) or version numbers,
> or between the characters of multi-character version
> relationships.
>
> [1] https://man7.org/linux/man-pages/man5/deb822.5.html
>

Thank you.  So we are lucky, and in this case it's not necessary to
do what is described below.

>> The best practice with regard to shell programming: Don't ever
>> misuse a (non‐array) shell variable v as an array variable by
>> merely gluing together the array elements with white space and
>> then using the value of the variable by unquoted references ($v)
>> to have it be split at white space.
>>
>> With Bash and other modern shells you have got even true array
>> variables. So you could use them in this case. (But as you might
>> know, I prefer (if at all possible) to use solutions which work
>> with any shell conforming to the POSIX standard.)
>
> But this may lead to the loss of some convenience. There is no best
> of both worlds in the world.

Compared with the disatrous consequences of an exploit I'd clearly
prefer array variables with Bash and other modern shells.

And even if I want to stay compatible with the POSIX standard, I
consider an exploit far more inconvenient than the increased
complexity in programming for the shell.

Simulating an array variable using the POSIX standard could be done
like this:

Define a shell function "quote_words" (see below), which, when
given zero or more invocation arguments, computes (a part of) a
shell command line (of a simple command) resembling a command line
to invoke those arguments and writes it to standard output, where
it can be put in a simple shell variable by command substitution,
like this:

v="$(quote_words printf '%s\n' 'Hello, world!')"

It can then invoked using the "eval" shell builtin command:

eval "$v"

Try the following command to see, what that is.  The commands

v="$(quote_words printf '%s\n' 'Hello, world!')" &&
printf '%s\n' "$v" &&
eval "$v"

will output

'printf' '%s\n' 'Hello, world!'
Hello, world!

Now, this content of the variable v, which is a (part of a) shell
command line of a simple command, can even be glued together with
more parts of a command line by putting a blank between each of the
command line parts to be glued together and stored in a simple
shell variable.  Finally, this shell variable can be fed to the
"eval" shell builtin command to let it be invoked as a command
line.

In the example of the OP this would look like these shell commands:

v="$(quote_words sudo apt install -y)" &&

# Here's that comment for packages related to a.
#
v="$v $(quote_words 'pkg a 1' 'pkg a 2' … 'pkg a n+1')" &&

# Here's that comment for packages related to b.
#
v="$v $(quote_words 'pkg b 1' 'pkg b 2' … 'pkg b n+1')" &&

# Finally, let the "eval" command parse and invoke it:
#
eval "$v" &&

That quote_words function might look like this:

### The function quote_words starts here.
quote_words()
(
# Outputs a (part of) a command line resembling a 'simple
# command' to be used by a POSIX-compliant shell.
#
# Usage:
#
# quote_words words to be quoted...
#
# exit code:
# 0, if the command line has been successfully constructed,
# !=0, if the function failed to construct the command line.
#
# Examples:
#
# Put a word list resembling a command invocation into a command
# line:
#
# if command_line="$(
# quote_words program with parameters
# )"
# then
# # "eval" it:
# eval " $command_line"
#
# # or pass it to a new shell:
# sh -c -- "$command_line" sh
#
# # or pass it to "su":
# su -- - a_user -c -- "$command_line" -su
#
# # or use it remotely:
# ssh user@remote.host.example " $command_line"
#
# else
# # failed to compute the command line.
# fi
#
#
# Store the shell positional parameters in a variable and
# restore them later:
#
# if args="$(quote_words "$@")"
# then
# # the positional parameters may be changed for any purpose
# # ...
#
# # restore the positional parameters:
#
# eval "set -- $args"
# else
# # failed to save the positional parameters.
# fi

# "wordsep" contains a separator used for the list of the quoted
# words. The first need not to be preceded by a separater:

wordsep for word
do
# "$wordsep" separates each word (except the first one) from
# its precedessor:
printf '%s' "$wordsep"
if test -z "$word"
then
# The word is empty. Then the result is "''":
printf '%s' "''"
else
# The word is not empty.
while
{
prefix="${word%%\'*}"
word="${word#"$prefix"}"
# "$prefix" is the longest prefix of "$word", that does
# not contain any apostrophes; it is removed from "$word".
# Conclusion: "$word" is either empty or starts with an
# apostrophe.
if test -n "$prefix"
then
# "$prefix" consists of one or more characters. Put
# them in between of two apostrophes:
printf '%s' \'"${prefix}"\'
fi
test -n "$word" &&
{
# "$word" is not empty. Conclusion: "$word" starts with
# one or more apostrophes.
apostr="${word%%[!\']*}"
# "$apostr" ist the longest beginning part of "$word",
# that contains apostrophes only.
if test -n "${apostr#"'"}"
then
# There are at least 2 apostrophes: Put it in between
# of two double quotes:
printf '"%s"' "${apostr}"
else
# There is 1 apostrophe: Precede it by a backslash:
printf '\\%s' "${apostr}"
fi
# Remove the beginning apostrophes from "$word":
word="${word#"$apostr"}"
# If nothing is left, we are done:
${word:+:} false
}
}
do
:
done
fi
# All following words (except the first one) have to be
# separated from their precedessor with a blank:
wordsep=' '
done
printf '\n'
)
### The function quote_words ends here.

Of course I won't want to write this function from scratch in every
shell script where it is to be used.  Therefore I put it in a file,
say "~/shell-lib/quote_words.sh".  Then, if I like to use it in a
shell script, I just do


Click here to read the complete article
Re: How to simulate array variables in the POSIX shell (was: Use comments on the next line of the line continuation symbol (\).)

<3adb7130-3728-4e0e-9748-9d70d8a9969cn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4832&group=comp.unix.shell#4832

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:622a:44e:: with SMTP id o14mr21963166qtx.369.1640780100263;
Wed, 29 Dec 2021 04:15:00 -0800 (PST)
X-Received: by 2002:a05:6214:27ee:: with SMTP id jt14mr23290599qvb.2.1640780100060;
Wed, 29 Dec 2021 04:15:00 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Wed, 29 Dec 2021 04:14:59 -0800 (PST)
In-Reply-To: <838rw4bed3.fsf_-_@helmutwaitzmann.news.arcor.de>
Injection-Info: google-groups.googlegroups.com; posting-host=203.175.13.154; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 203.175.13.154
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
<sq7io1$ii1$1@dont-email.me> <83tuewgsih.fsf@helmutwaitzmann.news.arcor.de>
<sq8nde$kij$1@dont-email.me> <4b50e439-b736-4947-9b85-f2a3ec665f33n@googlegroups.com>
<83h7avg8c1.fsf@helmutwaitzmann.news.arcor.de> <c81dfbad-6ff1-4b24-bdfb-0d5627c392cdn@googlegroups.com>
<83v8z9d4op.fsf@helmutwaitzmann.news.arcor.de> <7e7e5d71-26a6-40f0-8ec1-4aecf7f3f6a5n@googlegroups.com>
<838rw4bed3.fsf_-_@helmutwaitzmann.news.arcor.de>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <3adb7130-3728-4e0e-9748-9d70d8a9969cn@googlegroups.com>
Subject: Re: How to simulate array variables in the POSIX shell (was: Use
comments on the next line of the line continuation symbol (\).)
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Wed, 29 Dec 2021 12:15:00 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 332
 by: hongy...@gmail.com - Wed, 29 Dec 2021 12:14 UTC

On Wednesday, December 29, 2021 at 6:42:40 AM UTC+8, Helmut Waitzmann wrote:
> "hongy...@gmail.com" <hongy...@gmail.com>:
> > On Tuesday, December 28, 2021 at 8:55:13 AM UTC+8, Helmut Waitzmann wrote:
> >> "hongy...@gmail.com" <hongy...@gmail.com>:
> >> As far as I understand, the cited text does not say anything
> >> about the package, version, architecture, and package-type
> >> components of a full package name other than that they are
> >> specified in the control file (of unknown contents) of the
> >> package, and that the version part is a concatenation of the
> >> upstream version information (of unknown contents) followed by a
> >> hyphen and the revision information (of unknown contents). And
> >> finally these parts are concatenated by underscores.
> >>
> >> If any of these parts contains some white space, then that white
> >> space will be in the full package name, as well.
> >
> > Sorry. I should have shown you, but I haven't found it till now,
> > the following description given in "Debian RFC822 control data
> > format" [1]:
> >
> > I should have shown you, but I didn't find the following
> > description given in "Debian RFC822 Control Data Formats" until
> > now:
> >
> > $ man deb822 | egrep -A3 -i 'whitespace must not'
> > Whitespace must not appear inside names (of packages,
> > architectures, files or anything else) or version numbers,
> > or between the characters of multi-character version
> > relationships.
> >
> > [1] https://man7.org/linux/man-pages/man5/deb822.5.html
> >
> Thank you. So we are lucky, and in this case it's not necessary to
> do what is described below.
> >> The best practice with regard to shell programming: Don't ever
> >> misuse a (non‐array) shell variable v as an array variable by
> >> merely gluing together the array elements with white space and
> >> then using the value of the variable by unquoted references ($v)
> >> to have it be split at white space.
> >>
> >> With Bash and other modern shells you have got even true array
> >> variables. So you could use them in this case. (But as you might
> >> know, I prefer (if at all possible) to use solutions which work
> >> with any shell conforming to the POSIX standard.)
> >
> > But this may lead to the loss of some convenience. There is no best
> > of both worlds in the world.
> Compared with the disatrous consequences of an exploit I'd clearly
> prefer array variables with Bash and other modern shells.
>
>
> And even if I want to stay compatible with the POSIX standard, I
> consider an exploit far more inconvenient than the increased
> complexity in programming for the shell.
>
> Simulating an array variable using the POSIX standard could be done
> like this:
>
> Define a shell function "quote_words" (see below), which, when
> given zero or more invocation arguments, computes (a part of) a
> shell command line (of a simple command) resembling a command line
> to invoke those arguments and writes it to standard output, where
> it can be put in a simple shell variable by command substitution,
> like this:
>
> v="$(quote_words printf '%s\n' 'Hello, world!')"
>
> It can then invoked using the "eval" shell builtin command:
>
>
> eval "$v"
>
>
> Try the following command to see, what that is. The commands
>
>
> v="$(quote_words printf '%s\n' 'Hello, world!')" &&
> printf '%s\n' "$v" &&
> eval "$v"
>
>
> will output
>
>
> 'printf' '%s\n' 'Hello, world!'
> Hello, world!
>
>
> Now, this content of the variable v, which is a (part of a) shell
> command line of a simple command, can even be glued together with
> more parts of a command line by putting a blank between each of the
> command line parts to be glued together and stored in a simple
> shell variable. Finally, this shell variable can be fed to the
> "eval" shell builtin command to let it be invoked as a command
> line.
>
>
> In the example of the OP this would look like these shell commands:
>
>
> v="$(quote_words sudo apt install -y)" &&
> # Here's that comment for packages related to a.
> #
> v="$v $(quote_words 'pkg a 1' 'pkg a 2' … 'pkg a n+1')" &&
> # Here's that comment for packages related to b.
> #
> v="$v $(quote_words 'pkg b 1' 'pkg b 2' … 'pkg b n+1')" &&
>
> # Finally, let the "eval" command parse and invoke it:
> #
> eval "$v" &&
>
>
> That quote_words function might look like this:
>
>
> ### The function quote_words starts here.
> quote_words()
> (
> # Outputs a (part of) a command line resembling a 'simple
> # command' to be used by a POSIX-compliant shell.
> #
> # Usage:
> #
> # quote_words words to be quoted...
> #
> # exit code:
> # 0, if the command line has been successfully constructed,
> # !=0, if the function failed to construct the command line.
> #
> # Examples:
> #
> # Put a word list resembling a command invocation into a command
> # line:
> #
> # if command_line="$(
> # quote_words program with parameters
> # )"
> # then
> # # "eval" it:
> # eval " $command_line"
> #
> # # or pass it to a new shell:
> # sh -c -- "$command_line" sh
> #
> # # or pass it to "su":
> # su -- - a_user -c -- "$command_line" -su
> #
> # # or use it remotely:
> # ssh us...@remote.host.example " $command_line"
> #
> # else
> # # failed to compute the command line.
> # fi
> #
> #
> # Store the shell positional parameters in a variable and
> # restore them later:
> #
> # if args="$(quote_words "$@")"
> # then
> # # the positional parameters may be changed for any purpose
> # # ...
> #
> # # restore the positional parameters:
> #
> # eval "set -- $args"
> # else
> # # failed to save the positional parameters.
> # fi
>
> # "wordsep" contains a separator used for the list of the quoted
> # words. The first need not to be preceded by a separater:
>
> wordsep=
> for word
> do
> # "$wordsep" separates each word (except the first one) from
> # its precedessor:
> printf '%s' "$wordsep"
> if test -z "$word"
> then
> # The word is empty. Then the result is "''":
> printf '%s' "''"
> else
> # The word is not empty.
> while
> {
> prefix="${word%%\'*}"
> word="${word#"$prefix"}"
> # "$prefix" is the longest prefix of "$word", that does
> # not contain any apostrophes; it is removed from "$word".
> # Conclusion: "$word" is either empty or starts with an
> # apostrophe.
> if test -n "$prefix"
> then
> # "$prefix" consists of one or more characters. Put
> # them in between of two apostrophes:
> printf '%s' \'"${prefix}"\'
> fi
> test -n "$word" &&
> {
> # "$word" is not empty. Conclusion: "$word" starts with
> # one or more apostrophes.
> apostr="${word%%[!\']*}"
> # "$apostr" ist the longest beginning part of "$word",
> # that contains apostrophes only.
> if test -n "${apostr#"'"}"
> then
> # There are at least 2 apostrophes: Put it in between
> # of two double quotes:
> printf '"%s"' "${apostr}"
> else
> # There is 1 apostrophe: Precede it by a backslash:
> printf '\\%s' "${apostr}"
> fi
> # Remove the beginning apostrophes from "$word":
> word="${word#"$apostr"}"
> # If nothing is left, we are done:
> ${word:+:} false
> }
> }
> do
> :
> done
> fi
> # All following words (except the first one) have to be
> # separated from their precedessor with a blank:
> wordsep=' '
> done
> printf '\n'
> )
> ### The function quote_words ends here.
>
>
> Of course I won't want to write this function from scratch in every
> shell script where it is to be used. Therefore I put it in a file,
> say "~/shell-lib/quote_words.sh". Then, if I like to use it in a
> shell script, I just do
>
> . ~/shell-lib/quote_words.sh
>
> in that shell script to have that function defined. It's far more
> convenient than to be hit by any exploit.
>
>
> Look again at the example above, annotated by some comments on the
> value of the variable v:
>
> v="$(quote_words sudo apt install -y)" &&
> #
> # Now the variable v has got the contents
> #
> # 'sudo' 'apt' 'install' '-y'
> #
> # Note, how the 4 parameters have been quoted. (That will be
> # needed by the "eval" command, below.)
> # Here's that comment for packages related to a.
> #
> # Note, how the package names are glued with a blank character
> # to the end of the existing value of the variable v:
> # v="$v $(quote_words …)"
> #
> v="$v $(quote_words 'pkg a 1' 'pkg a 2' … 'pkg a n+1')" &&
> #
> # Now the variable v has got the contents:
> #
> # 'sudo' 'apt' 'install' '-y' 'pkg a 1' 'pkg a 2' … 'pkg a n+1'
> # Here's that comment for packages related to b.
> #
> v="$v $(quote_words 'pkg b 1' 'pkg b 2' … 'pkg b n+1')" &&
> #
> # Now the variable v has got the contents (shown wrapped for
> # better readability):
> #
> # 'sudo' 'apt' 'install' '-y' \
> # 'pkg a 1' 'pkg a 2' … 'pkg a n+1' \
> # 'pkg b 1' 'pkg b 2' … 'pkg b n+1'
> #
> # Note, how the parameters have been quoted. (Thus they will
> # be protected from being broken apart by the "eval" command.)
>
> eval "$v" &&
> #
> # This command line has got the contents (shown wrapped
> # because of its length):
> #
> # ''\''sudo'\'' '\''apt'\'' '\''install'\'' '\''-y'\'' \
> # '\''pkg a 1'\'' '\''pkg a 2'\'' … '\''pkg a n+1'\'' \
> # '\''pkg b 1'\'' '\''pkg b 2'\'' … '\''pkg b n+1'\'
> #
> # Yes, that's terrible for humans but perfect for the shell.
> #
> # The shell will parse that command line, thus invoking the
> # "eval" builtin command with the following single literally
> # parameter (shown wrapped because of its length):
> #
> # 'sudo' 'apt' 'install' '-y' \
> # 'pkg a 1' 'pkg a 2' … 'pkg a n+1' \
> # 'pkg b 1' 'pkg b 2' … 'pkg b n+1'
> #
> # The "eval" builtin command will parse its parameter like
> # a shell command line -- that's the purpose of the "eval"
> # command -- and finally invoke the equivalent of the following
> # command line:
> #
> # 'sudo' 'apt' 'install' '-y' \
> # 'pkg a 1' 'pkg a 2' … 'pkg a n+1' \
> # 'pkg b 1' 'pkg b 2' … 'pkg b n+1'
>
> This is the difference between using the "quote_words" function and
> merely gluing parameters together with white space: "quote_words"
> will proper quote each of the arguments before gluing them
> together. That allows and mandates the resulting string to be
> parsed by the "eval" builtin command. And of course that string is
> not to be split at white space prior to be passed to the "eval"
> command, therefore it shall be quoted. In the example above, that
> is
>
> eval "$v"
>
> Because the argument to the "eval" command has been quoted, it will
> not be broken apart at white space as would be the case with an
> unquoted reference to the variable.


Click here to read the complete article
Re: How to simulate array variables in the POSIX shell

<sqhqla$n1r$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=4833&group=comp.unix.shell#4833

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: How to simulate array variables in the POSIX shell
Date: Wed, 29 Dec 2021 15:17:13 +0100
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <sqhqla$n1r$1@dont-email.me>
References: <8f390ffa-4f1e-42e0-9d31-16ce30d8fda9n@googlegroups.com>
<sq7io1$ii1$1@dont-email.me> <83tuewgsih.fsf@helmutwaitzmann.news.arcor.de>
<sq8nde$kij$1@dont-email.me>
<4b50e439-b736-4947-9b85-f2a3ec665f33n@googlegroups.com>
<83h7avg8c1.fsf@helmutwaitzmann.news.arcor.de>
<c81dfbad-6ff1-4b24-bdfb-0d5627c392cdn@googlegroups.com>
<83v8z9d4op.fsf@helmutwaitzmann.news.arcor.de>
<7e7e5d71-26a6-40f0-8ec1-4aecf7f3f6a5n@googlegroups.com>
<838rw4bed3.fsf_-_@helmutwaitzmann.news.arcor.de>
<3adb7130-3728-4e0e-9748-9d70d8a9969cn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 29 Dec 2021 14:17:14 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="a0a25d945309b0070dc3c38fc8cc70dd";
logging-data="23611"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/bfZehkGc7RijAtbrB4YqQ"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:5yV9PdOrJx2iUOxBkHt4JLBFcwg=
In-Reply-To: <3adb7130-3728-4e0e-9748-9d70d8a9969cn@googlegroups.com>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Wed, 29 Dec 2021 14:17 UTC

On 29.12.2021 13:14, hongy...@gmail.com wrote:
> On Wednesday, December 29, 2021 at 6:42:40 AM UTC+8, Helmut Waitzmann
> wrote:
>> [ 325 lines of quoted text and code deleted ]

(It's neither necessary [in Usenet] nor welcome to quote all
the text if you don't refer to it substantially, especially if
the quote is 100+ times as large as your own "contribution".)
(It's also Netiquette [in Usenet] to break long lines at word
boundaries after around 72-76 characters of text, although in
Google epoch that seems to have partly passed out of mind.)

> I read through your long reply and didn't quite understand it.

I only skimmed through Helmut's text and think he was addressing
the question and the method how to handle array type information
with spaces and whatnot in the data values through flat variable
structures by adding quoting. Doing that in the right way is not
trivial, thus the lengthy code.

Using arrays (if your shell supports them) in the first place is
preferable if only to avoid complex (potentially error-prone) code.

> But I think for a problem with a small probability of occurrence,
> is it necessary to do such complex processing and consideration?

I think your thinking is flawed here. (Or maybe it's just how you
formulated it?) - It's not about "probabilities". If you write
code that's not robust it may fail at any time, just depending
on the data.

And failing code includes potential loss of data or security
issues, depending on the software context. It may be irrelevant
for you on your private machine at home. Though I am shuddering
if one is responsible for the systems of a company and for other
folks that is relying on your code.

Janis

>
> HZ
>

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor