从Scala REPL执行shell命令

时间:2012-03-27 08:48:42

标签: scala

Scala REPL的一个有趣特性是,如果你删除%SCALA_HOME%\lib目录中的任何jar,它都可以从REPL导入。我在那里有几个罐子,我经常需要找出哪些罐子可以包括在内。所以我总是要打开另一个命令窗口,找出该目录中存在哪些jar。如果REPL允许我执行诸如dirls之类的系统命令或者至少列出上述lib目录中的所有jar,那将是很好的。在REPL中调用shell命令最简单的方法是什么(如果有的话)?

5 个答案:

答案 0 :(得分:23)

在REPL中,:sh命令允许您引入shell命令:

Windows版本:

scala> :sh cmd /C dir
  res0: scala.tools.nsc.interpreter.ProcessResult = `cmd /C dir` (28 lines, exit 0)
scala> res0 foreach println

(遗憾的是,在shell命令之前无法避免调用cmd \C

类似Unix的版本:

scala> :sh ls
  res0: scala.tools.nsc.interpreter.ProcessResult = `cmd /C dir` (28 lines, exit 0)
scala> res0 foreach println

更新:Daniel's answer的启发,对于Windows用户来说是一个小技巧:

scala> implicit def stringToDosProcess(s: String) =
  scala.sys.process.stringToProcess("cmd /C "+ s)
stringToDosProcess: (s: String)scala.sys.process.ProcessBuilder

scala> "dir".!

答案 1 :(得分:16)

替代方案:使用Scala的sys.process库:

scala> import sys.process._
import sys.process._

scala> "ls /home/dcs/scala-2.9.1.final".!
bin
doc
lib
man
meta
misc
src
res1: Int = 0

答案 2 :(得分:6)

<强>更新

多年来,提取:sh输出的方法发生了变化。

Welcome to Scala 2.11.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_91).
Type in expressions for evaluation. Or try :help.

scala> :sh date
res0: scala.tools.nsc.interpreter.ProcessResult = `date` (1 lines, exit 0)

scala> res0 foreach println
<console>:13: error: value foreach is not a member of scala.tools.nsc.interpreter.ProcessResult
       res0 foreach println
            ^

scala> res0.show
<console>:13: error: value show is not a member of scala.tools.nsc.interpreter.ProcessResult
       res0.show
            ^

scala> res0.lines
res3: List[String] = List(Sat Sep 17 19:29:26 PDT 2016)

答案 3 :(得分:3)

这是一个鲜为人知的REPL技巧:

Welcome to Scala version 2.10.0-20120323-101508-45eebcf98d (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.

scala> /home/dcs/scala-2.9.1.final/<TAB>
/home/dcs/scala-2.9.1.final/misc   /home/dcs/scala-2.9.1.final/bin    /home/dcs/scala-2.9.1.final/man    /home/dcs/scala-2.9.1.final/src    /home/dcs/scala-2.9.1.final/lib    
/home/dcs/scala-2.9.1.final/meta   /home/dcs/scala-2.9.1.final/doc    

scala> /home/dcs/scala-2.9.1.final/lib/<TAB>
/home/dcs/scala-2.9.1.final/lib/scala-dbc.jar        /home/dcs/scala-2.9.1.final/lib/scala-swing.jar      /home/dcs/scala-2.9.1.final/lib/jline.jar            
/home/dcs/scala-2.9.1.final/lib/scala-library.jar    /home/dcs/scala-2.9.1.final/lib/scala-compiler.jar   /home/dcs/scala-2.9.1.final/lib/scalap.jar           

scala> /home/dcs/scala-2.9.1.final/lib/scala-library.jar
res0: scala.tools.nsc.io.File = /home/dcs/scala-2.9.1.final/lib/scala-library.jar

<TAB>我在哪里按标签

答案 4 :(得分:0)

更新2018/01/15

示例:您希望查看当前工作目录中的文件:

scala> :sh ls -l
res3: scala.tools.nsc.interpreter.ProcessResult = `ls -l` (13 lines, exit 0)

但你不能这样做:

scala> res3.foreach {println}
<console>:40: error: value foreach is not a member of scala.tools.nsc.interpreter.ProcessResult
       res3.foreach {println}
            ^

首先,您必须将行分配给支持迭代的另一种类型:

scala> res3.lines
res7: List[String] = List(total 960, -rw-r--r--@  1 dave  staff   11325 Jan  3 15:01 LICENSE, -rw-r--r--@  1 dave  staff    8859 Jan  3 15:01 README.rst, drwxr-xr-x@  3 dave  staff      96 Jan  3 15:03 assembly, drwxr-xr-x@ 20 dave  staff     640 Jan  3 15:01 bin, drwxr-xr-x@ 13 dave  staff     416 Jan  3 15:01 doc, drwxr-xr-x@  7 dave  staff     224 Jan  3 15:01 docker, drwxr-xr-x@  6 dave  staff     192 Jan  3 15:03 examples, -rw-r--r--@  1 dave  staff     826 Jan  3 15:01 gradle.properties, -rw-r--r--@  1 dave  staff     128 Jan  3 15:04 h2o_drivers.txt, drwxr-xr-x   3 dave  staff      96 Jan 16 00:54 h2ologs, drwxr-xr-x@  5 dave  staff     160 Jan  3 15:04 py, -rw-r--r--@  1 dave  staff  455890 Sep 19 04:18 rsparkling.tar.gz)

然后迭代,瞧!

scala> res7.foreach {println}
total 960
-rw-r--r--@  1 dave  staff   11325 Jan  3 15:01 LICENSE
-rw-r--r--@  1 dave  staff    8859 Jan  3 15:01 README.rst
drwxr-xr-x@  3 dave  staff      96 Jan  3 15:03 assembly
drwxr-xr-x@ 20 dave  staff     640 Jan  3 15:01 bin
drwxr-xr-x@ 13 dave  staff     416 Jan  3 15:01 doc
drwxr-xr-x@  7 dave  staff     224 Jan  3 15:01 docker
drwxr-xr-x@  6 dave  staff     192 Jan  3 15:03 examples
-rw-r--r--@  1 dave  staff     826 Jan  3 15:01 gradle.properties
-rw-r--r--@  1 dave  staff     128 Jan  3 15:04 h2o_drivers.txt
drwxr-xr-x   3 dave  staff      96 Jan 16 00:54 h2ologs
drwxr-xr-x@  5 dave  staff     160 Jan  3 15:04 py
-rw-r--r--@  1 dave  staff  455890 Sep 19 04:18 rsparkling.tar.gz