天天看点

终端用shell读写plist文件

【原帖置顶】shell文件中读取plist文件并实现shell中的数值计算

Mac操作plist文件用

PlistBuddy

,路径在

/usr/libexec/PlistBuddy

0. 准备工作

先准备一个简单的plist文件,当前

BundleVersion

的值为1。

终端用shell读写plist文件

测试用的plist

这个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中的值

// Print:读取值并打印的命令
// /usr/libexec/PlistBuddy -c:指定PlistBuddy的路径
// -c:后接要执行的命令(不负责任地猜测是传递cmd参数的意思^_^)
// CFBundleVersion:要读取value的key
// /Users/ypf/Desktop/Test.plist:plist文件的路径
/usr/libexec/PlistBuddy -c "Print CFBundleVersion" /Users/ypf/Desktop/Test.plist
           
终端用shell读写plist文件

执行效果

2. 用变量保存读到的BundleVersion

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

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

3. 变量的值+1

终端下直接写

+

-

*

/

(加减乘除)会被当做字符串,写计算表达式要用到

expr

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

echo $BundleVersion

输出结果,可以看到

BundleVersion

的新值为2.

终端用shell读写plist文件

计算BundleVersion的新值

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

PlistBuddy

向plist文件写入命令为

Set

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

执行上述命令后,

BundleVersion

的新值为2。

终端用shell读写plist文件

执行结果

5. Done!

附:原帖中给出的自动编译打包ipa的shell文件

shell文件中读取plist文件并实现shell中的数值计算

#!/bin/bash

#Author Jileniao.Net
SCHEMENAME='JILENIAO'
DATE=`date +%Y%m%d_%H%M`
SOURCEPATH=$( cd "$( dirname $0 )" && pwd)
IPAPATH=$SOURCEPATH
DISNAME=$SCHEMENAME"_"$DATE
ARCHNAME=$DISNAME.xcarchive
IPANAME=$DISNAME
ExportOptions='ExportOptions.plist'
INFOPLIST=$SCHEMENAME/Info.plist
BUNDLE_ID='net.jileniao.iosblog'
DISPLAY_NAME='极乐鸟'
VERSION_NAME='1.0.5'

#BUILD_CODE='153'
PROVISION_PROFILE='jileniao-dis'

# 测试版APP
if [ -n "$1" ]; then
  # extra parameter 1
fi
rm -rf ./build
rm main.jsbundle
rm main.jsbundle.meta
rm -rf ./Build
rm -rf ./ModuleCache
rm -rf ./Logs
xcodebuild clean
CURR=`pwd`
cd .. && react-native bundle --entry-file index.js --platform ios --dev false --bundle-output ios/main.jsbundle --assets-dest ios/
cd $CURR

# 先读取之前的BUILD_CODE,加1得到新的BUILD_CODE
BUILD_CODE=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" $INFOPLIST)
BUILD_CODE=`expr $BUILD_CODE + 1`

# 设置编译参数
/usr/libexec/PlistBuddy -c "Set CFBundleDisplayName $DISPLAY_NAME" $INFOPLIST
/usr/libexec/PlistBuddy -c "Set CFBundleShortVersionString $VERSION_NAME" $INFOPLIST
/usr/libexec/PlistBuddy -c "Set CFBundleVersion $BUILD_CODE" $INFOPLIST
/usr/libexec/PlistBuddy -c "Set CFBundleIdentifier $BUNDLE_ID" $INFOPLIST
/usr/libexec/PlistBuddy -c "Delete provisioningProfiles" $ExportOptions
/usr/libexec/PlistBuddy -c "Add provisioningProfiles dict" $ExportOptions
/usr/libexec/PlistBuddy -c "Add provisioningProfiles:$BUNDLE_ID string $PROVISION_PROFILE" $ExportOptions

# 构建
xcodebuild archive \
 -project JILENIAO.xcodeproj \
 -scheme $SCHEMENAME \
 -configuration Release \
 -archivePath $ARCHNAME \
 clean \
 build \
 -derivedDataPath ./
if [ -e $ARCHNAME ]; then
  echo "xcodebuild archive Successful"
else
  echo "xcodebuild archive Failed"
  exit 1
fi

# 导出ipa
xcodebuild -exportArchive \
 -archivePath $ARCHNAME \
 -exportPath $IPANAME \
 -exportOptionsPlist $ExportOptions
if [ -e $IPANAME ]; then
  echo "Export ipa Successful"
  open $IPAPATH
else
  echo "Export ipa Failed"
  exit 1
fi

#删除临时文件
rm -rf $ARCHNAME
rm -rf ./Build
rm -rf ./ModuleCache
rm -rf ./Logs