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

Re: shellcheck, bashism's and one liners.



On 17/03/2024 23:56, Greg Wooledge wrote:
On Sun, Mar 17, 2024 at 11:14:56PM +0700, Max Nikulin wrote:

args() { printf '%s\0' a b c d; }
args | xargs -0 sh -c 'count() { echo $#; }; count "$@"' sh-count

It would be easier in the case of script file instead of shell function. An
assumption is that all arguments may be passed through command line.

Tim's assumption here is that he can write a function which emits a
stream of whitespace-separated words, and use this safely in an unquoted
command substitution.

     count $(args)

I'm guessing "count" is a stand-in for something more complex, but $(args)
is pretty much exactly what he wants to do in his real script.

A rudimentary variant of xargs may be implemented in shell. I have no idea how to get null-delimited list from apt-get.

sh-xargs() {
        declare -a args
        mapfile -t args
        "$@" "${args[@]}"
}
count () {
        echo $#
}
# shellcheck disable=SC2016
apt-get indextargets --format '$(FILENAME)' 'Identifier: Packages' |
    sh-xargs count

I find it acceptable to suppress warnings from static code analyzers at specific lines. As to variables that sometimes expands to nothing, perhaps their values may be collected to arrays. However it is the point when a full-fledged programming language becomes a better alternative.


Reply to: