Opening a File via Pipes, Redirection and direct access and its
Opening a File via Pipes, Redirection and direct access and its
am 15.04.2008 00:11:17 von Fox
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
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
Re: Opening a File via Pipes, Redirection and direct access and its effect on Command
am 15.04.2008 00:37:30 von mallin.shetland
Fox scrisse:
> However, when I run time command (time command2
> 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.
Re: Opening a File via Pipes, Redirection and direct access and its effect on Command
am 15.04.2008 05:19:18 von Maxwell Lol
Fox writes:
> However, when I run time command (time command2
> 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?
Unix commands have always been fairly fast, except when the input file
is very large. A general principle for Unix users is - don't optimize
unless you notice a significant performance penalty.
If the command takes less than second, it takes that long to type the
command in. Spend your time and energy adding things. The real issue is
* Which command requires less characters when you type?
*) Which comand will let you solve the problem faster?
Sometimes it's better to go with a program you know that may
take an extra 10 characters to type, than to learn a new
utility. This depends on how often you may need to use it again.
*) If this is going into a script that others have to maintain -
which command is clearer?
*) If you are going to show this to experts, they will find
nits - to improve the script. That's what we do.
If you want to be prooud of your script, you can polish it up some
more. But many scripts are throw-aways. As I said - if it takes a
long tine to run, then trying to speed it up is a good idea. In
general, 90% of the time is spent in 10% of the code/script.
Worry about that 10%.
Having said that, there are some subtle differences
command2 < file
command2 file
The second may not work for all programs/scripts. A good script woould
let you use it either way. Some don't.
And of course the difference between
command1 fiulename | command2
and
command2 < filename
is that in the first one, "command1" is executed.
Re: Opening a File via Pipes, Redirection and direct access and its effect on Command
am 15.04.2008 10:22:10 von PK
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.