Invoking complex shell commands via Process? (How to use "exec" in DartVM?)

1,040 views
Skip to first unread message

Kevin Moore

unread,
Feb 18, 2013, 7:01:00 PM2/18/13
to General Dart Discussion
I'd like to redirect a file to standard input

   git write-tree < treefile.txt

Or maybe send stdin of one process to stdout of another using pipe.

  more foo.txt | grep special_value

In both cases, I naively split everything after the first command into an String list and passed things into Process.run and/or start.

Not happy with the results.

Node has something that looks like Process.start -> child_process.spawn(command, [args], [options])

I think Alexi may have been looking for something like -> child_process.exec(command, [options], callback)

The example in the node docs: exec('cat *.js bad_file | wc -l'...

That's what I'm looking for.

What's the likelihood of getting 

  static Future<Process> exec(String command, [ProcessOptions options])

in Process?

Kevin Moore

unread,
Feb 18, 2013, 7:22:30 PM2/18/13
to mi...@dartlang.org
A work around:

On unix, one can pass the full command to "bash -c"

For Example

  Process.run('bash', ['-c', 'git mktree < $treeFile']);

This does what I want quite nicely.

Issue 1705 talks about this problem with a similar work-around for Windows.

Not sure if generalizing this cross-platform is straight-forward, although it seems node has tried.

Ladislav Thon

unread,
Feb 19, 2013, 12:51:03 AM2/19/13
to General Dart Discussion


> A work around:
>
> On unix, one can pass the full command to "bash -c"
>
> For Example
>
>   Process.run('bash', ['-c', 'git mktree < $treeFile']);

This is not a workaround. This is the only way to do it. Redirections or pipes are features of the shell, so if you want to use them, you have to use a shell.

Maybe the documentation could say that Process.start doesn't invoke a shell, but that could confuse some people that don't know what the hell a shell is (like, you know, Windows users :-) ).

> Not sure if generalizing this cross-platform is straight-forward, although it seems node has tried.

My naive try would be:

Process.shell(String cmdLine) {
  if (windows) {
    return Process.start("cmd", ["/c", cmdLine]);
  } else {
    return Process.start("sh", ["-c", cmdLine]);
  }
}

I'm sure it's more complicated than that, but I'd guess not much.

LT

Ladislav Thon

unread,
Feb 19, 2013, 12:56:49 AM2/19/13
to General Dart Discussion


> Process.shell(String cmdLine)

Oh, I see that there are different methods Process.start and Process.run. In that case, I would use a different name, something like Process.startShell. Or maybe define both Process.startShell and Process.runShell.

I agree they would be useful.

LT

Reply all
Reply to author
Forward
0 new messages