From: Fox on
Hi,

After reading bash man page I came to following conclusion among three
commands (they produces the same output):

command1 filename | command2
command2 < filename
command2 filename

i) pipe - Command command1 executed by by opening filename and its
output is given to command2 command. So the output of each command in
the pipeline is connected via a pipe to the input of the next command.

ii) redirection - Filename is opened before executing command2 command
using a shell redirection facility

iii) direct open filename - Filename is directly open by command2
command

However, when I run time command (time command2 <filename) on all of
the above I'm getting same output. I'm confused about the real
differences among the above 3 commands. Do I need to consider time
factor (command execution factor)? If not then what are the *real
differences among* three commands?

TIA

Fox
From: mallin.shetland on
Fox scrisse:

> However, when I run time command (time command2 <filename) on all of
> the above I'm getting same output. I'm confused...


time is a /sui/ /generis/ command; its parameter is another
command. On the standard output there is the output of the
latter and the output of time is on the standard error.


From: pk on
Fox wrote:

> Hi,
>
> After reading bash man page I came to following conclusion among three
> commands (they produces the same output):
>
> command1 filename | command2
> command2 < filename
> command2 filename

Unless "command1" is "cat", it's unlikely that all the above produce the
same output.

You could include also

command1 < filename | command2

in the above list.

> ii) redirection - Filename is opened before executing command2 command
> using a shell redirection facility
>
> iii) direct open filename - Filename is directly open by command2
> command

There are some differences.

command2 < filename
command2 filename

These are different because in the first case filename is opend by the
shell, which connects the stdin of command2 to filename. command2 is not
aware that it's reading from a file. It just reads from stdin, as if the
user were typing at the leyboard.
In the second case, filename is opened by command2 itself. So command2 is
aware that it's reading from a file, and knows its name. In this case,
command2 can read from stdin too at the same time.
Note that not all programs support both syntaxes (see eg tr).

For an example, see eg:

$ cat file
BLAH
$ awk '{print FILENAME}' file
file
$ awk '{print FILENAME}' < file
-

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.