mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 11:19:45 +08:00
Added correct code for Bourne shells
16
SC2181.md
16
SC2181.md
@@ -14,11 +14,21 @@ fi
|
||||
### Correct code:
|
||||
|
||||
```sh
|
||||
if ! make mytarget;
|
||||
if ! make mytarget
|
||||
then
|
||||
echo "Build failed"
|
||||
fi
|
||||
```
|
||||
For the Solaris 10 Bourne shell:
|
||||
```sh
|
||||
if make mytarget
|
||||
then
|
||||
:
|
||||
else
|
||||
echo "Build failed"
|
||||
fi
|
||||
```
|
||||
|
||||
### Rationale:
|
||||
|
||||
Running a command and then checking its exit status `$?` against 0 is redundant.
|
||||
@@ -39,6 +49,8 @@ To additionally capture output with command substitution: `if ! output=$(mycomma
|
||||
|
||||
This also applies to `while`/`until` loops.
|
||||
|
||||
The default Solaris 10 Bourne shell does not support negating exit statuses with `!`, so `! mycommand` tries to invoke a utility named "!" instead. To test for failure, use `if mycommand; then :; else ...; fi` and `until mycommand; do ...; done`.
|
||||
|
||||
### Exceptions:
|
||||
|
||||
The default Solaris 10 Bourne shell does not support `!` outside of the `test` command (`if ! mycommand; then ...` returns `!: not found`)
|
||||
None.
|
Reference in New Issue
Block a user