Shell redirection and explicit file processing code

I am not a native English speaker, so please excuse the awkward title of this question. I just did not know how to formulate this correctly.

I am in the FreeBSD window, and I have a small filter tool written in Cthat reads a list of data through stdinand displays the processed list through stdout. I call it like this: find . -type f | myfilter > /tmp/processed.txt.

Now I want to give my filter a little more information and publish it. The Convention states that tools should allow something like this:find . -type f | myfilter -f - -o /tmp/processed.text

This will force me to write code that is simply not needed, since the shell can do the job, so I try to leave it out.

My question is: can I skip any argument (another, but convention), why the reading and writing of files should be done in my code, and not be delegated to shell redirection?

+5
source share
4 answers

There is nothing wrong. Your filter is an interface similar to, for example c++filt.

You can consider processing files if you want to automatically select the output file based on the name of the input file or if you want special processing for processing several files in one command.

, , . cmd infile outfile, .

+2

. ,

grep foo file | myfilter > /tmp/processed.text

find

find . -type f -exec myfilter {} + > /tmp/processed.text
+1

, , , :

freopen( "filename" , "wb" , stdout );

, printf , . - , , .

+1

. :

myfilter [-f ./infile] [-o ./outfile] #or
myfilter [-o outfile] [filename] #and (the best one)
myfilter [-f file] [-o file] #so, when the input and output are the same file - the filter should working correctly anyway

In a good example, check the command sort. It is usually used as a filter in pipes, but can perform [-o output]and process correctly same input/output problemtoo ...

And why is that good? For example, when you need to run a command from "C" to "fork / exec" and do not want to run a shell to process I / O. In this case, it is much simpler (and faster) execve(.....)with arguments on how to run cmd with a shell shell.

0
source

All Articles