executeShell doesn't work but system does

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 26 09:02:18 PDT 2016


On 06/26/2016 05:37 PM, Smoke Adams wrote:
> system("cls") works but executeShell doesn't. system is depreciated.

Unsolicited spelling correction: no 'i' in "deprecated".

> What's going on? The docs say that it creates a new process. I simply
> want to clear the console!

`system` directly prints its output, `executeShell` returns it in a 
tuple with the status code. Maybe cls works by printing some specific 
clear code. If so, you have to print the output of the command.

This works with `clear` on Linux which seems to behave similarly to 
Windows' cls:

----
void main()
{
     import std.stdio: write, writeln;
     import std.process: executeShell;
     import std.exception: enforce;
     writeln("A");
     auto r = executeShell("clear");
     enforce(r.status == 0);
     write(r.output);
     writeln("B");
}
----

`wait(spawnShell(...))` is the other suggestion from `system`'s 
deprecation message. It works for `clear`, too:

----
void main()
{
     import std.stdio: writeln;
     import std.process: spawnShell, wait;
     writeln("A");
     wait(spawnShell("clear"));
     writeln("B");
}
----


More information about the Digitalmars-d-learn mailing list