home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 14.13 Which Directory Am I in, Really? Chapter 14
Moving Around in a Hurry
Next: 15. Wildcards

14.14 Automatic Setup When You Enter/Exit a Directory

If 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 -O ).

Here's a sample .enter.csh file:


# Save previous umask; reset in .exit.csh:
set prevumask=`umask`

# Let everyone in the group edit my files here:
umask 002
echo ".enter.csh: setting umask to 002"
# Prompt (with blank line before) to keep me awake:
set prompt="\
% "

and the .exit.csh to go with it:


# .enter.csh file may put old umask in shell variable:
if ($?prevumask) then
   umask $prevumask

   echo ".exit.csh: setting umask to $prevumask"
   unset prevumask
# Reminder to come back here if need to:
echo "If you didn't check in the RCS files, type 'cd $cwd'."
# Set generic prompt (setprompt alias comes from .cshrc file):

NOTE: The umask set in the .enter file for some directory will also set the permissions for files you create in other directories with commands that use pathnames - like vi  / somedir / somefile .

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:

source ~/.global_enter

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 .enter. $user (6.9 ) (if your UNIX has a 14-character filename limit, you'll need a shorter name).

- JP

Previous: 14.13 Which Directory Am I in, Really? UNIX Power Tools Next: 15. Wildcards
14.13 Which Directory Am I in, Really? Book Index 15. Wildcards

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System