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

    From Kenny McCormack@21:1/5 to hongy...@gmail.com on Sat Dec 25 14:43:09 2021
    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) --

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From hongyi.zhao@gmail.com@21:1/5 to All on Sat Dec 25 06:28:16 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dan Espen@21:1/5 to hongy...@gmail.com on Sat Dec 25 12:00:49 2021
    "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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Keith Thompson@21:1/5 to Dan Espen on Sat Dec 25 15:29:37 2021
    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 */

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dan Espen@21:1/5 to Keith Thompson on Sat Dec 25 19:21:37 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dan Espen@21:1/5 to Helmut Waitzmann on Sat Dec 25 22:26:38 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From hongyi.zhao@gmail.com@21:1/5 to Dan Espen on Sat Dec 25 20:53:37 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From hongyi.zhao@gmail.com@21:1/5 to hongy...@gmail.com on Sat Dec 25 20:58:55 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From hongyi.zhao@gmail.com@21:1/5 to Helmut Waitzmann on Sun Dec 26 03:48:54 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tavis Ormandy@21:1/5 to hongy...@gmail.com on Sun Dec 26 17:19:58 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Tavis Ormandy on Sun Dec 26 23:56:44 2021
    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.


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From hymie!@21:1/5 to who on Mon Dec 27 13:27:40 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From hongyi.zhao@gmail.com@21:1/5 to Helmut Waitzmann on Mon Dec 27 23:46:41 2021
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)