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

shellcheck, bashism's and one liners.



Hi,

I've been cleaning up some bash scripts and, where possible, addressing
things reported by shellcheck.

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[@]}")"

I have a number of other places where I'm relying on a variable
containing a number of space separated items that I DO want word
splitting and so the shellcheck warning is incorrect and I either
suppress it or find a fix similar to the above.

In almost all other cases, the space separated items cannot, even in
theory, contain a rogue space, so suppressing the warning is fine but
the above one could, therefore I wanted to find a proper fix.

Is there a one-liner way to make shellcheck happy on the count line
below (other than # shellcheck disable=SC2046)?

args() { echo a b c d; }
count() { echo $#; }
count $(args)

Obviously, any correct solution should output 4

Thanks.


For background, this script builds a debian image and this part of the
script checks to see if any of the packages in the minimal image have
been updated since it was built and so it needs rebuilding.

As an optimization, this script first checks the timestamp on the
Packages files that are used to build the image, if they're older than
the image then it's not possible for there to be a newer package and so
I don't need to do the checks on individual packages.



Reply to: