1. 程式人生 > >Scala cmd 執行命令

Scala cmd 執行命令

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

scala> "ls -al".!
總用量 24
drwxrwxr-x 6 1001 1001 4096 3月   4 2016 .
drwxr-xr-x 4 root root 4096 9月  13 15:53 ..
drwxrwxr-x 2 1001 1001 4096 3月   4 2016 bin
drwxrwxr-x 4 1001 1001 4096 3月   4 2016 doc
drwxrwxr-x 2 1001 1001 4096 3月   4 2016 lib
drwxrwxr-x 3 1001 1001 4096 3月   4 2016 man
res0: Int = 0
scala> val exitCode = "ls -al".!
總用量 24
drwxrwxr-x 6 1001 1001 4096 3月   4 2016 .
drwxr-xr-x 4 root root 4096 9月  13 15:53 ..
drwxrwxr-x 2 1001 1001 4096 3月   4 2016 bin
drwxrwxr-x 4 1001 1001 4096 3月   4 2016 doc
drwxrwxr-x 2 1001 1001 4096 3月   4 2016 lib
drwxrwxr-x 3 1001 1001 4096 3月   4 2016 man
exitCode: Int = 0
scala> :paste
// Entering paste mode (ctrl-D to finish)

def listFile: Int ={
    val cmd = "ls -al"
    val exitCode = cmd.!
    exitCode
  }


// Exiting paste mode, now interpreting.

listFile: Int

scala> listFile
總用量 24
drwxrwxr-x 6 1001 1001 4096 3月   4 2016 .
drwxr-xr-x 4 root root 4096 9月  13 15:53 ..
drwxrwxr-x 2 1001 1001 4096 3月   4 2016 bin
drwxrwxr-x 4 1001 1001 4096 3月   4 2016 doc
drwxrwxr-x 2 1001 1001 4096 3月   4 2016 lib
drwxrwxr-x 3 1001 1001 4096 3月   4 2016 man
res1: Int = 0
scala> Seq("ls","-al").!
總用量 24
drwxrwxr-x 6 1001 1001 4096 3月   4 2016 .
drwxr-xr-x 4 root root 4096 9月  13 15:53 ..
drwxrwxr-x 2 1001 1001 4096 3月   4 2016 bin
drwxrwxr-x 4 1001 1001 4096 3月   4 2016 doc
drwxrwxr-x 2 1001 1001 4096 3月   4 2016 lib
drwxrwxr-x 3 1001 1001 4096 3月   4 2016 man
res2: Int = 0
scala> Process("ls -al").!
總用量 24
drwxrwxr-x 6 1001 1001 4096 3月   4 2016 .
drwxr-xr-x 4 root root 4096 9月  13 15:53 ..
drwxrwxr-x 2 1001 1001 4096 3月   4 2016 bin
drwxrwxr-x 4 1001 1001 4096 3月   4 2016 doc
drwxrwxr-x 2 1001 1001 4096 3月   4 2016 lib
drwxrwxr-x 3 1001 1001 4096 3月   4 2016 man
res3: Int = 0
scala> val files = Process("find / -print").lines
warning: there was one deprecation warning; re-run with -deprecation for details
files: Stream[String] = Stream(/, ?)

scala> Exception in thread "Thread-17" java.lang.OutOfMemoryError: GC overhead limit exceeded
	at scala.util.Right$.apply(Either.scala:201)
	at scala.sys.process.BasicIO$Streamed$$anonfun$apply$2.apply(BasicIO.scala:52)
	at scala.sys.process.BasicIO$Streamed$$anonfun$apply$2.apply(BasicIO.scala:52)
	at scala.sys.process.BasicIO$.readFully$1(BasicIO.scala:184)
	at scala.sys.process.BasicIO$.processLinesFully(BasicIO.scala:188)
	at scala.sys.process.BasicIO$$anonfun$processFully$1.apply(BasicIO.scala:165)
	at scala.sys.process.BasicIO$$anonfun$processFully$1.apply(BasicIO.scala:163)
	at scala.sys.process.ProcessBuilderImpl$Simple$$anonfun$3.apply$mcV$sp(ProcessBuilderImpl.scala:74)
	at scala.sys.process.ProcessImpl$Spawn$$anon$1.run(ProcessImpl.scala:23)

由於 根目錄下 / 檔案太多,導致 出現了 gc

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

scala> val result = "ls -al" !!
warning: there was one feature warning; re-run with -feature for details
result: String =
"總用量 24
drwxrwxr-x 6 1001 1001 4096 3月   4 2016 .
drwxr-xr-x 4 root root 4096 9月  13 15:53 ..
drwxrwxr-x 2 1001 1001 4096 3月   4 2016 bin
drwxrwxr-x 4 1001 1001 4096 3月   4 2016 doc
drwxrwxr-x 2 1001 1001 4096 3月   4 2016 lib
drwxrwxr-x 3 1001 1001 4096 3月   4 2016 man
"

scala> println(result)
總用量 24
drwxrwxr-x 6 1001 1001 4096 3月   4 2016 .
drwxr-xr-x 4 root root 4096 9月  13 15:53 ..
drwxrwxr-x 2 1001 1001 4096 3月   4 2016 bin
drwxrwxr-x 4 1001 1001 4096 3月   4 2016 doc
drwxrwxr-x 2 1001 1001 4096 3月   4 2016 lib
drwxrwxr-x 3 1001 1001 4096 3月   4 2016 man