需要用到PlistBuddy
PlistBuddy是Mac自帶的專門解析plist的小工具
1、拿到工程絕對路徑
#獲取plist文件所在路徑
project_path=$"/Users/~/~/~"
PlistPath=${project_path}/config.plist
2、PlistBuddy各命令
- 獲取
#獲取整個(gè)plist
/usr/libexec/PlistBuddy -c "print" info.plist
#讀取某個(gè)Key
/usr/libexec/PlistBuddy -c "Print : key" "$PlistPath"
- 添加key
Number=$"123"
#添加
/usr/libexec/PlistBuddy -c "Add :APP_ID: $Number" "$PlistPath"
#添加制定類型
/usr/libexec/PlistBuddy -c "Add :APP_ID: String $Number" "$PlistPath"
- 修改
#修改
/usr/libexec/PlistBuddy -c "Set :APP_KEY $Number" "$PlistPath"
- 刪除
#刪除
/usr/libexec/PlistBuddy -c "Delete :APP_ID" "$PlistPath"
- 合并
# 將A.plist 合并到 B.plist中
/usr/libexec/PlistBuddy -c 'Merge A.plist' B.plist
sell腳本
#!/bin/bash
Targets_Name="test"
#工程絕對路徑
project_path=$"/Users/~/Desktop/test/test"
#plist文件所在路徑
PlistPath=${project_path}/PushConfig.plist
Number=$"123"
#添加
/usr/libexec/PlistBuddy -c "Add :APP_ID: $Number" "$PlistPath"
#添加制定類型
/usr/libexec/PlistBuddy -c "Add :APP_ID: String $Number" "$PlistPath"
#修改
/usr/libexec/PlistBuddy -c "Set :APP_KEY $Number" "$PlistPath"
#刪除
/usr/libexec/PlistBuddy -c "Delete :APP_ID" "$PlistPath"
#獲取整個(gè)plist
/usr/libexec/PlistBuddy -c "print" info.plist
#讀取某個(gè)Key
/usr/libexec/PlistBuddy -c "Print : key" "$PlistPath"
# 將A.plist 合并到 B.plist中
/usr/libexec/PlistBuddy -c 'Merge A.plist' B.plist
exit 0