我有一個通常在 windows 環境中執行的 build.gradle 檔案;但是當在 Linux 環境中檢出相同的檔案時,某些行中提到的批處理檔案會發生沖突,所以我們試圖將構建失敗的確切行替換為使其作業的行,結帳完成后使用 shell 腳本。
build.gradle 檔案
......
......
task exportBatch(type:Exec) {
doFirst {
println "Exporting batch..."
commandLine = ['cmd', '/C', 'start', 'export.bat']
}
}
task importBatch(type:Exec) {
doFirst {
println "Importing batch..."
commandLine = ['cmd', '/C', 'start', 'import.bat']
}
}
.....
.....
我想在執行構建之前實作以下目標;在源代碼被簽出到本地作業區之后。
在任務 exportBatch 中,要使用的行是
命令列 'sh', './export.sh'
代替
commandLine = ['cmd', '/C', 'start', 'export.bat']
在任務 importBatch 中,要使用的行
命令列 'sh', './import.sh'
代替
commandLine = ['cmd', '/C', 'start', 'import.bat']
我們厭倦了根據行號更改行,但是當行號發生更改時,我們的方法失敗了。
uj5u.com熱心網友回復:
使用sed你可以試試
$ sed "/exportBatch/,/}/ {s|commandLine =.*|commandLine 'sh', './export.sh'|}; /importBatch/,/}/ {s|commandLine =.*|commandLine 'sh', './import.sh'|}" input_file
......
......
task exportBatch(type:Exec) {
doFirst {
println Exporting batch...
commandLine 'sh', './export.sh'
}
}
task importBatch(type:Exec) {
doFirst {
println Importing batch...
commandLine 'sh', './import.sh'
}
}
.....
.....
/exportBatch/,/}/-exportBatch和之間的匹配}
s|commandLine =.*|commandLine 'sh', './export.sh'|- 在上面的匹配中,commandLine =.*用commandLine 'sh', './export.sh' 注釋替換帶有內容的行。使用了雙引號。
|- 管道被用作分隔符,但任何未出現在資料中的符號都可以用作分隔符。正如您的資料一樣/,sed無法使用默認分隔符,因為它會發生沖突并導致錯誤。
在代碼的第二部分重復相同的條件來處理第二次替換。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/360643.html
