Create and write to .plist using terminal or bash script

I need to create a .plist file during post install and the only option I can use is a bash script. I need to create foo.plist in /Library/launchAgents

with a bash script and I used the following command:

cd /Library/launchAgents
touch foo.plist

      

now i need to write the content to this .plist file like:

".plist contents" >> foo.plist

      

Is there a command that can do this in the terminal?

+3


source to share


2 answers


Your question doesn't state very well what you have, or why you need to do it in bash

, but if you have to do it this way, you can do it like this:

#!/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

      

So, you save this in a file named Buildplist

and then you do this to make it executable

chmod +x Buildplist

      

and then start it by typing the following:

./Buildplist

      

You can force it to write the plist file directly to /Library/launchAgents

by changing the second line to something like this:

cat > /Library/launchAgents/yourApp/yourApp.plist <<EOF

      



You can make it accept parameters too. So if you want to pass the Author as the first parameter, you can do it like this:

#!/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

      

and then run

./Buildplist "Freddy Frog"

      

transfer "Freddy Frog" as the author.

If you want to avoid overwriting any plist file that already exists, you can do it like this:

#!/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

      

I put the name of the plist file in a variable to make it easier to maintain and not have to type it twice.

+3


source


PlistBuddy is what you want.

/usr/libexec/PlistBuddy Info.plist
File Doesn't Exist, Will Create: Info.plist

      

Then add an entry to a file like this,



/usr/libexec/PlistBuddy -c 'add CFBundleIdenfier string com.tencent.myapp' Info.plist

      

By the way, man plist

it man plutil

may be useful to you.

+9


source







All Articles