shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

[FR] Add an option to assume that FILES... are one

Open rockandska opened this issue 1 year ago • 2 comments

For new checks and feature suggestions

  • [X ] https://www.shellcheck.net/ (i.e. the latest commit) currently gives no useful warnings about this
  • [X ] I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related

Here's a snippet or screenshot that shows the problem:

$ cat > a.sh <<EOF
TEST=1
EOF
$ cat > b.sh <<EOF               
echo "$TEST"
EOF

Here's what shellcheck currently says:

$ docker run --rm -v "$PWD:/mnt" koalaman/shellcheck:stable -s bash a.sh b.sh               

In a.sh line 1:
TEST=1
^--^ SC2034 (warning): TEST appears unused. Verify use (or export if used externally).

For more information:
  https://www.shellcheck.net/wiki/SC2034 -- TEST appears unused. Verify use (...
$

Here's what I wanted or expected to see:

$ docker run --rm -v "$PWD:/mnt" koalaman/shellcheck:stable -s bash --concat a.sh b.sh
$

Having a --concat option add the ability for people who split src files and concatenate them later to create bin/script to not be hit by those warnings while preserving accurate warnings compared to running shellcheck on the final bin/script

Regards,

rockandska avatar Aug 24 '22 11:08 rockandska

This would be equivalent to:

shellcheck <( cat *.sh )

except that the error reporting would indicate the actual line numbers.

Perhaps another approach would be instead to have something equivalent to the #line directive in C, which affects the reported location of any subsequent errors.

I suggest recognizing two forms:

  • # shellcheck source="filename" line=num column=num where line= and column= are optional, and
  • ==> filename <== being the preamble inserted by the head command.

The second form would allow us to write:

shellcheck <( head -n999999 a.sh b.sh c.sh )

(and yes, if your scripts are longer than a million lines, this will fail; that's a feature not a bug).

kurahaupo avatar Sep 03 '22 04:09 kurahaupo

I suggest recognizing two forms

Having only one header recognized would be sufficient since we could generate the one we want on the fly with awk without relying on head

awk 'FNR == 1 { print "# shellcheck source="FILENAME }; { print }' *.sh

rockandska avatar Sep 12 '22 07:09 rockandska

Yay! awk strikes again! It's a cool little language, and that's an elegant solution.

I was simply offering head as a better-known -- and shorter -- option, particular for novice Shell programmers (the very people who gain the most from using Shellcheck).

kurahaupo avatar Sep 22 '22 02:09 kurahaupo