From b96c56585bf19dfa6a195c0ad72ba82ab3cbf801 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 3 May 2020 12:08:29 -0700 Subject: [PATCH] Created SC2259 (markdown) --- SC2259.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 SC2259.md diff --git a/SC2259.md b/SC2259.md new file mode 100644 index 0000000..1972ffa --- /dev/null +++ b/SC2259.md @@ -0,0 +1,33 @@ +## This redirection overrides piped input. To use both, merge or pass filenames. + +### Problematic code: + +```sh +gzcat yesterday.log.gz | grep "$USER" < today.log +``` + +### Correct code: + +```sh +# Specify non-piped inputs as filenames +gzcat yesterday.log.gz | grep "$USER" - today.log + +# Or merge multiple inputs into a single stream +{ gzcat yesterday.log.gz; cat today.log; } | grep "$USER" +``` + +### Rationale: + +A process only has a single standard input stream. Pipes and input redirections both overwrite it, so you can't use both at the same time. If you try, the redirection takes precedence and the input pipe is closed. + +Many commands support specifying multiple filenames, where one can be stdin (canonically by specifying `-` as a filename, or alternatively by using `/dev/stdin`). In these cases, you can rewrite the command to use one piped input, and as many extra files (or process substitutions) as you want. + +For commands that only process a single input stream (like `tr`), you can also concatenate multiple commands or files into a single stream using a `{ command group; }` as in the example. + +### Exceptions: + +None. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file