在bash中键入以下语句:
developer@playground:~$ echo dump | nc localhost 32773 | grep brokers
我有:
/brokers/ids/1
但是在scala控制台中,我没有得到任何结果:
scala> "echo dump | nc localhost 32773 | grep brokers".!
dump | nc localhost 32773 | grep brokers
res2: Int = 0
我也尝试过:
scala> "echo dump | nc localhost 32773 | grep brokers".!!
res0: String =
"dump | nc localhost 32773 | grep brokers
"
我在做什么错了?
答案 0 :(得分:3)
管道|
由外壳程序解释,但是您不调用外壳程序。
尝试使用ProcessBuilder
的管道命令。
scala> import sys.process._
import sys.process._
scala> "echo | wc".!
| wc <--- not what we want
res0: Int = 0
scala> ("echo" #| "wc").!
1 0 1
res1: Int = 0
或者,调用外壳程序。
scala> Seq("sh", "-c", "echo | wc").!
1 0 1
res2: Int = 0
答案 1 :(得分:0)
您需要导入import sys.process._
,然后执行命令"echo dump | nc localhost 32773 | grep brokers".!
:
scala> import sys.process._
scala> "echo dump | nc localhost 32773 | grep brokers".!