简体   繁体   English

流程和ProcessBuilder

[英]Process and ProcessBuilder

I'm trying to run a process that greps free memory, but the process builder fails to parse the awk portion ... 我正在尝试运行可释放可用内存的进程,但是进程生成器无法解析awk部分...

scala> import scala.sys.process._

scala> "grep MemFree /proc/meminfo" #| "awk '{print $2}'" !
awk: cmd. line:1: '{print
awk: cmd. line:1: ^ invalid char ''' in expression
res16: Int = 1

Same results with this ... 与此相同的结果...

scala> Seq("grep", "MemFree /proc/meminfo") #| Seq("awk", "'", "{print $2}", "'") !
awk: cmd. line:1: '
awk: cmd. line:1: ^ invalid char ''' in expression

Escaping doesn't work either ... 转义也不起作用...

scala> Seq("grep", "MemFree /proc/meminfo") #| Seq("awk", "\"'{print $2}'\"") ! 
res21: Int = 0

How can I get awk '{print $2}' to work? 如何获得awk'{print $ 2}'的工作? Is there a way to examine what the process builder is trying to execute? 有没有一种方法可以检查流程构建器试图执行什么?

The apostrophes are only used by the bash to know where the boundaries of arguments are. 猛击仅使用撇号来知道自变量的边界在哪里。 In Scala, when you use a Seq , these boundaries are given and the apostrophe has no meaning, they are passed to awk which then complains. 在Scala中,当您使用Seq ,将给出这些边界,并且撇号没有任何意义,它们将传递给awk ,然后抱怨。

import scala.sys.process._

(Seq("grep", "MemFree", "/proc/meminfo") #| Seq("awk", "{print $2}")).!

Also note that the arguments for grep must be separated. 还要注意,grep的参数必须分开。


By the way, if you want to get the value into Scala, you can use !! 顺便说一句,如果您想将价值带入Scala,可以使用!! :

(Seq("grep","MemFree","/proc/meminfo") #| Seq("awk","{print $2}")).!!.trim.toInt

The pair of ' is used in Bash or similar shells to indicate that spaces within are not delimiters for separate arguments. Bash或类似的shell中使用的对'表示其中的空格不是分隔参数的分隔符。 That is '{print $2}' means that the first argument to the awk command should be the string {print $2} . '{print $2}'意味着awk命令的第一个参数应为字符串{print $2}

So you're looking for Seq("awk", "{print $2}") . 因此,您正在寻找Seq("awk", "{print $2}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM