grep and sed ignore stdin if you provide a filename parameter:
$ echo foo > test.txt
$ echo foobar | grep f test.txt
foo
$ echo foobar | sed -e 's/foo/bar/' test.txt
bar
Is this established best practice? If so, why?
|
A program knows whether files are specified on its command line. It cannot know whether there is any input available on stdin (other than by trying to read it), and if there is input, there is no way for the program to know whether that input was intended for it. So the only sensible option is to have a clear rule, based on how the program was invoked (command line arguments, environment variables, configuration files, …), to decide whether it will read from stdin or not. For example, typical text utilities ( Consider a shell snippet like
You need to know whether |
|||
|
|
|
The Answer I found is that's how it is designed. If you want to read from both stdin and file use
|
||||
|
|
foo filename -for both filename and stdin(the-), only read from stdin if you are told to OR no filename is provided. – cjh Dec 30 '12 at 1:31