我想在Scala中运行此命令,尽管我遇到了各种各样的错误:
sed -i '.back' 's/maxTablesPerQuestion/maxTablesPerQuestion = 2.0 /g' /Users/i-danielk/ideaProjects/aristo/solvers/tableilp/src/main/resources/application.conf
或
sed -i '.back' "s/maxTablesPerQuestion/maxTablesPerQuestion = 2.0 /g" /Users/i-danielk/ideaProjects/aristo/solvers/tableilp/src/main/resources/application.conf
我在这里列出了所有尝试(无效)。运行此命令的正确方法是什么?
尝试1:
scala> val command = "sed -i '.back' 's/maxTablesPerQuestion/maxTablesPerQuestion = 2.0 /g' /Users/i-danielk/ideaProjects/aristo/solvers/tableilp/src/main/resources/application.conf"
scala> command.!
sed: 1: "'s/maxTablesPerQuestion ...": invalid command code '
res25: Int = 1
尝试2:双引号:
scala> val command = "sed -i '.back' \"s/maxTablesPerQuestion/maxTablesPerQuestion = 2.0 /g\" /Users/i-danielk/ideaProjects/aristo/solvers/tableilp/src/main/resources/application.conf"
scala> command.!
sed: 1: ""s/maxTablesPerQuestion ...": invalid command code "
res26: Int = 1
尝试3:使用Seq
:
scala> val command = Seq("sed -i '.back' \"s/maxTablesPerQuestion/maxTablesPerQuestion = 2.0 /g\" /Users/i-danielk/ideaProjects/aristo/solvers/tableilp/src/main/resources/application.conf")
scala> command.!
java.io.IOException: Cannot run program "sed -i '.back' "s/maxTablesPerQuestion/maxTablesPerQuestion = 2.0 /g" /Users/i-danielk/ideaProjects/aristo/solvers/tableilp/src/main/resources/application.conf": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at scala.sys.process.ProcessBuilderImpl$Simple.run(ProcessBuilderImpl.scala:69)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.run(ProcessBuilderImpl.scala:98)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.$bang(ProcessBuilderImpl.scala:112)
... 33 elided
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:248)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 36 more
尝试4:使用Seq
:
scala> val command = Seq("sed -i '.back' \"s/maxTablesPerQuestion/maxTablesPerQuestion = 2.0 /g\"", "/Users/i-danielk/ideaProjects/aristo/solvers/tableilp/src/main/resources/application.conf")
scala> command.!
java.io.IOException: Cannot run program "sed -i '.back' "s/maxTablesPerQuestion/maxTablesPerQuestion = 2.0 /g"": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at scala.sys.process.ProcessBuilderImpl$Simple.run(ProcessBuilderImpl.scala:69)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.run(ProcessBuilderImpl.scala:98)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.$bang(ProcessBuilderImpl.scala:112)
... 33 elided
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:248)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 36 more
尝试5:Seq
:
scala> val command = Seq("sed -i '.back'", "\"s/maxTablesPerQuestion/maxTablesPerQuestion = 2.0 /g\"", "/Users/i-danielk/ideaProjects/aristo/solvers/tableilp/src/main/resources/application.conf")
scala> command.!
java.io.IOException: Cannot run program "sed -i '.back'": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at scala.sys.process.ProcessBuilderImpl$Simple.run(ProcessBuilderImpl.scala:69)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.run(ProcessBuilderImpl.scala:98)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.$bang(ProcessBuilderImpl.scala:112)
... 33 elided
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:248)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 36 more
尝试6:
scala> val command = Seq("sed -i", "'.back'", "\"s/maxTablesPerQuestion/maxTablesPerQuestion = 2.0 /g\"", "/Users/i-danielk/ideaProjects/aristo/solvers/tableilp/src/main/resources/application.conf")
scala> command.!
java.io.IOException: Cannot run program "sed -i": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at scala.sys.process.ProcessBuilderImpl$Simple.run(ProcessBuilderImpl.scala:69)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.run(ProcessBuilderImpl.scala:98)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.$bang(ProcessBuilderImpl.scala:112)
... 33 elided
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:248)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 36 more
尝试7:使用Seq
:
scala> val command = Seq("sed", "-i", "'.back'", "\"s/maxTablesPerQuestion/maxTablesPerQuestion = 2.0 /g\"", "/Users/i-danielk/ideaProjects/aristo/solvers/tableilp/src/main/resources/application.conf")
scala> command.!
sed: 1: ""s/maxTablesPerQuestion ...": invalid command code "
res34: Int = 1
答案 0 :(得分:3)
在Scala中执行系统命令的最佳方法是使用&#34; Sequence语法&#34; (见代码)。具有转义字符的长字符串更难以正确使用。使用此语法时,我几乎从未遇到任何问题。
需要注意的一点是,您不再需要使用单'
或双引号"
来定义命令的各个部分。序列已经提供了sed
(或任何其他命令)所需的所有详细信息。
尝试一下,如果它没有完全符合你的需要,请告诉我。
import sys.process._
Seq("sed","-i",".back","s/maxTablesPerQuestion/maxTablesPerQuestion = 2.0 /g", "/Users/i-danielk/ideaProjects/aristo/solvers/tableilp/src/main/resources/application.conf").!