Macos 使用Terminal或bash脚本创建并写入.plist

Macos 使用Terminal或bash脚本创建并写入.plist,macos,bash,terminal,info.plist,Macos,Bash,Terminal,Info.plist,我需要在安装后创建一个.plist文件,唯一可以使用的选项是bash脚本。我必须使用bash脚本在/Library/launchAgents中创建foo.plist,我使用了以下命令: cd /Library/launchAgents touch foo.plist 现在我需要将内容写入这个.plist文件,例如: ".plist contents" >> foo.plist #!/bin/bash VERSION=2.12 cat > foo.plist <<

我需要在安装后创建一个.plist文件,唯一可以使用的选项是bash脚本。我必须使用bash脚本在
/Library/launchAgents
中创建foo.plist,我使用了以下命令:

cd /Library/launchAgents
touch foo.plist
现在我需要将内容写入这个.plist文件,例如:

".plist contents" >> foo.plist
#!/bin/bash
VERSION=2.12
cat > foo.plist <<EOF
<?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>BuildAliasOf</key>
        <string>ProEditor</string>
        <key>BuildVersion</key>
        <value>$VERSION</value>
</dict>
</plist>
EOF
cat > /Library/launchAgents/yourApp/yourApp.plist <<EOF
#!/bin/bash
VERSION=2.12
AUTHOR="$1"
cat > foo.plist <<EOF
<?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>BuildAliasOf</key>
        <string>ProEditor</string>
        <key>BuildVersion</key>
        <value>$VERSION</value>
        <author>$AUTHOR</author>
</dict>
</plist>
EOF
#!/bin/bash
PLISTFILE="/Library/launchAgents/yourApp/yourApp.plist"

# If plist already exists, do not overwrite, just exit quietly
[ -f "$PLISTFILE" ] && exit

cat > "$PLISTFILE" <<EOF
...
...
EOF

终端中是否有可以执行此操作的命令?

您的问题没有很好地说明您得到了什么,或者您为什么需要在
bash
中执行此操作,但是如果您必须这样做,您可以这样做:

".plist contents" >> foo.plist
#!/bin/bash
VERSION=2.12
cat > foo.plist <<EOF
<?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>BuildAliasOf</key>
        <string>ProEditor</string>
        <key>BuildVersion</key>
        <value>$VERSION</value>
</dict>
</plist>
EOF
cat > /Library/launchAgents/yourApp/yourApp.plist <<EOF
#!/bin/bash
VERSION=2.12
AUTHOR="$1"
cat > foo.plist <<EOF
<?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>BuildAliasOf</key>
        <string>ProEditor</string>
        <key>BuildVersion</key>
        <value>$VERSION</value>
        <author>$AUTHOR</author>
</dict>
</plist>
EOF
#!/bin/bash
PLISTFILE="/Library/launchAgents/yourApp/yourApp.plist"

# If plist already exists, do not overwrite, just exit quietly
[ -f "$PLISTFILE" ] && exit

cat > "$PLISTFILE" <<EOF
...
...
EOF
然后通过键入以下命令来运行它:

./Buildplist
通过将第二行更改为类似以下内容,您可以将plist文件直接写入
/Library/launchAgents

".plist contents" >> foo.plist
#!/bin/bash
VERSION=2.12
cat > foo.plist <<EOF
<?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>BuildAliasOf</key>
        <string>ProEditor</string>
        <key>BuildVersion</key>
        <value>$VERSION</value>
</dict>
</plist>
EOF
cat > /Library/launchAgents/yourApp/yourApp.plist <<EOF
#!/bin/bash
VERSION=2.12
AUTHOR="$1"
cat > foo.plist <<EOF
<?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>BuildAliasOf</key>
        <string>ProEditor</string>
        <key>BuildVersion</key>
        <value>$VERSION</value>
        <author>$AUTHOR</author>
</dict>
</plist>
EOF
#!/bin/bash
PLISTFILE="/Library/launchAgents/yourApp/yourApp.plist"

# If plist already exists, do not overwrite, just exit quietly
[ -f "$PLISTFILE" ] && exit

cat > "$PLISTFILE" <<EOF
...
...
EOF
通过“弗雷迪青蛙”作为作者

如果要避免覆盖任何已存在的plist文件,可以这样做:

".plist contents" >> foo.plist
#!/bin/bash
VERSION=2.12
cat > foo.plist <<EOF
<?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>BuildAliasOf</key>
        <string>ProEditor</string>
        <key>BuildVersion</key>
        <value>$VERSION</value>
</dict>
</plist>
EOF
cat > /Library/launchAgents/yourApp/yourApp.plist <<EOF
#!/bin/bash
VERSION=2.12
AUTHOR="$1"
cat > foo.plist <<EOF
<?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>BuildAliasOf</key>
        <string>ProEditor</string>
        <key>BuildVersion</key>
        <value>$VERSION</value>
        <author>$AUTHOR</author>
</dict>
</plist>
EOF
#!/bin/bash
PLISTFILE="/Library/launchAgents/yourApp/yourApp.plist"

# If plist already exists, do not overwrite, just exit quietly
[ -f "$PLISTFILE" ] && exit

cat > "$PLISTFILE" <<EOF
...
...
EOF
#/bin/bash
PLISTFILE=“/Library/launchAgents/yourApp/yourApp.plist”
#如果plist已经存在,不要覆盖,只需悄悄退出即可
[-f“$PLISTFILE”]&退出

cat>“$PLISTFILE”PlistBuddy就是您想要的

/usr/libexec/PlistBuddy Info.plist
File Doesn't Exist, Will Create: Info.plist
然后像这样向文件中添加一个条目

/usr/libexec/PlistBuddy -c 'add CFBundleIdenfier string com.tencent.myapp' Info.plist
顺便说一下,
manplist
manplutil
可能会对您有所帮助

Command Format:
    Help - Prints this information
    Exit - Exits the program, changes are not saved to the file
    Save - Saves the current changes to the file
    Revert - Reloads the last saved version of the file
    Clear [<Type>] - Clears out all existing entries, and creates root of Type
    Print [<Entry>] - Prints value of Entry.  Otherwise, prints file
    Set <Entry> <Value> - Sets the value at Entry to Value
    Add <Entry> <Type> [<Value>] - Adds Entry to the plist, with value Value
    Copy <EntrySrc> <EntryDst> - Copies the EntrySrc property to EntryDst
    Delete <Entry> - Deletes Entry from the plist
    Merge <file.plist> [<Entry>] - Adds the contents of file.plist to Entry
    Import <Entry> <file> - Creates or sets Entry the contents of file

Entry Format:
    Entries consist of property key names delimited by colons.  Array items
    are specified by a zero-based integer index.  Examples:
        :CFBundleShortVersionString
        :CFBundleDocumentTypes:2:CFBundleTypeExtensions

Types:
    string
    array
    dict
    bool
    real
    integer
    date
    data

Examples:
    Set :CFBundleIdentifier com.apple.plistbuddy
        Sets the CFBundleIdentifier property to com.apple.plistbuddy
    Add :CFBundleGetInfoString string "App version 1.0.1"
        Adds the CFBundleGetInfoString property to the plist
    Add :CFBundleDocumentTypes: dict
        Adds a new item of type dict to the CFBundleDocumentTypes array
    Add :CFBundleDocumentTypes:0 dict
        Adds the new item to the beginning of the array
    Delete :CFBundleDocumentTypes:0 dict
        Deletes the FIRST item in the array
    Delete :CFBundleDocumentTypes
        Deletes the ENTIRE CFBundleDocumentTypes array
命令格式:
帮助-打印此信息
退出-退出程序,更改不会保存到文件中
保存-将当前更改保存到文件
还原-重新加载上次保存的文件版本
Clear[]-清除所有现有条目,并创建类型为的根
打印[]-打印条目的值。否则,打印文件
设置-将输入处的值设置为值
Add[]-将条目添加到plist,并带有值
复制-将EntrySrc属性复制到EntryDst
Delete-从plist中删除条目
合并[]-将file.plist的内容添加到条目中
导入-创建或设置输入文件的内容
输入格式:
条目由冒号分隔的属性键名称组成。数组项
由基于零的整数索引指定。示例:
:CFBundleShortVersionString
:CbundleDocumentTypes:2:CbundleTypeExtensions
类型:
一串
排列
字典
布尔
真实的
整数
日期
数据
示例:
设置:CFBundleIdentifier com.apple.plistbuddy
将CbundleIdentifier属性设置为com.apple.plistbuddy
添加:CbundlegetInfoString“应用程序版本1.0.1”
将CbundleGetInFoString属性添加到plist
Add:CbundleDocumentTypes:dict
将dict类型的新项添加到CbundleDocumentTypes数组中
添加:CbundleDocumentTypes:0 dict
将新项添加到数组的开头
删除:CbundleDocumentTypes:0 dict
删除数组中的第一项
删除:CbundleDocumentTypes
删除整个CbundleDocumentTypes数组

要写入plist的plist内容在哪里?我的意思是你有他们的清单吗?有相应的价值吗?它们是否在shell变量中?为什么必须使用bash,为什么不能使用编辑器?是的,我有一个工作的.plist文件。我需要使用bash,因为安装程序将运行此bash脚本。因此,在安装过程中将此过程自动化为安装后的一个步骤。“为什么我必须这样做?”因为我想在应用程序安装过程中执行脚本。当安装程序“whichisamg”和“PKG”安装程序完成安装后,我需要在最后执行这个bash脚本,将这个.plist文件放入/Library中。是否有任何方法可以检查和跳过文件是否已经存在?当然可以。我在最后增加了一点额外的内容来展示如何做到这一点。请再看一看。
/usr/libexec/PlistBuddy
更好。除了这个答案之外,我发现这篇文章非常有用: