Shell-Script
sh 腳本中的 sed 和 awk 不替換文本
查找字元串並替換為另一個(所有發生)以及重寫/更新文件的所需工作。
該文件是一個
pbxproj
文件。我連結了這個文件的一個樣本文件範例*(請注意此文件太大,無法在此處共享非工作範例。您可以查看之前的連結)*:
// !$*UTF8*$! { archiveVersion = 1; classes = { }; objectVersion = 46; objects = { /* Begin PBXBuildFile section */ 4D1498151706397100876A74 /* Icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 4D1498141706397100876A74 /* Icon.png */; }; 4D1498171706397300876A74 /* Icon@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 4D1498161706397300876A74 /* Icon@2x.png */; }; F8F1FD8E1705DF6A0012AA85 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8F1FD8D1705DF6A0012AA85 /* UIKit.framework */; }; F8F1FD901705DF6A0012AA85 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8F1FD8F1705DF6A0012AA85 /* Foundation.framework */; }; F8F1FD921705DF6A0012AA85 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8F1FD911705DF6A0012AA85 /* CoreGraphics.framework */; }; F8F1FDB61705DF6A0012AA85 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8F1FD8D1705DF6A0012AA85 /* UIKit.framework */; }; F8F1FDB71705DF6A0012AA85 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8F1FD8F1705DF6A0012AA85 /* Foundation.framework */; }; F8F1FDF5170609D20012AA85 /* UIImage+TBTint.m in Sources */ = {isa = PBXBuildFile; fileRef = F8F1FDDD170609D20012AA85 /* UIImage+TBTint.m */; }; F8F1FDF6170609D20012AA85 /* TBBAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = F8F1FDE0170609D20012AA85 /* TBBAppDelegate.m */; }; F8F1FDF7170609D20012AA85 /* TBBFlatImageViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F8F1FDE2170609D20012AA85 /* TBBFlatImageViewController.m */; }; F8F1FDF8170609D20012AA85 /* TBBGradientImageViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F8F1FDE4170609D20012AA85 /* TBBGradientImageViewController.m */; }; F8F1FDF9170609D20012AA85 /* TBBMasterViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F8F1FDE6170609D20012AA85 /* TBBMasterViewController.m */; }; F8F1FDFB170609D20012AA85 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = F8F1FDEA170609D20012AA85 /* Default-568h@2x.png */; }; F8F1FDFC170609D20012AA85 /* Default.png in Resources */ = {isa = PBXBuildFile; fileRef = F8F1FDEB170609D20012AA85 /* Default.png */; }; F8F1FDFD170609D20012AA85 /* Default@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = F8F1FDEC170609D20012AA85 /* Default@2x.png */; }; F8F1FDFE170609D20012AA85 /* MainStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F8F1FDED170609D20012AA85 /* MainStoryboard.storyboard */; }; . . . . . 44567HGFGHJK456GH44GHJK /* Debug */ = { . . . buildSettings = { ... FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Flutter", ); INFOPLIST_FILE = Runner/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 12.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; LIBRARY_SEARCH_PATHS = ( "$(inherited)", "$(PROJECT_DIR)/Flutter", ); MARKETING_VERSION = 1.1.1;
#!/bin/sh cpath=$(cd -P -- "$(dirname -- "$0")" && printf '%s\n' "$(pwd -P)/") xcodePath="${cpath}ios/Runner.xcodeproj/project.pbxproj" sed -i -e 's/MARKETING_VERSION = 1.1.1/MARKETING_VERSION = 1.1.2/g' xcodePath # or tried with awk too # awk '{gsub("MARKETING_VERSION = 1.1.1","MARKETING_VERSION = 1.1.2"); print}' xcodePath
完成後,我檢查了文件,沒有任何改變。沒有錯誤,沒有警告!有什麼辦法可以解決嗎?
您的腳本無法使用“$”擴展變數 xcodepath。喜歡:
sed -i -e 's/MARKETING_VERSION = 1.1.1/MARKETING_VERSION = 1.1.2/g' "$xcodePath"
要在 shell 中獲取變數的值,您需要在它前面加上
$
(並引用它,除非您有非常特殊的需要不引用它)所以xcodePath
需要"$xcodePath"
.您還需要轉義
.
元字元並錨定正則表達式,否則您會不希望地更改MARKETING_VERSION = 1.1.10
或MARKETING_VERSION = 12151
,例如,更改為MARKETING_VERSION = 1.1.2
. 您也不需要重複字元串MARKETING_VERSION = 1.1.
來將最後一位數字從 1 更改為 2,只需使用擷取組即可。sed -Ei 's/(\<MARKETING_VERSION = 1\.1\.)1;/\12;/' "$xcodePath"
您已經在使用 GNU sed 了,
-i
所以我添加了-E
只是這樣我們就不需要轉義擷取組中的括號。