⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch44_02.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 44] 44.2 Writing a Simple Shell Program </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:53:26Z"><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_01.htm"TITLE="44.1 Everyone Should Learn Some Shell Programming "><LINKREL="next"HREF="ch44_03.htm"TITLE="44.3 What's a Shell, Anyway? "></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_01.htm"TITLE="44.1 Everyone Should Learn Some Shell Programming "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.1 Everyone Should Learn Some Shell Programming "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_03.htm"TITLE="44.3 What's a Shell, Anyway? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 44.3 What's a Shell, Anyway? "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7050">44.2 Writing a Simple Shell Program </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50029"></A><ACLASS="indexterm"NAME="AUTOID-50032"></A><ACLASS="indexterm"NAME="AUTOID-50035"></A><ACLASS="indexterm"NAME="AUTOID-50038"></A><ACLASS="indexterm"NAME="AUTOID-50041"></A>A shell script need be no more than a complex command line saved in afile. For example, let's assume that you'd like a compact list ofall the users who are currently logged in on the system.</P><PCLASS="para">A command like this might do the trick:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>who | cut -c1-8 | sort -u | pr -l1 -8 -w78 -t</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">A list of logged-in users should come out in columns, lookingsomething like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">abraham  appleton biscuit  charlie  charlott fizzie   howard   howiehstern   jerry    kosmo    linda    ocshner  peterson root     rosssutton   yuppie</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50052"></A>We used four UNIX commands joined with pipes:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para"><SPANCLASS="link"><CODECLASS="literal">who</CODE> (<ACLASS="linkend"HREF="ch51_04.htm"TITLE="Who's On? ">51.4</A>)</SPAN>gives a list of all users.</P></LI><LICLASS="listitem"><PCLASS="para"><SPANCLASS="link"><CODECLASS="literal">cut -c1-8</CODE> (<ACLASS="linkend"HREF="ch35_14.htm"TITLE="Cutting Columns or Fields with cut ">35.14</A>)</SPAN>outputs columns 1-8 of the <EMCLASS="emphasis">who</EM> output&nbsp;- theusernames.If your system doesn't have <EMCLASS="emphasis">cut</EM>, use the command<SPANCLASS="link"><CODECLASS="literal">colrm&nbsp;9</CODE> (<ACLASS="linkend"HREF="ch35_15.htm"TITLE="Cutting Columns with colrm ">35.15</A>)</SPAN>.</P></LI><LICLASS="listitem"><PCLASS="para">The<SPANCLASS="link"><CODECLASS="literal">sort -u</CODE> (<ACLASS="linkend"HREF="ch36_06.htm"TITLE="Miscellaneous sort Hints ">36.6</A>)</SPAN>puts names in order and takes out names of userswho are logged on more than once.</P></LI><LICLASS="listitem"><PCLASS="para">The<SPANCLASS="link"><CODECLASS="literal">pr -l1 -8 -w78 -t</CODE> (<ACLASS="linkend"HREF="ch35_17.htm"TITLE="Making Text in Columns with pr ">35.17</A>, <ACLASS="linkend"HREF="ch43_07.htm"TITLE="Quick-and-Dirty Formatting Before Printing ">43.7</A>)</SPAN>takes the list of usernames, one perline, and makes it into 8 columns on 78-character-wide lines.(The <CODECLASS="literal">-l1</CODE> is the lowercase letter <EMCLASS="emphasis">L</EM>followed by the digit <EMCLASS="emphasis">1</EM>.)</P></LI></OL><PCLASS="para">If you wanted to do this frequently, wouldn't it be better if all youhad to do was type something like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>loggedin</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">to get the same result?  Here's how:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">Start your favorite text editor (Emacs, <EMCLASS="emphasis">vi</EM>, whatever)on a new file named <EMCLASS="emphasis">loggedin</EM>.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50091"></A>If your system supports the<SPANCLASS="link">special <CODECLASS="literal">#!</CODE> notation (<ACLASS="linkend"HREF="ch44_04.htm"TITLE="Testing How Your System Executes Files ">44.4</A>)</SPAN>,the first line of the script file should be:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">#!/bin/sh</PRE></BLOCKQUOTE></P><PCLASS="para">Otherwise, leave the first line blank.(When the first line of a script is blank, most shells will start aBourne shell to read it.Articles<ACLASS="xref"HREF="ch45_02.htm"TITLE="The Story of  : #  #! ">45.2</A>and<ACLASS="xref"HREF="ch45_06.htm"TITLE="Making Sure Your Script Runs with Bourne Shell, Without #! ">45.6</A>have more information.)</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50103"></A><ACLASS="indexterm"NAME="AUTOID-50106"></A>I think that the second line of a shell script should always be a commentto explain what the script does.(Use more than one line, if you want.)A comment starts with a hash mark (<CODECLASS="literal">#</CODE>); all characters after iton the line are ignored:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># loggedin - list logged-in users, once per user, in 8 columns</PRE></BLOCKQUOTE></P><PCLASS="para">Put this on the third line, just like you did on the command line:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">who | cut -c1-8 | sort -u | pr -l1 -8 -w78 -t</PRE></BLOCKQUOTE></P><PCLASS="para">(As I explained earlier,you might need <EMCLASS="emphasis">colrm</EM> instead of <EMCLASS="emphasis">cut</EM>.)</P></LI><LICLASS="listitem"><PCLASS="para">Save the file and leave the editor. You've just written a shellscript.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50122"></A><ACLASS="indexterm"NAME="AUTOID-50125"></A><ACLASS="indexterm"NAME="AUTOID-50128"></A>Next, you need to make the shell script executable.The<SPANCLASS="link"><EMCLASS="emphasis">chmod</EM> (<ACLASS="linkend"HREF="ch22_07.htm"TITLE="Using chmod to Change File Permission ">22.7</A>)</SPAN>(change mode) command is used to change permissions on a file.The plus sign followed by an x (<CODECLASS="literal">+x</CODE>) makes the file executable:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>chmod +x loggedin</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50140"></A><ACLASS="indexterm"NAME="AUTOID-50143"></A><ACLASS="indexterm"NAME="AUTOID-50146"></A><ACLASS="indexterm"NAME="AUTOID-50149"></A>If your account uses the C shell, you'll need to reset its command searchtable.To do that, type:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch04_02.htm"TITLE="4.2 A bin Directory for Your Programs and Scripts ">rehash</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>rehash</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">Finally, try the script.Just type its name and it should run:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>loggedin</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If that doesn't run, your current directory may not be in your shell'scommand search path.In that case, try this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>./loggedin</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If it still doesn't work, and you started the first line of your scriptwith <CODECLASS="literal">#!</CODE>, be sure that the Bourne shell's pathname on that line (like<CODECLASS="literal">/bin/sh</CODE>) is correct.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-50171"></A>If you want to run the script from somewhere other than the currentdirectory, or if you want other programs and scripts you write to beable to use it, you need to put it in a directory that's in your <SPANCLASS="link">search path (<ACLASS="linkend"HREF="ch08_07.htm"TITLE="Setting Your Search Path ">8.7</A>)</SPAN>.If you're the only person who plans to use the script, you should put it inyour personal<SPANCLASS="link"><EMCLASS="emphasis">bin</EM> directory (<ACLASS="linkend"HREF="ch04_02.htm"TITLE="A bin Directory for Your Programs and Scripts ">4.2</A>)</SPAN>.Otherwise, you might ask your system administrator if there's asystemwide directory for local commands.</P></LI></OL><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_01.htm"TITLE="44.1 Everyone Should Learn Some Shell Programming "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 44.1 Everyone Should Learn Some Shell Programming "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_03.htm"TITLE="44.3 What's a Shell, Anyway? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 44.3 What's a Shell, Anyway? "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">44.1 Everyone Should Learn Some Shell Programming </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.3 What's a Shell, Anyway? </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 &amp; 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 + -