<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: path code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 06 Sep 2008 19:50:18 GMT</pubDate>
    <description>DZone Snippets: path code</description>
    <item>
      <title>add a folder to PATH, but not if it is already in PATH</title>
      <link>http://snippets.dzone.com/posts/show/4821</link>
      <description>Often times one uses a single .profile or .bashrc across several hosts.  These hosts may be differently configured, with varying directory locations, and different initial PATH values from /etc/profile.  In situations like this, setting PATH the traditional way -- "PATH=$PATH:/path/to/some/stuff" -- can result in a cumbersomely long PATH that includes inaccessible or duplicate folders.  &lt;br /&gt;&lt;br /&gt;The snippet below nicely handles this problem.  It has been tested on Solaris and Linux.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;AddPath ()&lt;br /&gt;# Add argument to $PATH if:&lt;br /&gt;# - it is not already present&lt;br /&gt;# - it is a directory&lt;br /&gt;# - we have execute permission on it&lt;br /&gt;#&lt;br /&gt;# This snippet is public domain; you may use it freely.  Death to copyright, patents, &lt;br /&gt;# and all other forms of intellectual monopoly.  &lt;br /&gt;#&lt;br /&gt;{&lt;br /&gt;  _folder=$1&lt;br /&gt;  echo " $PATH " | sed 's/:/ /g' | grep " $_folder " &gt; /dev/null&lt;br /&gt;  [ $? -ne 0 ] &amp;&amp; [ -d $_folder ] &amp;&amp; [ -x $_folder ] &amp;&amp; PATH=$PATH:$_folder&lt;br /&gt;  export PATH&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;# Add some common paths:&lt;br /&gt;AddPath /usr/bin&lt;br /&gt;AddPath /usr/local/bin&lt;br /&gt;AddPath /opt/somepackage/bin&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 28 Nov 2007 18:38:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4821</guid>
      <author>jmcvetta (Jason McVetta)</author>
    </item>
  </channel>
</rss>
