shellscalacommand-lineprocessscala-script

Execute subcommand in Scala (script) and let it "take over"


I'd like to create a dummy script (with the prospect of writing a real life script) that invokes for example the Python interactive interpreter from within a Scala process and lets the user fully interact with the subprocess; i.e. the stdin/stdout/stderr of the child process should be connected to those of the parent (Scala) process. I've tried using the following to no avail:

#!/usr/bin/env scala -savecompiled

import sys.process._

stringToProcess("python").run(BasicIO.standard(connectInput = true)).exitValue

however, while it does seem to successfully run a python subprocess, the only interaction I get to have with it is Ctrl-C:

~$ ./scalashelltest.scala
foo

^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyboardInterrupt

If press Ctrl-C immediately, I don't even get that output:

~$ ./scalashelltest.scala
^C~$ 

Any idea why this is happening and how to make it work as expected?


Solution

  • You aren't giving python a (pseudo-)tty. You are just giving it stdin (and possibly stdout).

    So python is operating in a non-interactive mode.

    Running python with the -i flag will force it to use prompts even without a tty but the more correct fix is likely to find a scala/java library which can create a (pseudo-)tty and run an application in it.