mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated Template (markdown)
22
Template.md
22
Template.md
@@ -1,20 +1,32 @@
|
|||||||
## (Message goes here, use `code` and *foo* wisely)
|
## Add < /dev/null to prevent ssh from swallowing stdin.
|
||||||
|
|
||||||
|
The same error applies to multiple commands, like `ffmpeg` and `mplayer`.
|
||||||
|
|
||||||
### Problematic code:
|
### Problematic code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# Simple example of problematic code
|
while read -r host
|
||||||
|
do
|
||||||
|
ssh "$host" "uptime"
|
||||||
|
done < hosts.txt
|
||||||
```
|
```
|
||||||
|
|
||||||
### Correct code:
|
### Correct code:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# Simple example of above code, only fixed
|
while read -r host
|
||||||
|
do
|
||||||
|
ssh "$host" "uptime" < /dev/null
|
||||||
|
done < hosts.txt
|
||||||
```
|
```
|
||||||
### Rationale:
|
### Rationale:
|
||||||
|
|
||||||
(An explanation of why the code is problematic and how the correct code is an improvement)
|
Commands that process stdin will compete with the `read` statement for input. This is especially tricky for commands you wouldn't expect reads from stdin, like `ssh .. uptime`, `ffmpeg` and `mplayer`.
|
||||||
|
|
||||||
|
The most common symptom of this is a `while read` loop only running once, even though the input contains many lines. The is because the rest of the lines are swallowed by the offending command.
|
||||||
|
|
||||||
|
To refuse such commands input, redirect their stdin with `< /dev/null`.
|
||||||
|
|
||||||
### Exceptions:
|
### Exceptions:
|
||||||
|
|
||||||
(Cases where the user may choose to ignore this warning, if any.)
|
None.
|
Reference in New Issue
Block a user