How can I provide input for an external team?

As far as I understand, Scala can run system commands and get their output. I am working on writing a web client for the system command that I have, so when they tell me that I need to execute the command, I get the output, based on the output I can give it a command. I want to continue to do this until the user kills the command. I got a little familiar with ProcessIO and I got to the point of getting a command from Scala and getting the output, but how can I enter it?

EDIT: What I'm looking for, I call the command with scala. The command I invoke asks for input, the user provides input to the Scala program, and then passes it to the called program.

Example:

Scala Program → Call System Program → System program asks for a username and password → Scala The program then asks for a username and password from user and user input → Scala, transfers the programs to the System program.

I would like to keep this process in the process of submitting it.

+4
source share
2 answers

, - , stdin, . , , " " Scala, . , , Scala, / Scala, , Process.run(true) /err , .

SimpleProgram :

package so

import java.util.Scanner

object SimpleProgram extends App {

  def getInput(prompt: String): Option[String] = {
    print( s"$prompt: " )
    val sc = new Scanner(System.in)
    sc.hasNextLine match {
      case true => val out = sc.nextLine
        if( out.length > 1 ) Some(out) else None
      case _ => None
    }
  }

  while( true ) {
    (getInput("username"), getInput("password")) match {
      case (Some(u), Some(p)) => println( s"Logged in as $u" ); System.exit(0)
      case _ => println( "Invalid input.  Please try again." )
    }
  }
}

, ProcessBuilder ( ):

package so

import scala.sys.process._

object ProcessBuilderTest extends App {

  val classpath = "<path to>/scala-library.jar:./classes"
  val pb = Process("java", Seq("-cp", classpath, "so.SimpleProgram" ))
  pb.run(true) // all the magic happens here.
}

Process ProcessBuilder. ProcessIO , , .. , run(true) .

:

username: foo
password: 
Invalid input.  Please try again.
username: foo
password: bar
Logged in as foo

"java" Scala, Scala, mac, Eclipse. Eclipse ProcessBuilderTest , "" "bin".

+1

,

import scala.sys.process._
import java.io.File

"""echo -e hello\nworld""" #> new File("hello") !

new File ("hello") #> """grep world -c""" !!

1.

- ; .

"""echo -e hello\nworld""" #> """grep world -c""" !!

. .

+4

Source: https://habr.com/ru/post/1535179/


All Articles