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?
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?
"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 <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?
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 willbe 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".
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.”
Agree.The backslash character at the end of the line on the comment willbe 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".
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"
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.”
Agree.The backslash character at the end of the line on the comment willbe 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".
If I had to have those comments, I suppose this would work:
a='pkg_1'Thank you and all others for the explanation given here. I figured out the following solution based on your above suggestion:
# Here's that comment...
b='pkg_2'
sudo ... "$a" "$b"
------------------= 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
"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 $bIt only works if neither of the package names contains white space.
------------------= end =--------------
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.
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?
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.
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
"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.
As for other operating systems, I'm not sure, but I believe it'sI agree, but I believe, it's good practice to carefully design
not a good practice to use whitespace in the naming of software
packages on a system.
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.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 73:33:32 |
Calls: | 6,657 |
Calls today: | 3 |
Files: | 12,203 |
Messages: | 5,332,384 |
Posted today: | 1 |