[Shell] |MAC通过PlistBuddy修改plist文件 | ipa版本号自增1

 Mac操作plist文件用PlistBuddy,路径在/usr/libexec/PlistBuddy

PlistBuddy是什么?

  • Mac自带的一个操作plist文件的工具
  • 路径:/usr/libexec/PlistBuddy
  • 使用: /usr/libexec/PlistBuddy -c "Set key value" plistUrl

0. 准备工作

先准备一个简单的plist文件,当前BundleVersion的值为1。

 这个plist文件对应的xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Udid</key>
    <string>bf76c991995e61c5c783f3441bff4a18605bc7ba</string>
    <key>BundleId</key>
    <string>com.foobar2000.mobile</string>
    <key>IpaPath</key>
    <string>/users/ypf/desktop</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
</dict>
</plist>

1. 读取plist中的值 

 测试文件path:/Users/xxxx/Desktop/test.plist
/usr/libexec/PlistBuddy -c "Print CFBundleVersion" /Users/xxxx/Desktop/test.plist

效果:

Mac mini:Desktop xxxx$ /usr/libexec/PlistBuddy -c "Print CFBundleVersion" /Users/xxxx/Desktop/test.plist
1 

命令解释:

// Print:读取值并打印的命令
// /usr/libexec/PlistBuddy -c:指定PlistBuddy的路径
// -c:后接要执行的命令
// CFBundleVersion:要读取value的key
// /Users/ypf/Desktop/Test.plist:plist文件的路径
/usr/libexec/PlistBuddy -c "Print CFBundleVersion" /Users/ypf/Desktop/test.plist

PlistBuddy使用TIPS:

  • 如何定义嵌套的键: 每个键之间使用":"符号分隔,比如:Software->Gallery->OnlineMarketplace表述为:":Software:Gallery:OnlineMarketplace", 第一个":"表示根.
  • 如果键值的名称包含空格等特殊字符的时候,如同命令行的转义字符一样,使用"/"来转义,比如: ":Software:Gallery:Online/ Marketplace".
  • PlistBuddy如果不使用"-c"参数,则进入人机交互模式, "-c"的意思就是执行它后面的命令列表,而命令如果有参数,需要把它们包含在引号中。

2. 用变量保存读到的BundleVersion

这个值后面还要用,所以需要定义一个变量保存起来~~ 

BundleVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" /Users/xxxx/Desktop/test.plist)
echo $BundleVersion

执行效果:

Mac mini:Desktop xxxx$ BundleVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" /Users/xxxx/Desktop/test.plist)
Mac mini:Desktop xxxx$ echo $BundleVersion
1

3. 变量的值+1 

终端下直接写+ - * /(加减乘除)会被当做字符串,写计算表达式要用到expr。 

// 注意!expr之后有个空格
BundleVersion=`expr $BundleVersion + 1`

echo $BundleVersion输出结果,可以看到BundleVersion的新值为2.

 

4. 将变量的新值保存到plist 

PlistBuddy向plist文件写入命令为Set  

// 指定plist路径、目标键值对的key
/usr/libexec/PlistBuddy -c "Set CFBundleVersion $BundleVersion" /Users/xxx/Desktop/test.plist

执行上述命令后,BundleVersion的新值为2。

 
增删改查操作示例:
1.添加
plistbuddy -c 'Add :Software:Gallery:Version string "1.0"' ~/Desktop/com.sample.plist
2.输出
plistbuddy -c "Print" ~/Desktop/com.sample.plist
3.修改
plistbuddy -c 'Set :Software:Gallery:Version "1.1"' ~/Desktop/com.sample.plist
4.删除
plistbuddy -c 'Delete :Software:Gallery:Version' ~/Desktop/com.sample.plist
5.合并
plistbuddy -c "Merge ~/Desktop/Global.plist :Software:Gallery" ~/Desktop/com.sample.plist

  

 

参考文档:

终端用shell读写plist文件

shell 脚本修改plist配置

利用shell语句批量修改plist文件

猜你喜欢

转载自www.cnblogs.com/kaerxifa/p/12169610.html