Executing bash strings using scala.sys.process

前端 未结 1 667
滥情空心
滥情空心 2020-12-31 13:55

I recently discovered sys.process package in Scala, and was amused by its power.

But when I try to combine it with bash pipes and backticks, I get stuck

1条回答
  •  说谎
    说谎 (楼主)
    2020-12-31 14:46

    You're doing multiple things wrong actually. You should be using the -c option of bash and you should be using a Seq[String] with each parameter to bash in its own String, or the scala library will just split the String at every space character. (This is why Rex Kerr's solution doesn't work.)

    scala> import sys.process.stringSeqToProcess
    import sys.process.stringSeqToProcess
    
    scala> Seq("bash", "-c", "echo `date`")!!
    res20: String = 
    "Sun Dec 4 16:40:04 CET 2011
    "
    

    0 讨论(0)
提交回复
热议问题