plistbuddy command value from csv, search and insert I have a plistbuddy command that I need to execute for 100 different users.

The value I have to enter for the 100 individual users depends on the hostname.

I was thinking I could use a csv that has a list of the hostnames/computernames and the corresponding plist value, then use a shell script to:  


Check for the hostname of the Mac
Find the hostname in the csv
find the value of the corresponding code needed for the plistbuddy command and save it to a variable
insert that value into the plistbuddy command and execute, changing the value of the desired plist file.


The csv would just be formatted as:
hostname, code
hostname1, 2001
hostname2, 2002  

Specifically the plist file is the printer preference plist file.

Here is the plistbuddy command:

/usr/libexec/PlistBuddy -c "Set :2ndfloor:com.apple.print.preset.settings:ManagementCodeValue 1001" ~/Library/Preferences/com.apple.print.custompresets.forprinter.CMI2ndFloorColor.plist


The value that needs to change depending on the hostname is the value "1001" in that command.

Those are the only elements; check hostname, find it in the csv, execute the plistbuddy command using the corresponding code in the csv.

Anyone have any good ideas for this? It would save hours and hours of GUI work.
Here's a three-line shell script for bash. Assume the CSV file is named hosts.csv.

h=`hostname -s`
c=`sed -nE -e "/^$h, ?/s/^.+, ?//p" hosts.csv`
/usr/libexec/PlistBuddy -c "Set :2ndfloor:com.apple.print.preset.settings:ManagementCodeValue $c" ~/Library/Preferences/com.apple.print.custompresets.forprinter.CMI2ndFloorColor.plist


The script sets the shell variables $h to the host name, and $c to the code that matches the host. Get the name of the host from the hostname command. Use $c in place of the value "1001" in the PlistBuddy command.

By default the hostname command returns domain information. Therefore if the CSV file has host names without domain information, use h=`hostname -s`, otherwise use h=`hostname`.

The sed command matches a line from the CSV file that starts with the $h host name followed by a comma and an optional space character. For the line that matches, sed removes everything but the code from the line.