Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to check if the User home on OS X has a file environment.plist in .MacOSX folder and if this file exists then append some new key values into it.

But if it does not exists then create this file with new key values. I am new to OS X so I don't know how to get user home and perform these checking, one of my friends told me I can do this by writing shell script.

Please guide me on this.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Manually editing the file

Note that you can also always just go into the Terminal and write:

mkdir -p ~/.MacOSX
touch ~/.MacOSX/environment.plist
open ~/.MacOSX/environment.plist

and edit the file accordingly, if that's what you are trying to do. You should preferably use Apple's own Property List Editor.app, which comes with Xcode 3. Surely there are other editors with support for plist files, but I can't recall any right now.

If you want, you can also just open ~/.MacOSX/ and then open the plist file with any other text editor, as long as you keep the right format.


With a shell script

But, in your question you asked for something like that:

#!/usr/bin/env bash    
FILE=~/.MacOSX/environment.plist
PLIST=/usr/libexec/PlistBuddy

# if the file doesn't exist, try to create folder
if [ ! -f $FILE ]
then
  mkdir -p ~/.MacOSX
fi

# then just add entries (file will be created if it doesn't exist)
$PLIST -c "Add :Variable1 string 'Value1'" $FILE
$PLIST -c "Add :Variable2 string 'Value2'" $FILE

This script will create the file if it doesn't exist already and then add two variables to it. Their names are Variable1 and Variable2, respectively, and their values are Value1 and Value2.

If the file already exists, the variables and values are just appended.

Copy this code, paste it to a file named, for example, script.sh. Save it to your home folder. Open the Terminal, then run

chmod +x script.sh

Now you can run it with ./script.sh and it will create the environment.plist file.

If you want to remove the plist file again, just enter

rm ~/.MacOSX/environment.plist

The folder will remain, but that's not really a problem.

share|improve this answer
    
He wants to add values if it exists. I suggest /usr/libexec/PlistBuddy for that. –  Daniel Beck May 28 '11 at 10:43
    
Thanks for plist editor suggestion but I want to do this from shell script. It would be helpful if you could also help me out in modifying the existing plist as I don't know the complexity of script. –  Sandy May 28 '11 at 10:48
    
@Daniel Ah, right. Updated :) @Sandy I edited my answer to use PlistBuddy as Daniel suggested. It basically just appends the variables and creates the file if necessary. What else do you need? –  slhck May 28 '11 at 11:22
    
@Sandy slhck responded to your comment. (slhck: I think it's still only one @notification per comment) –  Daniel Beck May 28 '11 at 11:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.