如何使用 Scala 进程执行需要用户交互的操作系统命令?
为了说明这一点,考虑例如 Unix/Linux 操作系统中的 passwd
,如下所示,
import sys.process._
import scala.language.postfixOps
等等
scala> "passwd"!
(current) UNIX password: passwd: Authentication token manipulation error
passwd: password unchanged
Changing password for userabc
res0: Int = 10
最佳答案
使用“!<”代替“!”
"passwd"!<
来自scala.sys.process.ProcessBuilder特点:
/** Starts the process represented by this builder, blocks until it exits, and
* returns the exit code. Standard output and error are sent to the console.
*/
def ! : Int
/** Starts the process represented by this builder, blocks until it exits, and
* returns the exit code. Standard output and error are sent to the console.
* The newly started process reads from standard input of the current process.
*/
def !< : Int
关于linux - 在 Scala 中执行需要用户交互的 OS 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26183943/