How would I retrieve the stdout error message of a system/shell command?

Timon Gehr timon.gehr at gmx.ch
Thu Sep 8 11:38:28 PDT 2011


On 09/08/2011 08:21 PM, Justin Whear wrote:
> Good point. It looks like shell throws if the return value is an error code
> (something other than 0 on Posix). It looks like dmd does return an error
> code on failed compilation, so redirecting to stdout won't work. Back to the
> pipes or file redirect then.
>
>
> Christophe wrote:
>
>> Justin Whear , dans le message (digitalmars.D.learn:29380), a écrit :
>>> That'll work if you don't mind normal output being mixed with error
>>> messages.
>>>
>>>
>>> Timon Gehr wrote:
>>>
>>>> On 09/08/2011 07:26 PM, Justin Whear wrote:
>>>>> The Posix solution is to use pipes. Basically, you'll want the parent
>>>>> process to set up a pipe for stderr, fork, then the child process uses
>>>>> the write end of the stderr while the parent reads from the other end.
>>>>> Not sure what the Windoze solution is.
>>>>> Alternatively, the cheap and easy way is to use redirects:
>>>>>
>>>>> system("dmd bla.d 2>error.log");
>>>>>
>>>>> If an error is thrown, read from error.log.
>>>>
>>>> I think the easiest way on a posix system is this:
>>>>
>>>> auto res=shell("dmd bla.d 2>&1");
>>>>
>>>> I haven't tested it tough. What it should do is redirect dmd's stderr to
>>>> stdout, which can then be read.
>>>
>>
>> Well, if shell throws, it will not return, and the output will not be
>> assigned to res.
>

It seems DMD actually writes the error messages to stdout anyways.

This will stop shell from throwing:

auto res = shell("dmd bla.d | cat");





More information about the Digitalmars-d-learn mailing list