Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] Help Bash Help You



What I use as my shell is not bash, I use zsh, but I think my
technique is quite useful for bash users so I'm gonna tell my zsh's
configuration files.

First of all, .zlogin which is read as it's a login shell has the
simple stuff as following:

  for x in ~/.profile.d*; do
    if [ -d $x ]; then
      for i in $x/*.sh; do
        if [ -f $i ]; then
          source $i
        fi
      done
    fi
  done

In ~/.profile.d you can put any files that you want to configure. If
you used sh, bash, or any other shells which has a sh syntax
compatibility like ksh, zsh, whatever, you would put files end with
".sh" suffix. In csh or tcsh you would put ".csh" suffixed files. For
example java.sh for java, mysql.sh for MySQL.

For instance I have two important files which are "AA_functions.sh"
and "ENV.sh".

AA_functions.sh which has useful functions that are used in
other files in ~/.profile.d is as following:

  add_env(){
    env_name=$1
    shift

    for i in $@; do
      if ! dirs=`eval echo $i` > /dev/null 2>&1; then
        continue
      fi

      for i in `eval echo $dirs`; do
        if eval echo \$$env_name | egrep '(\:|^)'$i'(\:|$)' >/dev/null 2>&1; then
          continue
        fi

        if [ -d $i ]; then
          eval $env_name=\$$env_name:$i
        fi
      done
    done
  }

  clean_env(){
    for i in $@; do
      eval clean_env_tmp=\$$i
      clean_env_tmp=`echo $clean_env_tmp | sed -e "s/^\://" -e "s/\:\:/:/g"`
      eval $i=$clean_env_tmp
    done
  }

add_env can add elements to an environment variable like PATH or
something like that.

clean_env is a function clean environment variables.

And a part of ENV.sh setting up the PATH environment variable is as
following:

  PATH=
  add_env PATH "${HOME}/bin"
  add_env PATH "/usr/local/bin" "/usr/local/sbin"
  add_env PATH "/usr/local/*/bin" "/usr/local/*/sbin"
  add_env PATH "/opt/local/bin" "/opt/local/sbin"
  add_env PATH "/opt/local/*/bin" "/opt/local/*/sbin"
  add_env PATH "/usr/ucb"
  add_env PATH "/bin" "/sbin"
  add_env PATH "/usr/bin" "/usr/sbin" "/usr/*/bin" "/usr/*/sbin"
  add_env PATH "/opt/*/bin" "/opt/*/sbin"
  clean_env PATH
  export PATH

What is a benefit of that system is that add_env can evaluate meta
characters of arguments. If you have /usr/local/apache/bin,
/usr/local/sendmain/bin and /usr/local/squid/bin, you even needn't
change ENV.sh. add_env can add the PATH environment variable these
directories automatically.

In case you want to override configurations you can create a new
~/.profile.d directory. For example ~/.profile.d-solaris for Solaris
environment. In ~/.profile.d you should write common settings.

--
Masato


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links