Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

add a folder to PATH, but not if it is already in PATH

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.

The snippet below nicely handles this problem. It has been tested on Solaris and Linux.


AddPath ()
# Add argument to $PATH if:
# - it is not already present
# - it is a directory
# - we have execute permission on it
#
# This snippet is public domain; you may use it freely.  Death to copyright, patents, 
# and all other forms of intellectual monopoly.  
#
{
  _folder=$1
  echo " $PATH " | sed 's/:/ /g' | grep " $_folder " > /dev/null
  [ $? -ne 0 ] && [ -d $_folder ] && [ -x $_folder ] && PATH=$PATH:$_folder
  export PATH
}

# Add some common paths:
AddPath /usr/bin
AddPath /usr/local/bin
AddPath /opt/somepackage/bin

bash aliases

For old duffers like me who were used to earlier shells when bash came along.

# These save typing, I use them a lot

alias sd='cd -' > /dev/null
alias cx='chmod +x'
alias which="type -path"

# This goes back to Unix v7, some time around 1985 I think.

alias lf='ls -CF'

# From my 'C' shell days. Those were almost 20 years ago as well. Sigh.

alias h='fc -l -20'
alias r='fc -s'

# Because real men use 'vi' and why use character mode if you have X?

alias vi='gvim'

.inputrc to make bash command-line editing like ksh

I was used to ksh and vi when bash came along, so I wanted the behaviour to remain the same. Some may think this odd.

set editing-mode vi
set keymap vi

Basename in ksh without using /usr/bin/basename

Faster than $( basename ) or `basename`
BASENAME=${0##*/}
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS