Never been to CodeSnippets 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 (or not, you can keep them private!)

add2path

# version 1

function add2path() {

   declare defaultpath path pathvar

   # add the new path at the beginning of $PATHVARIABLE
   if [[ "${1}" == '-b'  ]]; then
      if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi
      path="${3}:${2}"
      #path="${path// /}"   # remove spaces
      printf "%s" "${path//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' 

   # add the new path at the end of $PATHVARIABLE
   elif [[ "${1}" == '-e'  ]]; then
      if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi
      # first remove identical path from $PATHVARIABLE (path identical with ${3}) if necessary
      pathvar="$(printf "%s" "${2//:/$'\n'}" | /usr/bin/egrep -v "^${3}$" | /usr/bin/tr '\n' ':')"
      path="${pathvar}:${3}"
      path="${path//::/:}"
      #path="${path// /}"   # remove spaces
      printf "%s" "${path//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' 

   # default: add the new path at the beginning of $PATHVARIABLE
   elif [[ $# -eq 2 ]]; then
      if [[ ! -d "${2}" ]]; then printf "%s" "${1}"; return 1; fi
      path="${2}:${1}"
      #path="${path// /}"       # remove spaces
      printf "%s" "${path//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':'
 
   else

defaultpath='
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin 
/usr/local/sbin
/usr/local/lib
/usr/local/include
/usr/local/man
/opt/local/bin
/opt/local/sbin
/opt/local/lib
/opt/local/include
/opt/local/man
/usr/X11R6
/usr/X11R6/bin
/usr/X11R6/include
/usr/X11R6/lib
/usr/X11R6/man
'

      defaultpath="${defaultpath// /}"        # remove spaces
      defaultpath="${defaultpath//$'\n'/:}"   # convert newlines into colons
      defaultpath="${defaultpath#:}"          # cut off leading colon
      defaultpath="${defaultpath%:}"          # cut off trailing colon
      printf "%s" "${defaultpath}"
      return 1   

   fi

   return 0
}

# usage: 
# add2path [$PATHVARIABLE] /path/to/dir
# add2path [-b|-e] [$PATHVARIABLE] /path/to/dir


# store the original $PATH
OPATH="${PATH}"

# create test directories
testdir="${HOME}/Desktop/testdir"
/bin/mkdir -p "${testdir}"
for ((i=0;i<=5;i++)) { /bin/mkdir -p "${testdir}${i}"; }

# list test directories
echo "${testdir}"
for ((i=0;i<=5;i++)) { echo "${testdir}${i}"; }


add2path "${PATH}" "${testdir}" | tr ':' '\n' | nl
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

export PATH="$(add2path "${PATH}" "${testdir}")"
printf "%s\n" "${PATH}" | tr ':' '\n' | nl


for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}")"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path -b "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl


add2path | tr ':' '\n' | nl

add2path abc | tr ':' '\n' | nl

add2path -b "${PATH}" "${testdir}" | tr ':' '\n' | nl
add2path -e "${PATH}" "${testdir}" | tr ':' '\n' | nl


# restore original $PATH
export PATH="${OPATH}"
printf "%s\n" "${PATH}" | tr ':' '\n' | nl


#--------------------------------------------------------------


# version 2

function add2path() {

   # add the new path at the beginning of $PATHVARIABLE
   if [[ "${1}" == '-b'  ]]; then
      if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi
      printf "%s" "${3}:${2}" | /usr/bin/sed -E -e '/^:+/s/^:+//' -e '/:+$/s/:+$//' -e '/::+/s/::+/:/g' | /usr/bin/tr ':' '\n' | \
         /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' | /usr/bin/sed -E -e '/:+$/s/:+$//' 

   # add the new path at the end of $PATHVARIABLE
   elif [[ "${1}" == '-e'  ]]; then
      if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi

      # first remove identical path from $PATHVARIABLE (path identical with ${3}) if necessary
      pathvar="$(printf "%s" "${2}" | /usr/bin/tr ':' '\n' | /usr/bin/egrep -v "^${3}$" | /usr/bin/tr '\n' ':')"

      printf "%s" "${pathvar}:${3}" | /usr/bin/sed -E -e '/^:+/s/^:+//' -e '/:+$/s/:+$//' -e '/::+/s/::+/:/g' | /usr/bin/tr ':' '\n' | \
         /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' | /usr/bin/sed -E -e '/:+$/s/:+$//' 

   # default: add the new path at the beginning of $PATHVARIABLE
   elif [[ $# -eq 2 ]]; then
      if [[ ! -d "${2}" ]]; then printf "%s" "${1}"; return 1; fi
      printf "%s" "${2}:${1}" | /usr/bin/sed -E -e '/^:+/s/^:+//' -e '/:+$/s/:+$//' -e '/::+/s/::+/:/g' | /usr/bin/tr ':' '\n' | \
         /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' | /usr/bin/sed -E -e '/:+$/s/:+$//' 

   else

path='
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin 
/usr/local/sbin
/usr/local/lib
/usr/local/include
/usr/local/man
/opt/local/bin
/opt/local/sbin
/opt/local/lib
/opt/local/include
/opt/local/man
/usr/X11R6
/usr/X11R6/bin
/usr/X11R6/include
/usr/X11R6/lib
/usr/X11R6/man
'

      printf "%s" "${path// /}" | /usr/bin/tr '\n' ':' | /usr/bin/sed -E '/^:|:$/s/^:|:$//g'

      return 1   

   fi

   return 0
}


# usage: 
# add2path [$PATHVARIABLE] /path/to/dir
# add2path [-b|-e] [$PATHVARIABLE] /path/to/dir


OPATH="${PATH}"

testdir="${HOME}/Desktop/testdir"
/bin/mkdir -p "${testdir}"
for ((i=0;i<=5;i++)) { /bin/mkdir -p "${testdir}${i}"; }


printf "%s\n" "${PATH}" | tr ':' '\n' | nl
export PATH="$(add2path "${PATH}" "${testdir}" )"
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl


for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path -b "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl

for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; }
printf "%s\n" "${PATH}" | tr ':' '\n' | nl


export PATH="${OPATH}"
printf "%s\n" "${PATH}" | tr ':' '\n' | nl


#---------------------------------------------------------------


function remove_from_path() {

   declare defaultpath newpath

   if [[ $# -ne 2 ]]; then

defaultpath='
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin 
/usr/local/sbin
/usr/local/lib
/usr/local/include
/usr/local/man
/opt/local/bin
/opt/local/sbin
/opt/local/lib
/opt/local/include
/opt/local/man
/usr/X11R6
/usr/X11R6/bin
/usr/X11R6/include
/usr/X11R6/lib
/usr/X11R6/man
'

      defaultpath="${defaultpath// /}"        # remove spaces
      defaultpath="${defaultpath//$'\n'/:}"   # convert newlines into colons
      defaultpath="${defaultpath#:}"          # cut off leading colon
      defaultpath="${defaultpath%:}"          # cut off trailing colon
      printf "%s" "${defaultpath}"

      return 1
   fi 

   if [[ ! -d "${2}" ]]; then printf "%s" "${1}"; return 1; fi

   newpath="$(printf "%s" "${1//:/$'\n'}" | /usr/bin/egrep -v "^${2}$" | /usr/bin/tr '\n' ':')"
   #newpath="${newpath// /}"        # remove spaces
   newpath="${newpath#:}"          # cut off leading colon
   newpath="${newpath%:}"          # cut off trailing colon
   #printf "%s" "${newpath//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' 
   printf "%s" "${newpath}"

   return 0
}

printf "%s\n" "${PATH}" | tr ':' '\n' | nl

export PATH="$(remove_from_path "${PATH}" "/opt/local/bin" )"
export PATH="$(remove_from_path "${PATH}" "/opt/local/xyz" )"
export PATH="$(remove_from_path "${PATH}" "" )"
export PATH="$(remove_from_path "${PATH}" )"

printf "%s\n" "${PATH}" | tr ':' '\n' | nl


#---------------------------------------------------------------


man bash 2>/dev/null | less -p "Functions are executed"

# Functions are executed in the context of the current shell; no new process is created to 
# interpret  them  (contrast  this with the execution of a shell script).

Compiling GNU coreutils on Mac OS X

# See:
# - http://www.gnu.org/software/coreutils/
# - http://en.wikipedia.org/wiki/GNU_Core_Utilities
# - http://homepage.mac.com/matsuan_tamachan/software/CoreUtils.html
# - http://www.mail-archive.com/bug-gnulib@gnu.org/msg08420.html

# In addition see:
# The Heirloom Project, http://heirloom.sourceforge.net
# http://homepage.mac.com/stefan.tramm/iWiki/HeirloomNotes.html
# http://codesnippets.joyent.com/posts/show/1632


path='
/usr/local/bin 
/usr/local/sbin
/usr/local/lib
/usr/local/include
/usr/local/man
/usr/bin
/bin
/usr/sbin
/sbin
'

path="$(printf "%s" "${path// /}" | /usr/bin/tr '\n' ':' | /usr/bin/sed -E 's/^:|:$//g')"

export PATH="${path}"


echo $PATH
printf "%s\n" "$PATH" | tr ':' '\n'


alias curl=/usr/bin/curl
alias ls=/bin/ls
alias make=/usr/bin/make
alias nl=/usr/bin/nl
alias port=/opt/local/bin/port
alias tr=/usr/bin/tr

alias curl ls make nl port tr


# first compile & install gawk on Mac OS X
# http://www.gnu.org/software/gawk/

cd ~/Desktop
curl -L -O http://ftp.gnu.org/pub/gnu/gawk/gawk-3.1.6.tar.gz
tar -xzf gawk-3.1.6.tar.gz
cd gawk-3.1.6
./configure --help
./configure --disable-nls --prefix=/usr/local
make
/usr/bin/sudo /usr/bin/make install

/usr/local/bin/gawk --version


# alternative gawk installation
# port info gawk
# /usr/bin/sudo /opt/local/bin/port install gawk
# port installed | grep gawk
# /opt/local/bin/gawk --version
# export PATH="/opt/local/bin:${path}"


# compile & install the GNU coreutils

/usr/sbin/gcc_select -h
/usr/sbin/gcc_select -l
/usr/sbin/gcc_select      #  gcc version 4.0.1
#/usr/bin/sudo /usr/sbin/gcc_select 3.3

/usr/bin/sw_vers -productVersion    # 10.4.11

cd ~/Desktop
curl -L -O http://ftp.gnu.org/gnu/coreutils/coreutils-6.9.tar.gz
tar -xzf coreutils-6.9.tar.gz
cd coreutils-6.9
./configure --help
./configure --prefix=/usr/local/gnucoreutils
make
/usr/bin/sudo /usr/bin/make install


open -a Finder /usr/local/gnucoreutils
ls -1 /usr/local/gnucoreutils/bin | nl


export PATH="${PATH}:/usr/local/gnucoreutils/bin"
printf "%s\n" "${PATH}" | tr ':' '\n' | nl


# stat
which stat
/usr/bin/stat -x /dev/stdin
/usr/local/gnucoreutils/bin/stat -x /dev/stdin

/usr/local/gnucoreutils/bin/stat --help
/usr/local/gnucoreutils/bin/stat /dev/stdin

/usr/bin/sudo /bin/ln -is /usr/local/gnucoreutils/bin/stat /usr/local/bin/gnustat
which gnustat
gnustat --version


# ls
ls --version
/usr/bin/sudo /bin/ln -is /usr/local/gnucoreutils/bin/ls /usr/local/bin/gnuls
gnuls --version


# seq
which seq   #  /usr/local/gnucoreutils/bin/seq
/usr/bin/sudo /bin/ln -is /usr/local/gnucoreutils/bin/seq /usr/local/bin
which seq   #  /usr/local/bin/seq

seq --version
seq 5 25
seq 5 0.5 10


# For information on MANPATH see man man, man 5 man.conf and
# the "MANPATH /path/to/mandir" entries in: 
# sudo nano +40 /usr/share/misc/man.conf or 
# sudo nano +40 /private/etc/man.conf
# example: MANPATH /usr/local/gnucoreutils/share/man
# (in general make sure there is no MANPATH directory that is a symbolic link to another directory)

/usr/bin/manpath | tr ':' '\n' | nl
export MANPATH="${MANPATH}:/usr/local/gnucoreutils/share/man"
printf "%s\n" "${MANPATH}" | tr ':' '\n'

man seq

man -aW stat
man -aW stat | grep coreutils | xargs man
man -a stat


man 1 info
info --help
info info 2>/dev/null | less -p INFOPATH
export INFOPATH="${INFOPATH}:/usr/local/gnucoreutils/share/info"

Ubuntu and writing Java servlets

Kind of a strange, and I'm sure this wouldn't have happened on any other distribution of Linux, but I spent forever looking through blogs, message boards, and all manner of other places looking for an answer to this question.

The problem was with this little itty bitty smidge of code:
/**
 * hello.java
 */
import javax.servlet.*;

class HelloWorld
{
        public static void main(String[] args)
        {
                System.out.println("Hello World!");
        }
}


Everytime I wanted to compile this, I got something like "Could not find javax.servlet. blah blah" So, eventually, I just pulled an apt-cache search j2 (for j2ee) and saw an entry for libservlet2.4-java. Hmmmmm, what the heck is that I wondered. I installed it and went to the Ubuntu site to look at what files were installed and sure enough:

/usr/share/java/jsp-api-2.0.jar
/usr/share/java/jsp-api.jar
/usr/share/java/servlet-api-2.4.jar
/usr/share/java/servlet-api.jar


Perfect! I added these paths to my CLASSPATH variable in my .bashrc and whamo, I'm in business and flaunting my little HelloWorld.class file all around town.

I hope this helps someone else!