14.14 Automatic Setup When You Enter/Exit a DirectoryIf you work in a lot of different directories, here's a way to make the shell do automatic setup when you enter a directory or cleanup as you leave. We've broken it onto two lines for printing; enter it as one line. On bash , make a shell function instead; there's also a Korn shell version on the disc: alias cd 'if (-o .exit.csh) source .exit.csh; chdir \!*; if (-o .enter.csh) source .enter.csh' cd() { test -r .exit.sh && . .exit.sh builtin cd "$1" # builtin is a bash command test -r .enter.sh && . .enter.sh } Then create .enter.csh and/or .exit.csh files in the directories where you want a custom setup. Bourne-type shell users, make .enter.sh and/or .exit.sh files instead. When you cd to a new directory, an .exit file is source d (44.23 ) into your current shell before you leave the old directory. As you enter the new directory, an .enter file will be read if it exists. If you use pushd and popd (14.6 ) , you'll probably want to make the same kind of aliases or functions for them. The C shell alias tests to be sure you own the files;
this helps to stop other users
from leaving surprises for you!
But if lots of users will be sharing the directory, they may all want to
share the same files - in that case, replace the - o
tests with
-r
(true if the file is readable).
Article
47.4
describes C shell tests like -o
;
in sh
-like shells, use
test
(44.20
)
(the bash
ownership test
operator is Here's a sample .enter.csh file:
and the .exit.csh to go with it:
Can more than one of your directories use the same .enter or .exit file? If they can, you'll save disk space and redundant editing by making hard links (18.4 ) between the files. If the directories are on different filesystems, you'll have to use a symbolic link (18.4 ) - though that probably won't save disk space. If you link the files, you should probably add a comment that reminds you of the links when you make your next edit. When your .enter files get really long, you might be able to put a command like this in them:
where the .global_enter file in your home directory has a procedure that you want to run from a lot of your .enter files. (Same goes for .exit , of course.) One last idea: if a lot of users share the same directory, they can make
files with names like .enter.joanne
, .exit.allan
, and so on.
Your aliases can test for a file named
- |
|