Help with a Shell Dilemma - Creating a PLIST and checking it.

jwells_Box
New Contributor II

Howdy All,

I'm hoping someone here can point in the direction of the best path for something I've been wanting to do.

I would like to create a shell script that checks for a file (Lets say a Plist) and if its NOT there, the script does XYZ functions and creates the Plist it looked for in the first place. My idea, is the script runs on a reoccurring basis and checks for the plist, as long as the plist exists, the script won't execute the addition functions. if the Plist isn't there for some reason, the script runs the additional functions and writes the Plist so it doesn't execute the additional functions again.

Does that make sense? What would be a best example of this to start with? I've been unable to find anything similar to what i've been searching for. :(

Thanks for any help in advance!

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

Hi @jwells_Box I have something similar to what you're describing in one of my scripts on my github page - https://github.com/mm2270/JamfProScripts/blob/master/post_restart_recon_control.sh
However, it's kind of buried inside a function in the script, so to make it a little easier, here's an example of how you could do what you want in your script.

#!/bin/bash

PLIST="/path/to/preference.plist"

if [ -e "$PLIST" ]; then
    echo "Plist file exists, exiting..."
    exit 0
else
    echo "Plist does not exist. Creating it..."
    /usr/bin/defaults write "$PLIST" SomeValue
    exit 0
fi

Basically, in a simple if/then check you see if the plist exists (you could go a step further and check the contents of the plist to see if the value(s) you expect is in it)
If it's there, it just exits, if not, it writes the plist, maybe with a default value using the defaults command, and then exits.

View solution in original post

5 REPLIES 5

mm2270
Legendary Contributor III

Hi @jwells_Box I have something similar to what you're describing in one of my scripts on my github page - https://github.com/mm2270/JamfProScripts/blob/master/post_restart_recon_control.sh
However, it's kind of buried inside a function in the script, so to make it a little easier, here's an example of how you could do what you want in your script.

#!/bin/bash

PLIST="/path/to/preference.plist"

if [ -e "$PLIST" ]; then
    echo "Plist file exists, exiting..."
    exit 0
else
    echo "Plist does not exist. Creating it..."
    /usr/bin/defaults write "$PLIST" SomeValue
    exit 0
fi

Basically, in a simple if/then check you see if the plist exists (you could go a step further and check the contents of the plist to see if the value(s) you expect is in it)
If it's there, it just exits, if not, it writes the plist, maybe with a default value using the defaults command, and then exits.

jwells_Box
New Contributor II

@mm2270 Thank you so much! this is perfect!

donmontalvo
Esteemed Contributor III

Interesting, without asking for any sensitive info, curious why the confirmation is needed at the start? I’m asking in case it can help me with some of what I’m doing.

--
https://donmontalvo.com

jwells_Box
New Contributor II

@donmontalvo Settings for each user the first time they login to the computer. they should only be applied once and never again. So I write the plist after the settings are done to prevent a repeat.

kevindigg
New Contributor

@jwells_Box Have you heard of outset? I think it does something similar to what you're looking for. It allows you to run a script at boot, login, or on demand. These scripts can run every time or just once per account. We use it for a little dock/preference configuration when an account logs in the first time.

https://github.com/chilcote/outset/wiki/FAQ
https://github.com/chilcote/outset/wiki