[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: shellcheck, bashism's and one liners.



On Sun, 17 Mar 2024, Geert Stappers wrote:

On Sun, Mar 17, 2024 at 09:25:10AM +0000, Tim Woodall wrote:
Hi,

I've been cleaning up some bash scripts

Good


and, where possible, addressing things reported by shellcheck.

Oh,  shellcheck, https://www.shellcheck.net/


I have this one-liner (which works but shellcheck doesn't like the
quoting)

idxsrc="$( newest_file $( APT_CONFIG=${APT_CONFIG} apt-get indextargets --format '$(FILENAME)' 'Identifier: Packages' ))"

SC2016: Expressions don't expand in single quotes, use double quotes for that.
SC2046: Quote this to prevent word splitting.

The first is easy enough to avoid by using backslash instead. But the
second I can't see how to fix as a one-liner.

I can make shellcheck happy by doing it like this:

mapfile -t idxpackages < <( APT_CONFIG=${APT_CONFIG} apt-get indextargets --format \$\(FILENAME\) 'Identifier: Packages' )
idxsrc="$( newest_file "${idxpackages[@]}")"

For what it is worth:

- a shell is nice and good tool
- shellcheck is an afterthought
- my shellcheck experience learnt me that it can't see the difference
  between "dangerous" and "potentionally dangerous"


Thing I'm trying to tell:  Avoid that shellcheck blocks you

Oh, it's not blocking me. Where I know it's OK I've added a
# shellcheck disable=
to silence the warning.

But I've got one more case where it's *possible* that a quoting error
that shellcheck doesn't like could cause things to break.

I have an array of filenames (inc paths). These are all local files
generated by the script so they will never contain a space in the script
controlled bit. But if, for some reason, I decided to do:
mv /mnt/mirror "/mnt/m i r r o r"
then it would get very upset.

But these lists are in an associative array, so it's hard to make
shellcheck safe. I was hoping that an easy answer to my one liner would
give me a clue to this harder case.

I could write some very convoluted code, generating lists on the fly
with eval and then using namerefs in the associative array, but that
would be letting the shellcheck tail wag the scripting dog!


Reply to: