mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2067 (markdown)
31
SC2067.md
Normal file
31
SC2067.md
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
## Missing ';' or + terminating -exec. You can't use |/||/&&, and ';' has to be a separate, quoted argument.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
find . -type f -exec shellcheck {} | wc -l \;
|
||||||
|
find . -exec echo {} ;
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
find . -type f -exec sh -c 'shellcheck "$1" | wc -l' -- {} \;
|
||||||
|
find . -exec echo {} \;
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
`find -exec` is still subject to all normal shell rules, so all shell features like `|`, `||`, `&` and `&&` will apply to the `find` command itself, and not to the command you are trying to construct with `-exec`.
|
||||||
|
|
||||||
|
`find . -exec foo {} && bar {} \;` means run the command `find . -exec foo {}`, and if find is successful, run the command `bar "{}" ";"`.
|
||||||
|
|
||||||
|
To instead go through each file and run `foo file && bar file` on it, invoke a shell that can interpret `&&`:
|
||||||
|
|
||||||
|
find . -exec sh 'foo "$1" && bar "$1"' -- {} \;
|
||||||
|
|
||||||
|
You can also use find `-a` instead of shell `&&`:
|
||||||
|
|
||||||
|
find . -exec foo {} \; -a -exec bar {} \;
|
||||||
|
|
||||||
|
This will have the same effect (`-a` is also the default when two commands are specified, and can therefore be omitted).
|
||||||
|
|
||||||
|
### Contraindications
|
||||||
|
|
||||||
|
None
|
Reference in New Issue
Block a user