📄 ch44_03.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 44] 44.3 What's a Shell, Anyway? </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:53:29Z"><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="ch44_01.htm"TITLE="44. Shell Programming for the Uninitiated"><LINKREL="prev"HREF="ch44_02.htm"TITLE="44.2 Writing a Simple Shell Program "><LINKREL="next"HREF="ch44_04.htm"TITLE="44.4 Testing How Your System Executes Files "></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="ch44_02.htm"TITLE="44.2 Writing a Simple Shell Program "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.2 Writing a Simple Shell Program "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 44<BR>Shell Programming for the Uninitiated</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch44_04.htm"TITLE="44.4 Testing How Your System Executes Files "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 44.4 Testing How Your System Executes Files "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7060">44.3 What's a Shell, Anyway? </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50182"></A><ACLASS="indexterm"NAME="UPT-ART-7060-IX-SHELL"></A><ACLASS="indexterm"NAME="AUTOID-50187"></A><ACLASS="indexterm"NAME="AUTOID-50190"></A><ACLASS="indexterm"NAME="AUTOID-50193"></A>A <EMCLASS="emphasis">shell</EM> is a program that interprets your command lines and runs other programs.Another name for the shell is "command interpreter."This article covers the two major UNIX shells, including discussionabout how shells run, how they search for programs, and how they read shellscript files. </P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7060-SECT-1.1">44.3.1 How Shells Run Other Programs </A></H3><PCLASS="para">For each command it runs, a shell does a series of steps.First,if the shell is reading commands from a terminal (interactively),it prints a prompt (such as a <CODECLASS="literal">%</CODE> or <CODECLASS="literal">$</CODE>) and waits for youto type something.Next, the shell reads the command line (like <EMCLASS="emphasis">cat -v afile bfile > cfile</EM>),<SPANCLASS="link">interprets it (<ACLASS="linkend"HREF="ch08_01.htm#UPT-ART-1810"TITLE="What the Shell Does ">8.1</A>, <ACLASS="linkend"HREF="ch08_05.htm"TITLE="Command-Line Evaluation ">8.5</A>)</SPAN>,and runs that command line.When the command finishes running (unless the command is<SPANCLASS="link">in the background (<ACLASS="linkend"HREF="ch01_26.htm"TITLE="What a Multiuser System Can Do for You ">1.26</A>, <ACLASS="linkend"HREF="ch01_27.htm"TITLE="How Background Processing Works ">1.27</A>)</SPAN>),the shell is ready to read another command line.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7060-SECT-1.2">44.3.2 Interactive Use vs. Shell Scripts </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50207"></A>A shell can read command lines from a terminal or it can read them froma file.When you put command lines into a file, that file is called a<SPANCLASS="link"><EMCLASS="emphasis">shell script</EM> (<ACLASS="linkend"HREF="ch44_02.htm"TITLE="Writing a Simple Shell Program ">44.2</A>)</SPAN>or shell program.The shell handles the shell script just as it handles the commands you type from a terminal (though it doesn't print the <CODECLASS="literal">%</CODE> or <CODECLASS="literal">$</CODE> prompts).With this information, you already know how to write simple shell scripts - just put commands in a file and feed them to the shell!</P><PCLASS="para">In addition though, there are a number of programming constructs that makeit possible to write shell programs that are much more powerful than just alist of commands.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7060-SECT-1.3">44.3.3 Types of Shells </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50217"></A><ACLASS="indexterm"NAME="AUTOID-50218"></A><ACLASS="indexterm"NAME="UPT-ART-7060-IX-SHELLS"></A>There are two main kinds of shells in UNIX:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50226"></A><ACLASS="indexterm"NAME="AUTOID-50228"></A><ACLASS="indexterm"NAME="AUTOID-50231"></A>The <EMCLASS="emphasis">C shell</EM> (<EMCLASS="emphasis">csh</EM>) is especially goodfor working on a terminal.<EMCLASS="emphasis">csh</EM> will read shell scripts and has some useful features forprogrammers. Unfortunately,it has some<SPANCLASS="link">quirks (<ACLASS="linkend"HREF="ch47_02.htm"TITLE="C Shell Programming Considered Harmful ">47.2</A>)</SPAN>that can makeshell programming tough.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50240"></A><ACLASS="indexterm"NAME="AUTOID-50243"></A><ACLASS="indexterm"NAME="AUTOID-50245"></A>The <EMCLASS="emphasis">Bourne shell</EM> (<EMCLASS="emphasis">sh</EM>) and shells like itare probably used more often for shell programming.(Some newer <EMCLASS="emphasis">sh</EM>-like shells, including<SPANCLASS="link"><EMCLASS="emphasis">ksh</EM> and <EMCLASS="emphasis">bash</EM> (<ACLASS="linkend"HREF="ch01_08.htm"TITLE="There Are Many Shells ">1.8</A>)</SPAN>,combinehandy interactive C shell-like features with Bourne shell syntax.)</P></LI></UL></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7060-SECT-1.4">44.3.4 Shell Search Paths </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50257"></A><ACLASS="indexterm"NAME="AUTOID-50259"></A><ACLASS="indexterm"NAME="AUTOID-50262"></A><ACLASS="indexterm"NAME="AUTOID-50266"></A>As article<ACLASS="xref"HREF="ch08_07.htm"TITLE="Setting Your Search Path ">8.7</A>explains, if the shell is trying to run a command andthe command isn't built-in to the shell itself, it looks in a list ofdirectories called a <EMCLASS="emphasis">search path</EM>.UNIX systems have standard directories with names like <EMCLASS="emphasis">/bin</EM> and<EMCLASS="emphasis">/usr/bin</EM> that hold standard UNIX programs.Almost everyone's search path has these directories.</P><PCLASS="para">If you do much shell programming, you should makea directory on your account for executable files.Most people name theirs <EMCLASS="emphasis">bin</EM> and put it under the home directory.See article<ACLASS="xref"HREF="ch04_02.htm"TITLE="A bin Directory for Your Programs and Scripts ">4.2</A>.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7060-SECT-1.5">44.3.5 Bourne Shell Used Here </A></H3><PCLASS="para">Most serious shell programmers write their scripts for the Bourne shell.So do we.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50280"></A><ACLASS="indexterm"NAME="AUTOID-50283"></A>Newer Bourne shells have features - like<SPANCLASS="link">shell functions (<ACLASS="linkend"HREF="ch10_09.htm"TITLE="Shell Functions ">10.9</A>)</SPAN>,an <EMCLASS="emphasis">unset</EM>command for shell variables, and others - that the earlier <EMCLASS="emphasis">Version 7</EM>Bourne shell didn't.Most scripts in this book are written to work on all Bourne shells,though - for portability, the scripts don't use these new features.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50290"></A><ACLASS="indexterm"NAME="AUTOID-50293"></A><ACLASS="indexterm"NAME="AUTOID-50296"></A>For the rest ofthese introductory articles,it may be easier if you have a terminalclose by so you can try the examples.If your account uses the Bourne shell or one of its relatives (<EMCLASS="emphasis">ksh</EM>,<EMCLASS="emphasis">bash</EM>, etc.), your prompt probably has a dollar sign (<CODECLASS="literal">$</CODE>) init.If your account isn't running the Bourne shell, start one by typing <CODECLASS="literal">sh</CODE>.Your prompt should change to a dollar sign (<CODECLASS="literal">$</CODE>).You'll be using the Bourne shell until you typeCTRL-dat the start of aline:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>sh</B></CODE>$ $ <ICLASS="lineannotation">...Enter commands...</I>$ <KBDCLASS="keycap">[CTRL-d]</KBD>%</PRE></BLOCKQUOTE></P></DIV><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="ch44_02.htm"TITLE="44.2 Writing a Simple Shell Program "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.2 Writing a Simple Shell Program "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="ch44_04.htm"TITLE="44.4 Testing How Your System Executes Files "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 44.4 Testing How Your System Executes Files "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">44.2 Writing a Simple Shell Program </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">44.4 Testing How Your System Executes Files </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 + -