OnDir
Synopsis
OnDir
Automatic script execution
On Dir is a program that automatically executes scripts as you traverse directories at a terminal.
Example usage:
alec@cavern:~$ umask
077
alec@cavern:~$ cd public_html
alec@cavern:~/public_html$ umask
022
alec@cavern:~/public_html$ cd
alec@cavern:~$ umask
077
The above is accomplished with the help of ondir, some scripts added to the users (or system) startup scripts and an entry in my ~/.ondirrc for the ~/public_html path.
The entry in my ~/.ondirrc would look like this
enter /home/athomas/public_html
umask 022
leave /home/athomas/public_html
umask 077
In addition to executing scripts in the directory you are entering or leaving, ondir also executes scripts in directories that are traversed on the way to that directory. For example, if you are in your home directory and type:
$ cd ~/public_html/myproject
On Dir scripts for ~/public_html
and ~/public_html/myproject
are executed because these directories are traversed.
You can download On Dir from the bottom of this page.
Tips and Tricks
On Dir has acquired a couple of very nice features over the years, here are a couple. Tilde expansion in paths.
enter ~/public_html
umask 022
leave ~/public_html
umask 077
Expansion of environment variables in the form $VAR
or ${VAR}
.
The environment variable $ONDIRWD
is always set to the directory being
traversed. This is not equivalent to $PWD
, as $PWD
is the destination
directory, whereas $ONDIRWD
is the directory that matched during path
traversal.
enter $HOME/public_html:/opt/movies
echo "You have traversed $ONDIRWD"
Multiple paths per section.
enter /opt/movies:/opt/music
umask 007
leave /opt/movies:/opt/music
umask 077
Regular expressions, including sub-pattern capturing and referencing (sub-patterns are assigned to the environment variables $0 through $9).
enter ~/projects/(ondir|todo)
echo "Welcome to $1"
leave ~/projects/(ondir|todo)
echo "Thanks for visiting $1"