2.068.0 std.process.executeShell how to set the shell?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 2 22:06:58 PDT 2015


On Wednesday, September 02, 2015 15:28:35 albatroz via Digitalmars-d-learn wrote:
> On Wednesday, 2 September 2015 at 01:46:18 UTC, Jonathan M Davis
> wrote:
> > On Wednesday, 2 September 2015 at 01:26:23 UTC, Jonathan M
> > Davis wrote:
> > [snip]
> >
> > https://issues.dlang.org/show_bug.cgi?id=15000
> >
> > - Jonathan M Davis
>
> Thank you for your reply and for the bug report.
>
> Looking at your suggestions I do not fully understand/agree.
>
> executeShell has been one of the easiest ways for us to interact
> with the shell, all other functions require more work to
> implement. Being able to define the shell to be used is something
> that makes sense across all functions in std.process.
>
> Honestly being able to set a different variable name instead of
> SHELL would make more sense. It would not take the default shell
> from the user thus using /bin/sh and would allow us to change it
> if desired with a different value. The change would be minimal
> across code and documentation.
>
> But that's just my suggestion, I'm not a library developer. I
> understand that all variants may have their own issues, but
> taking away from the developer the power to make decisions also
> doesn't seem a good option.

Oh, it would be nice to be able to specify the shell (and feel free to add a
comment on that to the bug report). It's just that spawnShell and
executeShell already have a lot of parameters, and it would kind of ugly to
add another. Not to mention, it would probably have to go on the end after
several parameters with default arguments, forcing you to list all of the
default arguments explicitly just so that you can list the shell. So, while
in principle, listing the shell, would be nice. In practice, I'm not sure
how to add it cleanly to the current API, and it _is_ possible to use
execute or spawnProcess to run a specific shell.

> Anyway I would like to ask if you can help to understand why this
> simple example using execute fails?
>
>    auto outputDiff = execute ( ["bash","echo"] );
>    writeln (outputDiff.output);
>
> Output:
> /bin/echo: /bin/echo: cannot execute binary file
>
> At the moment some of the uses I had are not working and I'm not
> able to implement any workaround.

That's because you're doing the equivalent of

bash echo

on the command line, and if you try that, you'll notice that you get the
same error there. What you need to do instead, per the bash man page, is to
run bash with the -c flag, where -c takes the command you want to run. So,
you can do something like

auto result = execute(["bash", "-c", "echo world"]);

though what gets printed doesn't seem to appear on the command line any more
than if you echoed with executeShell. If you want that, you'd have to use
spawnShell or spawnProcess, e.g.

auto result = wait(spawnProcess(["bash", "-c", "echo world"]));

But if you don't care about the commands appearing on the command line, then
execute works just fine. In either case, the key thing that you're missing
is -c.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list