From 266fdc217bf4e7f113e4c3b998a96c87dc85c4b9 Mon Sep 17 00:00:00 2001 From: koalaman Date: Mon, 7 Dec 2015 14:23:10 -0800 Subject: [PATCH] Created SC2095 (markdown) --- SC2095.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 SC2095.md diff --git a/SC2095.md b/SC2095.md new file mode 100644 index 0000000..bcab03d --- /dev/null +++ b/SC2095.md @@ -0,0 +1,32 @@ +## Add < /dev/null to prevent ssh from swallowing stdin. + +The same error applies to multiple commands, like `ffmpeg` and `mplayer`. + +### Problematic code: + +```sh +while read -r host +do + ssh "$host" "uptime" +done < hosts.txt +``` + +### Correct code: + +```sh +while read -r host +do + ssh "$host" "uptime" < /dev/null +done < hosts.txt +``` +### Rationale: + +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: + +None. \ No newline at end of file