[CentOS] Autorun ability on cd?

Wed Aug 5 14:15:47 UTC 2009
Stephen Harris <lists at spuddy.org>

On Wed, Aug 05, 2009 at 09:49:04AM -0400, James B. Byrne wrote:
> kluges around this.  I do not wish to change the pwd of the shell, I
> just want some way of testing for a certain file and running a
> specific command if it is found when I enter a working tree. If this
> requires testing every directory that I cd to then I can live with
> that.  If instead one can put a script that runs only when one
> enters certain directories then I can live with that as well.
> 
> Is there any way to do this?

Untested, and it depends very much on your shell, but create a function

  cdr()
  {
    cd $1 || exit
    if [ -x ./.myscript ]
    then
      ./.myscript
    fi
  }

Now if you use "cdr" (for Change Directory & Run) it should do what you
want (runs a file called ".myscript" in the new directory if it exists)


This _might_ work...

  cd()
  {
    command cd $1 || exit
    if [ -x ./.myscript ]
    then
      ./.myscript
    fi
  }

OK, I just tested the later under ksh88 and it worked.  You'd need to
test with bash/ksh93/whatever-your-shell-is

  $ cat x1/.myscript 
  echo hello
  $ cd x1
  hello
  $ mkdir x0
  $ cd x0
  $ cd ..
  hello
  $ 

As you can see, everytime I enter the "x1" directory it runs .myscript

-- 

rgds
Stephen