Shellcheck: SC2126: False warning

Created on 15 Apr 2020  路  5Comments  路  Source: koalaman/shellcheck

  • My shellcheck version: Online
  • The rule's wiki page does not already cover this.

Here's what shellcheck currently says:

    grep 'saved\|retrieved' ./*/wget*log* | wc -l
    ^-- SC2126: Consider using grep -c instead of grep|wc -l.

Currently, I want to grep multiple files for the matching string and print the matching line, then count it.
Using suggested solution gives individual file count.

grep -c 'saved\|retrieved' ./*/wget*log*
    ./wget-log:0
    ./wget-log.1:1
    ./wget-log:2:1
    ./wget-log.3:1

Using my command.

grep 'saved\|retrieved' ./*/wget*log* | wc -l
    3

Most helpful comment

No need for all that "bash magic", just pipe the contents into grep:

cat ./*/wget*log* | grep -c 'saved\|retrieved'

Updated the Wiki entry.

All 5 comments

When considered the warning put a ignore directive in and continue on.
That's how I treat these messages. It's hard to know what your intentions are.

You can avoid running wc and use only grep and some bash magics, but it's a bit cryptic:

echo $(($(c=($(grep -hc 'saved\|retrieved' ./*/wget*log*)); IFS=+; echo "${c[*]}")))

@brother Yeah, i did that.

Also, i think it's not that hard to know my intentions as this is only a problem for globs, check could be added in source.

Again, just a thought, this isn't something important.

@mnuccioarpae Well, bash magics are good, cryptic or not.

Thank you for the help.

No need for all that "bash magic", just pipe the contents into grep:

cat ./*/wget*log* | grep -c 'saved\|retrieved'

Updated the Wiki entry.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hugovk picture hugovk  路  4Comments

quchen picture quchen  路  3Comments

ghost picture ghost  路  4Comments

koalaman picture koalaman  路  4Comments

helau picture helau  路  4Comments