mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2214 (markdown)
38
SC2214.md
Normal file
38
SC2214.md
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
## This case is not specified by getopts.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
while getopts "vr" n
|
||||||
|
do
|
||||||
|
case "$n" in
|
||||||
|
v) echo "Verbose" ;;
|
||||||
|
r) echo "Recursive" ;;
|
||||||
|
n) echo "Dry-run" ;;
|
||||||
|
*) usage;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
while getopts "vrn" n # 'n' added here
|
||||||
|
do
|
||||||
|
case "$n" in
|
||||||
|
v) echo "Verbose" ;;
|
||||||
|
r) echo "Recursive" ;;
|
||||||
|
n) echo "Dry-run" ;;
|
||||||
|
*) usage;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
```
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
You have a `case` statement in a `while getopts` loop that matches a flag that hasn't been provided in the `getopts` option string.
|
||||||
|
|
||||||
|
Either add the flag to the options list, or delete the case statement.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None.
|
Reference in New Issue
Block a user