Полезная информация

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 8.7 Setting Your Search Path Chapter 8
How the Shell Interprets What You Type
Next: 8.9 Wildcards Inside of Aliases

8.8 A Directory for Commands You Shouldn't Run

How can you keep yourself from running some of the commands in a directory in your search path (6.4, 6.5)? For example, I use several different computers. I read and store my electronic mail (1.33) on just one computer - on that host, I want to use all the email commands. On the other computers, I want to be able to use mail-sending commands - but I don't want the mail-reading commands to work on my account there.

You might work on a project with shared filesystems where some commands will only work on certain computers. How can you stop the commands from being run accidentally on computers where they shouldn't be? There's a beginner on the system who shouldn't be running dangerous commands. How can you stop him from using just those commands?

You could make aliases (10.2) for those commands that just echo a message to the terminal. But having tens or hundreds of aliases like that can be a real headache.

Here's how I solved my problem. On all of my computers, the commands for the email system I use (called MH) are stored in the directory /usr/local/mh. I make a directory named no_run.hostname that has short shell scripts. The scripts have the same names as the the commands in /usr/local/mh that I don't want to run. On the computers where I don't want to run those commands, I put the no_run.hostname directory before the /usr/local/mh directory in my path:


switch (`uname -n`)
case cruncher:
        set path=( ... ~/no_run.cruncher /usr/local/mh ... )

(A per-host setup file (2.13) can help, too.) When I try to use a command that I shouldn't, the shell will find the shell script in the no_run directory before the real command in the mh directory. The shell script rings the bell, prints a message with its own name and the name of the computer to use, then quits:

% inc
beep... You can't run inc here.  Use sunspot.

To save disk space, the shell scripts in the no_run directory are all hard links (18.4) to each other:

% ls -li no_run.cruncher
270156 -rwxr-xr-x 31 jerry          82 Jun 12 09:10 inc
270156 -rwxr-xr-x 31 jerry          82 Jun 12 09:10 mark
270156 -rwxr-xr-x 31 jerry          82 Jun 12 09:10 msgchk
   ...a total of 31 links...

The script uses the command basename $0 (45.18) to include its (current) command name with the warning message:


#! /bin/sh
echo "\007You can't run `basename $0` here.  Use sunspot." 1>&2
exit 1

The \007 rings the bell on my version of echo; your version might need a \a or a real CTRL-g character (45.35) instead. Article 16.15 shows a similar script.

- JP

Previous: 8.7 Setting Your Search Path UNIX Power ToolsNext: 8.9 Wildcards Inside of Aliases
8.7 Setting Your Search Path Book Index8.9 Wildcards Inside of Aliases

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