31.13. 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 do 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. Go to http://examples.oreilly.com/upt3 for more information on: csh_init, sh_init alias cd 'if (-o .exit.csh) source .exit.csh; chdir \!*; if (-o .enter.csh) source .enter.csh' function 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 sourced (Section 35.29) into your current shell before you leave the old directory. As you enter the new directory, a .enter file will be read if it exists. If you use pushd and popd (Section 31.7), 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). Here's a sample .enter.csh file: Go to http://examples.oreilly.com/upt3 for more information on: .enter.csh, .enter.sh # 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="\ $cwd - PROJECT DEVELOPMENT DIRECTORY. EDIT CAREFULLY...\ % " Here's the .exit.csh to go with it: Go to http://examples.oreilly.com/upt3 for more information on: .exit.csh, .exit.sh setprompt Section 4.7 # .enter.csh file may put old umask in shell variable: if ($?prevumask) then umask $prevumask echo ".exit.csh: setting umask to $prevumask" unset prevumask endif # 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): setprompt 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, as well as the risk of the files getting out of sync, by making hard links (Section 10.4) between the files. If the directories are on different filesystems, you'll have to use a symbolic link (Section 10.4) -- though that probably won't save much 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 Section 35.29 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. Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|