Reading into the output of a long running shellExecute
helxi
brucewayneshit at gmail.com
Tue Nov 13 03:45:31 UTC 2018
On Saturday, 10 November 2018 at 15:54:07 UTC, JN wrote:
> On Saturday, 10 November 2018 at 15:05:38 UTC, helxi wrote:
>> Hi. I have not done any multi-threaded programming before.
>> What I basically want is to read into the output of a long
>> shellExecute function each second.
>>
>> In details, I am calling shellExecute("pkexec dd
>> if=/path/to/file of=/dev/sdx status=progress && sync");
>> It's a long running process and dd command prints how many
>> bytes it has written in stdout continuously. I want to read
>> and parse this output each second. How should I proceed?
>
> shellExecute won't work, because it waits for the process to
> end before moving on.
>
> I believe https://dlang.org/phobos/std_process.html#pipeProcess
> should do what you want. It returns a ProcessPipes object which
> has stdout, from which you should be able to read.
Okay I looked it up but now I have another question
1 import std.process;
2 import std.stdio;
3 import std.range;
4 import std.string;
5
6 // pkexec dd
if=/run/media/user1101/portable_drive/software/os/manjaro-kde-18.0-stable-x86_64.iso of=/dev/sdd bs=4M status=progress 2>&1 && sync
7
8 void main() {
9 auto pipe = pipeShell("bash", Redirect.stdout |
Redirect.stderr | Redirect.stdin);
10 pipe.stdin.writeln(
11 "pkexec dd
if=/run/media/user1101/portable_drive/software/os/manjaro-kde-18.0-stable-x86_64.iso of=/dev/sdd bs=4M status=progress 2>&1 && sync"
12 );
13 pipe.stdin.flush();
14 pipe.stdin.close();
15
16 foreach (line; pipe.stdout.byLineCopy)
17 foreach (word; line.split)
18 writeln(word);
19 }
What I want is to instantly print whatever dd prints to the
stdout. (I made dd to redirect output to stdout with 2>&1
already). But the problem is, the pipe waits until dd finishes
writing to stdout. How can I make the pipe not wait for dd to
finish?
}
More information about the Digitalmars-d-learn
mailing list