Scala cmd 执行Linux命令

  .! 的作用是直接打印结果 

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

 Seq("ls","-al")  会自动拼接成  ls -al

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

 执行这个的候,由于这个stream太大,发生了gc

 由于 根目录下 / 文件太多,导致 出现了 gc

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)

 用 !! 可以将结果用一个常量支承接,然后操作

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

 管道命令:  #| 

scala> ("ls -al" #| "grep doc").!
drwxrwxr-x 4 1001 1001 4096 3月   4 2016 doc
res10: Int = 0

 重定向命令: #>  #<

scala> import java.io.File
import java.io.File

scala> val contents = ("cat" #< new File("/etc/passwd")).!!
contents: String =
"root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
rpc:x:32:32:...

 

你可能感兴趣的:(Scala cmd 执行Linux命令)