Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'New Version for both Bash and Tcsh' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
New Version for both Bash and Tcsh
Authored by: ssantry on Apr 02, '04 11:47:55AM

I figured since Panther defaults to bash, I should modify the script to support bash as well, so here it is. Usage is pretty much the same, except you run this script from ~/.bashrc instead of ~/.tcshrc.


#!/usr/bin/ruby

#
# A script for parsing ~/.MacOSX/environment.plist and loading the 
# environment variables it defines into a shell environment.
#

# determine which shell the user is running
# currently we support bash and tcsh
# since bash is the default shell on Panther, 
# we'll default to that here as well
if /^\/[-A-Za-z\/]+\/(bash|tcsh)$/ =~ ENV['SHELL']
    shell = $1
else
    # if we can't determine the users shell, or if
    # it's an unsupported shell, bail out here
    exit 1
end

# a regex for matching <key>...</key> lines
# group 1 is the name of the key
key_re = /^\s*<key>([A-Za-z]+[_A-Za-z0-9]*)<\/key>\s*$/

# a regex for matching <string>...</string> value lines
# group 1 is the value of the environment variable
value_re = /^\s*<string>([-_:.\/0-9A-Za-z]+)<\/string>\s*$/

File.open("#{ENV['HOME']}/.MacOSX/environment.plist", "r") do |plist|

    currentKey = "" # the key we're currently processing
    
    # look at each line of the file to find keys
    # followed by values
    plist.each_line do |next_line| 
    
        # if we find a key, hold on to it 
        if key_re =~ next_line
            currentKey = $1
            currentValue = ""
        
        # since key lines alternate with value lines,
        # if we match a value line, we know it's a value
        # for the previously matched key
        elsif value_re =~ next_line
            currentValue = $1
        
            if shell == "bash"
                # output a setenv command to stdout that's 
                # suitable for running through bash's eval
                puts "#{currentKey}=#{currentValue}; export #{currentKey};"
            elsif shell == "tcsh"
                # output a setenv command to stdout that's 
                # suitable for running through tcsh's eval
                puts "setenv #{currentKey} #{currentValue};"
            else
                # we should never get to this point since we 
                # exit much earlier if the shell type can't be 
                # determined. But, just in case, exit here too.
                exit 1
            end
        
            currentKey = currentValue = ""
        end
    
    end

end

---
- Sean

[ Reply to This | # ]

New Version for both Bash and Tcsh
Authored by: ssantry on Apr 02, '04 11:54:58AM

Err, ignore that comment on lines 10 and 11 of the new script. If the script can't determine the shell, it exits right away and doesn't default to bash...



[ Reply to This | # ]
New Version for both Bash and Tcsh
Authored by: Thomas Kaiser on Apr 03, '04 11:46:36AM
Hey folks,

Apple provided us with the "defaults" command to deal with property lists.
So, to read all key/value pairs in a very simple and effective way, you simply need

     defaults read "${HOME}/.MacOSX/environment"

That said, the bash way suitable for 99 percent of all cases to include directly into .bashrc would read as follows (on a single line): defaults read "${HOME}/.MacOSX/environment" | tr -d "{" | tr -d "}" | sed 's{\ =\ {={g' | tr ";" "\n" | grep "=" | while read -r OneLine; do eval export $OneLine; done

Cheers,

Thomas

[ Reply to This | # ]
New Version for both Bash and Tcsh
Authored by: Thomas Kaiser on Apr 03, '04 12:29:47PM
Two small additions:

> the bash way suitable for 99 percent of all cases

That means, this will work unless key or value contain "{", "}", or ";" because they will be converted while parsing the property list.

> tr ";" "n"

Should read "\n". The backslash character "\" has been eaten by the form submission, so once again:
defaults read "${HOME}/.MacOSX/environment" | tr -d "{" | tr -d "}" | sed 's{ = {={g' | tr ";" "\n" | grep "=" | while read -r OneLine; do eval export $OneLine; done


[ Reply to This | # ]