所需的工作是找到一个字符串,然后替换为另一个字符串(所有情况),并重写/更新文件。
文件就是pbxproj
文件。我链接了该文件的示例
文件样本(请注意,此文件太大,无法在此处共享非工作示例。您可以之前检查链接):
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objects = {
/* Begin PBXBuildFile section */
4D1498151706397100876A74 /* Icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 4D1498141706397100876A74 /* Icon.png */; };
4D1498171706397300876A74 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 4D1498161706397300876A74 /* [email protected] */; };
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 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = F8F1FDEA170609D20012AA85 /* [email protected] */; };
F8F1FDFC170609D20012AA85 /* Default.png in Resources */ = {isa = PBXBuildFile; fileRef = F8F1FDEB170609D20012AA85 /* Default.png */; };
F8F1FDFD170609D20012AA85 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = F8F1FDEC170609D20012AA85 /* [email protected] */; };
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
完成后我检查了文件,没有任何变化。没有错误,没有警告!有什么办法可以修复吗?
答案1
您的脚本无法使用“$”扩展变量 xcodepath。喜欢:
sed -i -e 's/MARKETING_VERSION = 1.1.1/MARKETING_VERSION = 1.1.2/g' "$xcodePath"
答案2
要在 shell 中获取变量的值,您需要在其前面加上前缀$
(并引用它,除非您有非常具体的需要不引用它),所以xcodePath
需要是"$xcodePath"
。
您还需要转义.
元字符并锚定正则表达式,否则您可能会意外地将MARKETING_VERSION = 1.1.10
or更改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
这样的内容,这样我们就不需要转义捕获组中的括号。