D: How do I pipe (|) through three programs using std.process?
kdevel
kdevel at vogtner.de
Sun Nov 19 11:37:23 UTC 2023
On Saturday, 18 November 2023 at 18:09:53 UTC, BoQsc wrote:
> Latest iteration on this thread.
>
> Limitations:
> * pipes through two programs.
> * very verbose, hard to use.
>
What exactly are you trying to achieve?
> ```
> import std;
> import std.process;
>
> version (Windows) { enum Find = "find"; }
> version (Posix) { enum Find = "grep"; }
>
> void pipeTo(Pipe p, string nextprogram){
> spawnShell(nextprogram, p.readEnd, stdout);
> ```
If you allow invoking the shell from within your program why
don't you use one of its facilities, i.e. the shell's pipe
operator `|`, in the first place? `spawnShell` does not execute
`nextprogram` but it executes it under the shell.
> ```
> }
>
> auto program(string name){
> Pipe p = std.process.pipe;
> spawnShell(name, stdin, p.writeEnd);
> return p;
> }
>
> void main()
> {
> program("echo HelloWorld").pipeTo(nextprogram: Find ~ `
> "HelloWorld"`);
Have I missed the advent of named function arguments in D?
> ```
> }
> ```
Your whole program shrinks considerably:
```
import std;
import std.process;
version (Windows) { enum Find = "find"; }
version (Posix) { enum Find = "grep"; }
void main()
{
spawnShell("echo HelloWorld | " ~ Find ~ `
"HelloWorld"`).wait;
}
```
More information about the Digitalmars-d-learn
mailing list