📄 ch08_08.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 8] 8.8 A Directory for Commands You Shouldn't Run </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:34:20Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch08_01.htm"TITLE="8. How the Shell Interprets What You Type"><LINKREL="prev"HREF="ch08_07.htm"TITLE="8.7 Setting Your Search Path "><LINKREL="next"HREF="ch08_09.htm"TITLE="8.9 Wildcards Inside of Aliases "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch08_07.htm"TITLE="8.7 Setting Your Search Path "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.7 Setting Your Search Path "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 8<BR>How the Shell Interprets What You Type</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch08_09.htm"TITLE="8.9 Wildcards Inside of Aliases "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.9 Wildcards Inside of Aliases "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7865">8.8 A Directory for Commands You Shouldn't Run </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-7993"></A><ACLASS="indexterm"NAME="AUTOID-7996"></A>How can you keep yourself from running <EMCLASS="emphasis">some</EM> of the commands ina directory in your<SPANCLASS="link">search path (<ACLASS="linkend"HREF="ch06_04.htm"TITLE="The PATH Environment Variable ">6.4</A>, <ACLASS="linkend"HREF="ch06_05.htm"TITLE="PATH and path ">6.5</A>)</SPAN>?For example, I use several different computers.I read and store my<SPANCLASS="link">electronic mail (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>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 Idon't want the mail-reading commands to work on my account there.</P><PCLASS="para">You might work on a project with shared filesystems where some commands willonly work on certain computers.How can you stop the commands from being run accidentally on computerswhere they shouldn't be?There's a beginner on the system who shouldn't be running dangerouscommands.How can you stop him from using just those commands?</P><PCLASS="para">You could make<SPANCLASS="link">aliases (<ACLASS="linkend"HREF="ch10_02.htm"TITLE="Aliases for Common Commands ">10.2</A>)</SPAN>for those commands that just <EMCLASS="emphasis">echo</EM> a message to the terminal.But having tens or hundreds of aliases like that can be a real headache.</P><PCLASS="para">Here's how I solved my problem.On all of my computers, the commands for the email system I use (calledMH) are stored in the directory <EMCLASS="emphasis">/usr/local/mh</EM>.I make a directory named <EMCLASS="emphasis">no_run.</EM><CODECLASS="userinput"><B>hostname</B></CODE> that has shortshell scripts.The scripts have the same names as the the commands in<EMCLASS="emphasis">/usr/local/mh</EM> that I <EMCLASS="emphasis">don't</EM> wantto run.On the computers where I don't want to run those commands, I put the<EMCLASS="emphasis">no_run.</EM><CODECLASS="userinput"><B>hostname</B></CODE> directory before the <EMCLASS="emphasis">/usr/local/mh</EM>directory in my path:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch47_06.htm"TITLE="47.6 Quick Reference: C Shell switch Statement ">switch</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">switch (`uname -n`)case cruncher: set path=( ... ~/no_run.cruncher /usr/local/mh ... ) ...</PRE></TD></TR></TABLE></P><PCLASS="para">(A<SPANCLASS="link">per-host setup file (<ACLASS="linkend"HREF="ch02_13.htm"TITLE="A .cshrc.$HOST File for Per Host Setup ">2.13</A>)</SPAN>can help, too.)When I try to use a command that I shouldn't, the shell will find theshell script in the <EMCLASS="emphasis">no_run</EM> directory before the real command in the<EMCLASS="emphasis">mh</EM> directory.The shell script rings the bell, prints a message with its own name andthe name of the computer to use, then quits:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>inc</B></CODE><EMCLASS="emphasis">beep...</EM> You can't run inc here. Use sunspot.</PRE></BLOCKQUOTE></P><PCLASS="para"> To save disk space, the shell scripts in the <EMCLASS="emphasis">no_run</EM> directory are all<SPANCLASS="link">hard links (<ACLASS="linkend"HREF="ch18_04.htm"TITLE="More About Links ">18.4</A>)</SPAN>to each other:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls -li no_run.cruncher</B></CODE> ...270156 -rwxr-xr-x 31 jerry 82 Jun 12 09:10 inc270156 -rwxr-xr-x 31 jerry 82 Jun 12 09:10 mark270156 -rwxr-xr-x 31 jerry 82 Jun 12 09:10 msgchk <ICLASS="lineannotation">...a total of 31 links...</I></PRE></BLOCKQUOTE></P><PCLASS="para"> The script uses the command<SPANCLASS="link"><CODECLASS="literal">basename</CODE> <CODECLASS="literal">$0</CODE> (<ACLASS="linkend"HREF="ch45_18.htm"TITLE="Using basename and dirname ">45.18</A>)</SPAN>to include its (current) command name with the warning message:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch44_07.htm"TITLE="44.7 Exit Status of UNIX Processes ">1</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"> #! /bin/shecho "\007You can't run `basename $0` here. Use sunspot." 1>&2exit 1</PRE></TD></TR></TABLE></P><PCLASS="para">The <CODECLASS="literal">\007</CODE> rings the bell on my version of <EMCLASS="emphasis">echo</EM>; your versionmight need a <CODECLASS="literal">\a</CODE> or a<SPANCLASS="link">real CTRL-g character (<ACLASS="linkend"HREF="ch45_35.htm"TITLE="Using a Control Character in a Script ">45.35</A>)</SPAN>instead.Article<ACLASS="xref"HREF="ch16_15.htm"TITLE="Script with a :-) for UNIX Converts: dir, ..., ... ">16.15</A>shows a similar script.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch08_07.htm"TITLE="8.7 Setting Your Search Path "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.7 Setting Your Search Path "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch08_09.htm"TITLE="8.9 Wildcards Inside of Aliases "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.9 Wildcards Inside of Aliases "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">8.7 Setting Your Search Path </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">8.9 Wildcards Inside of Aliases </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -