Thread overview
How do I get the output of the time bash command?
Jan 27, 2021
Anthony
Jan 27, 2021
Arafel
Jan 27, 2021
Anthony
Jan 27, 2021
Marcone
Jan 27, 2021
bachmeier
Jan 27, 2021
Ferhat Kurtulmuş
January 27, 2021
I'm trying to read the timed output of a pipeShell command but it only results in empty output.

Does anyone know why this is?


```
    auto p = pipeShell("time ls");

    foreach(str; p.stdout.byLine) {
        writefln("%s",str);
    }
```
January 27, 2021
On 27/1/21 10:35, Anthony wrote:
> 
> I'm trying to read the timed output of a pipeShell command but it only results in empty output.
> 
> Does anyone know why this is?
> 
> 
> ```
>      auto p = pipeShell("time ls");
> 
>      foreach(str; p.stdout.byLine) {
>          writefln("%s",str);
>      }
> ```

I'm not sure why you get an empty output, you should get at least the `ls` output unless it's an empty directory (or one with only "dot" files).

However, in any case `time` returns the timing information through `stderr`, not `stdout`[1]. You can try [2,3] (untested):

```
auto p = pipeShell("time ls", Redirect.stderrToStdout);
```

Best,

A.

[1]: https://linux.die.net/man/1/time
[2]: https://dlang.org/library/std/process/pipe_shell.html
[3]: https://dlang.org/library/std/process/redirect.html
January 27, 2021
On Wednesday, 27 January 2021 at 09:35:21 UTC, Anthony wrote:
>
> I'm trying to read the timed output of a pipeShell command but it only results in empty output.
>
> Does anyone know why this is?
>
>
> ```
>     auto p = pipeShell("time ls");
>
>     foreach(str; p.stdout.byLine) {
>         writefln("%s",str);
>     }
> ```

writeln(executeShell("time ls")[1]);
January 27, 2021
On Wednesday, 27 January 2021 at 09:35:21 UTC, Anthony wrote:
>
> I'm trying to read the timed output of a pipeShell command but it only results in empty output.
>
> Does anyone know why this is?
>
>
> ```
>     auto p = pipeShell("time ls");
>
>     foreach(str; p.stdout.byLine) {
>         writefln("%s",str);
>     }
> ```

Probably, just getting a string output is not enough. It looks a little outdated, but there is this library:

https://code.dlang.org/packages/dexpect
January 27, 2021
On Wednesday, 27 January 2021 at 09:58:25 UTC, Marcone wrote:
> On Wednesday, 27 January 2021 at 09:35:21 UTC, Anthony wrote:
>>
>> I'm trying to read the timed output of a pipeShell command but it only results in empty output.
>>
>> Does anyone know why this is?
>>
>>
>> ```
>>     auto p = pipeShell("time ls");
>>
>>     foreach(str; p.stdout.byLine) {
>>         writefln("%s",str);
>>     }
>> ```
>
> writeln(executeShell("time ls")[1]);

This is the correct answer. The documentation for pipeShell says

> Like the functions they wrap, these functions return immediately, leaving the child process to execute in parallel with the invoking process. It is recommended to always call wait on the returned ProcessPipes.pid, as detailed in the documentation for wait.
January 27, 2021
On Wednesday, 27 January 2021 at 09:58:18 UTC, Arafel wrote:
> On 27/1/21 10:35, Anthony wrote:
>> 
>> I'm trying to read the timed output of a pipeShell command but it only results in empty output.
>> 
>> Does anyone know why this is?
>> 
>> 
>> ```
>>      auto p = pipeShell("time ls");
>> 
>>      foreach(str; p.stdout.byLine) {
>>          writefln("%s",str);
>>      }
>> ```
>
> I'm not sure why you get an empty output, you should get at least the `ls` output unless it's an empty directory (or one with only "dot" files).
>
> However, in any case `time` returns the timing information through `stderr`, not `stdout`[1]. You can try [2,3] (untested):
>
> ```
> auto p = pipeShell("time ls", Redirect.stderrToStdout);
> ```
>
> Best,
>
> A.
>
> [1]: https://linux.die.net/man/1/time
> [2]: https://dlang.org/library/std/process/pipe_shell.html
> [3]: https://dlang.org/library/std/process/redirect.html

Thanks! I think this was the issue.
Turns out that pipeShell uses sh not bash so the "time" program isn't available.
When I printed stderr, it showed me the error.