mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Added correct code for Bourne shells
16
SC2181.md
16
SC2181.md
@@ -14,11 +14,21 @@ fi
|
|||||||
### Correct code:
|
### Correct code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
if ! make mytarget;
|
if ! make mytarget
|
||||||
then
|
then
|
||||||
echo "Build failed"
|
echo "Build failed"
|
||||||
fi
|
fi
|
||||||
```
|
```
|
||||||
|
For the Solaris 10 Bourne shell:
|
||||||
|
```sh
|
||||||
|
if make mytarget
|
||||||
|
then
|
||||||
|
:
|
||||||
|
else
|
||||||
|
echo "Build failed"
|
||||||
|
fi
|
||||||
|
```
|
||||||
|
|
||||||
### Rationale:
|
### Rationale:
|
||||||
|
|
||||||
Running a command and then checking its exit status `$?` against 0 is redundant.
|
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.
|
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:
|
### 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