mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated SC2126 (markdown)
@@ -20,7 +20,7 @@ Instead of:
|
||||
grep foo *.log | wc -l
|
||||
```
|
||||
|
||||
Pipe all the file contents into `grep` (passing the files directly to `grep` causes `-c` to print each file's count separately, rather than the total):
|
||||
You can pipe all the file contents into `grep` (passing the files directly to `grep` causes `-c` to print each file's count separately, rather than the total):
|
||||
|
||||
```sh
|
||||
cat *.log | grep foo -c
|
||||
@@ -30,11 +30,10 @@ cat *.log | grep foo -c
|
||||
|
||||
This is purely a stylistic issue. `grep` can count lines without piping to `wc`.
|
||||
|
||||
Note that in many cases, this number is only used to see whether there are matches (i.e. `== 0`). In these cases, it's better and more efficient to use `grep -q` and check its exit status:
|
||||
Often this number is only used to see whether there are matches (i.e. `== 0`). In these cases it's clearer and more efficient to use `grep -q` and check its exit status:
|
||||
|
||||
```sh
|
||||
if grep -q pattern file
|
||||
then
|
||||
if grep -q pattern file; then
|
||||
echo "The file contains the pattern"
|
||||
fi
|
||||
```
|
||||
@@ -43,4 +42,4 @@ Also note that in `foo | grep bar | wc -l`, wc will mask the exit code of grep b
|
||||
|
||||
### Exceptions
|
||||
|
||||
If you e.g. want to count characters instead of lines, and you actually care about the number and not just whether it's greater than 0, you can [[ignore]] this error.
|
||||
If you find piping to `wc` is clearer in a given situation it's fine to [[ignore]] this error.
|
||||
|
Reference in New Issue
Block a user