[英]Using Quotes with Commands in Scala Shell Scripting
嘗試在 scala bash 文件中使用以下命令(已導入 sys.process._):
val writeToLine5 = "sed -i '5a some text' to.file".!
出現以下錯誤:
> "sed: -e expression #1, char 1: unknown command: `'';
命令本身在命令行中完美運行。
也試過:
"""sed -i "5a adding some text to" file.text""".!;
"sed -i \'5a adding some text to\' file.text".!;
這里有scala shell腳本專家嗎? 謝謝!
PS:在 askubuntu.com 上問過。 他們建議在這里問。
'
字符的解釋由 shell 完成,而不是由sed
本身完成,因此通常最容易讓 shell 為您完成。
import sys.process._
val writeToLine5 = Seq("sh", "-c", "sed -i '5a some text' to.file").!
但是你可以自己做解釋。
val writeToLine5 = Seq("sed", "-i", "5a some text", "to.file").!
您也可以使用 Regex 模式來解釋內部引用,但它很容易出錯,我真的不推薦它。
val cmd = "sed -i '5a some text' to.file"
val res = cmd.split(""" +(?=([^'"]*['"][^'"]*['"])*[^'"]*$)""") //split on non-quoted spaces
.map(_.replaceAll("['\"]","")) //remove all the internal quote marks
.toSeq.!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.