PlistBuddy
一个plist文件操作工具。记录下方便以后使用,常用以下4个方法:
-
Add
添加 -
Delete
删除 -
Set
更新 -
Print
读取 - ...
更多方法可 --help
, 以上具体使用方法如下:
Add
- 添加一个
key
:code
value
:404
/usr/libexec/PlistBuddy "Add :code integer 404" t1.plist
结果如下:
Dict {
code = 404
}
- 添加
Array
,先创建一个数组变量list
, 然后往list
中添加数据
/usr/libexec/PlistBuddy "Add :list Array" t1.plist
/usr/libexec/PlistBuddy "Add :list: string a1" t1.plist
# 根据索引插入值
/usr/libexec/PlistBuddy "Add :list:0 string a2" t1.plist
结果如下:
Dict {
list = Array {
a2
a1
}
}
- 添加
Dict
/usr/libexec/PlistBuddy "Add :result Dict" t1.plist
/usr/libexec/PlistBuddy "Add :result:name string Tom" t1.plist
/usr/libexec/PlistBuddy "Add :result:age integer 18" t1.plist
/usr/libexec/PlistBuddy "Add :result:list Array" t1.plist
/usr/libexec/PlistBuddy "Add :result:list: string aa" t1.plist
结果如下:
Dict {
result = Dict {
age = 18
list = Array {
aa
}
name = Tom
}
}
Delete
- 根据
key
删除 对应的value
/usr/libexec/PlistBuddy "Delete :code" t1.plist
Set
- 根据
key
更新value
/usr/libexec/PlistBuddy "Set :code 200"
结果如下:
Dict {
code = 200
}
- 根据
key
读取value
/usr/libexec/PlistBuddy "Print" t1.plist
/usr/libexec/PlistBuddy "Print :code" t1.plist
结果如下:
# t1.plist
Dict {
result = Dict {
age = 18
list = Array {
aa
}
name = Tom
}
code = 200
list = Array {
a2
a1
}
}
# code
200