<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: ssh code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 29 Aug 2008 17:01:51 GMT</pubDate>
    <description>DZone Snippets: ssh code</description>
    <item>
      <title>Load OS X's environment.plist in your shell</title>
      <link>http://snippets.dzone.com/posts/show/586</link>
      <description>To make environment variables available to Mac OS X GUI applications, those variables must be defined in your ~/.MacOSX/environment.plist. Sometimes you want to have those same environment variables available from your shell as well. If you're using Terminal, that's no problem--the variables from environment.plist are available. But what if you need to SSH into the machine? &lt;br /&gt;&lt;br /&gt;Adhering to the DRY principle, this script can be launched from your shell's rc file to load up the environment variables from environment.plist. To load it into my tcsh environment, I use&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;if ($?SSH_CLIENT) then&lt;br /&gt;    eval `~/bin/parseEnvironmentPlist.rb`&lt;br /&gt;endif&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And here's the ruby script:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;&lt;br /&gt;#&lt;br /&gt;# A script for parsing ~/.MacOSX/environment.plist and loading the &lt;br /&gt;# environment variables it defines into a shell environment.&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;# determine which shell the user is running&lt;br /&gt;# currently we support bash and tcsh&lt;br /&gt;if /^\/[-A-Za-z\/]+\/(bash|tcsh)$/ =~ ENV['SHELL']&lt;br /&gt;    shell = $1&lt;br /&gt;else&lt;br /&gt;    # if we can't determine the users shell, or if&lt;br /&gt;    # it's an unsupported shell, bail out here&lt;br /&gt;    exit 1&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# a regex for matching &lt;key&gt;...&lt;/key&gt; lines&lt;br /&gt;# group 1 is the name of the key&lt;br /&gt;key_re = /^\s*&lt;key&gt;([A-Za-z]+[_A-Za-z0-9]*)&lt;\/key&gt;\s*$/&lt;br /&gt;&lt;br /&gt;# a regex for matching &lt;string&gt;...&lt;/string&gt; value lines&lt;br /&gt;# group 1 is the value of the environment variable&lt;br /&gt;value_re = /^\s*&lt;string&gt;([-_:.\/0-9A-Za-z]+)&lt;\/string&gt;\s*$/&lt;br /&gt;&lt;br /&gt;File.open("#{ENV['HOME']}/.MacOSX/environment.plist", "r") do |plist|&lt;br /&gt;&lt;br /&gt;    currentKey = "" # the key we're currently processing&lt;br /&gt;    &lt;br /&gt;    # look at each line of the file to find keys&lt;br /&gt;    # followed by values&lt;br /&gt;    plist.each_line do |next_line| &lt;br /&gt;    &lt;br /&gt;        # if we find a key, hold on to it &lt;br /&gt;        if key_re =~ next_line&lt;br /&gt;            currentKey = $1&lt;br /&gt;            currentValue = ""&lt;br /&gt;        &lt;br /&gt;        # since key lines alternate with value lines,&lt;br /&gt;        # if we match a value line, we know it's a value&lt;br /&gt;        # for the previously matched key&lt;br /&gt;        elsif value_re =~ next_line&lt;br /&gt;            currentValue = $1&lt;br /&gt;        &lt;br /&gt;            if shell == "bash"&lt;br /&gt;                # output a setenv command to stdout that's &lt;br /&gt;                # suitable for running through bash's eval&lt;br /&gt;                puts "#{currentKey}=#{currentValue}; export #{currentKey};"&lt;br /&gt;            elsif shell == "tcsh"&lt;br /&gt;                # output a setenv command to stdout that's &lt;br /&gt;                # suitable for running through tcsh's eval&lt;br /&gt;                puts "setenv #{currentKey} #{currentValue};"&lt;br /&gt;            else&lt;br /&gt;                # we should never get to this point since we &lt;br /&gt;                # exit much earlier if the shell type can't be &lt;br /&gt;                # determined. But, just in case, exit here too.&lt;br /&gt;                exit 1&lt;br /&gt;            end&lt;br /&gt;        &lt;br /&gt;            currentKey = currentValue = ""&lt;br /&gt;        end&lt;br /&gt;    &lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;I wrote this script back when I was first learning Ruby, so A) that's why there are so many comments and 2) it could probably be improved, but it works for me!</description>
      <pubDate>Fri, 19 Aug 2005 04:33:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/586</guid>
      <author>santry (Sean Santry)</author>
    </item>
  </channel>
</rss>
