📄 external.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>External Filters, Programs and Commands</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Commands"HREF="part4.html"><LINKREL="PREVIOUS"TITLE="Internal Commands and Builtins"HREF="internal.html"><LINKREL="NEXT"TITLE="Complex Commands"HREF="moreadv.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="internal.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="moreadv.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="EXTERNAL"></A>Chapter 15. External Filters, Programs and Commands</H1><P><ANAME="EXTERNALREF"></A></P><P>Standard UNIX commands make shell scripts more versatile. The power of scripts comes from coupling system commands and shell directives with simple programming constructs.</P><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="BASIC"></A>15.1. Basic Commands</H1><DIVCLASS="VARIABLELIST"><P><B><ANAME="BASICCOMMANDS1"></A>The first commands a novice learns</B></P><DL><DT><ANAME="LSREF"></A><BCLASS="COMMAND">ls</B></DT><DD><P>The basic file <SPANCLASS="QUOTE">"list"</SPAN> command. It is all too easy to underestimate the power of this humble command. For example, using the <TTCLASS="OPTION">-R</TT>, recursive option, <BCLASS="COMMAND">ls</B> provides a tree-like listing of a directory structure. Other useful options are <TTCLASS="OPTION">-S</TT>, sort listing by file size, <TTCLASS="OPTION">-t</TT>, sort by file modification time, <TTCLASS="OPTION">-b</TT>, show escape characters, and <TTCLASS="OPTION">-i</TT>, show file inodes (see <AHREF="moreadv.html#IDELETE">Example 15-4</A>).</P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P> The <ICLASS="FIRSTTERM">ls</I> command returns a non-zero <AHREF="exit-status.html#EXITSTATUSREF">exit status</A> when attempting to list a non-existent file. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ls abc</B></TT> <TTCLASS="COMPUTEROUTPUT">ls: abc: No such file or directory</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $?</B></TT> <TTCLASS="COMPUTEROUTPUT">2</TT></PRE></TD></TR></TABLE> </P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX40"></A><P><B>Example 15-1. Using <ICLASS="FIRSTTERM">ls</I> to create a table of contents for burning a <SPANCLASS="ABBREV">CDR</SPAN> disk</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # ex40.sh (burn-cd.sh) 3 # Script to automate burning a CDR. 4 5 6 SPEED=2 # May use higher speed if your hardware supports it. 7 IMAGEFILE=cdimage.iso 8 CONTENTSFILE=contents 9 DEVICE=cdrom 10 # DEVICE="0,0" For older versions of cdrecord 11 DEFAULTDIR=/opt # This is the directory containing the data to be burned. 12 # Make sure it exists. 13 # Exercise: Add a test for this. 14 15 # Uses Joerg Schilling's "cdrecord" package: 16 # http://www.fokus.fhg.de/usr/schilling/cdrecord.html 17 18 # If this script invoked as an ordinary user, may need to suid cdrecord 19 #+ chmod u+s /usr/bin/cdrecord, as root. 20 # Of course, this creates a security hole, though a relatively minor one. 21 22 if [ -z "$1" ] 23 then 24 IMAGE_DIRECTORY=$DEFAULTDIR 25 # Default directory, if not specified on command line. 26 else 27 IMAGE_DIRECTORY=$1 28 fi 29 30 # Create a "table of contents" file. 31 ls -lRF $IMAGE_DIRECTORY > $IMAGE_DIRECTORY/$CONTENTSFILE 32 # The "l" option gives a "long" file listing. 33 # The "R" option makes the listing recursive. 34 # The "F" option marks the file types (directories get a trailing /). 35 echo "Creating table of contents." 36 37 # Create an image file preparatory to burning it onto the CDR. 38 mkisofs -r -o $IMAGEFILE $IMAGE_DIRECTORY 39 echo "Creating ISO9660 file system image ($IMAGEFILE)." 40 41 # Burn the CDR. 42 echo "Burning the disk." 43 echo "Please be patient, this will take a while." 44 cdrecord -v -isosize speed=$SPEED dev=$DEVICE $IMAGEFILE 45 46 exit $?</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="CATREF"></A><BCLASS="COMMAND">cat</B>, <BCLASS="COMMAND">tac</B></DT><DD><P><BCLASS="COMMAND">cat</B>, an acronym for <ICLASS="WORDASWORD">concatenate</I>, lists a file to <TTCLASS="FILENAME">stdout</TT>. When combined with redirection (<SPANCLASS="TOKEN">></SPAN> or <SPANCLASS="TOKEN">>></SPAN>), it is commonly used to concatenate files. <ANAME="CATUSES"></A> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 # Uses of 'cat' 2 cat filename # Lists the file. 3 4 cat file.1 file.2 file.3 > file.123 # Combines three files into one.</PRE></TD></TR></TABLE> The <TTCLASS="OPTION">-n</TT> option to <BCLASS="COMMAND">cat</B> inserts consecutive numbers before all lines of the target file(s). The <TTCLASS="OPTION">-b</TT> option numbers only the non-blank lines. The <TTCLASS="OPTION">-v</TT> option echoes nonprintable characters, using <SPANCLASS="TOKEN">^</SPAN> notation. The <TTCLASS="OPTION">-s</TT> option squeezes multiple consecutive blank lines into a single blank line.</P><P>See also <AHREF="textproc.html#LNUM">Example 15-27</A> and <AHREF="textproc.html#ROT13">Example 15-23</A>.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><ANAME="CATLESSEFF"></A> In a <AHREF="special-chars.html#PIPEREF">pipe</A>, it may be more efficient to <AHREF="io-redirection.html#IOREDIRREF">redirect</A> the <TTCLASS="FILENAME">stdin</TT> to a file, rather than to <BCLASS="COMMAND">cat</B> the file. </P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 cat filename | tr a-z A-Z 2 3 tr a-z A-Z < filename # Same effect, but starts one less process, 4 #+ and also dispenses with the pipe.</PRE></TD></TR></TABLE> </P></TD></TR></TABLE></DIV><P><BCLASS="COMMAND">tac</B>, is the inverse of <ICLASS="WORDASWORD">cat</I>, listing a file backwards from its end.</P></DD><DT><ANAME="REVREF"></A><BCLASS="COMMAND">rev</B></DT><DD><P>reverses each line of a file, and outputs to <TTCLASS="FILENAME">stdout</TT>. This does not have the same effect as <BCLASS="COMMAND">tac</B>, as it preserves the order of the lines, but flips each one around (mirror image).</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat file1.txt</B></TT> <TTCLASS="COMPUTEROUTPUT">This is line 1. This is line 2.</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>tac file1.txt</B></TT> <TTCLASS="COMPUTEROUTPUT">This is line 2. This is line 1.</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>rev file1.txt</B></TT> <TTCLASS="COMPUTEROUTPUT">.1 enil si sihT .2 enil si sihT</TT> </PRE></TD></TR></TABLE> </P></DD><DT><ANAME="CPREF"></A><BCLASS="COMMAND">cp</B></DT><DD><P>This is the file copy command. <TTCLASS="USERINPUT"><B>cp file1 file2</B></TT> copies <TTCLASS="FILENAME">file1</TT> to <TTCLASS="FILENAME">file2</TT>, overwriting <TTCLASS="FILENAME">file2</TT> if it already exists (see <AHREF="moreadv.html#EX42">Example 15-6</A>).</P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Particularly useful are the <TTCLASS="OPTION">-a</TT> archive flag (for copying an entire directory tree), the <TTCLASS="OPTION">-u</TT> update flag (which prevents overwriting identically-named newer files), and the <TTCLASS="OPTION">-r</TT> and <TTCLASS="OPTION">-R</TT> recursive flags.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 cp -u source_dir/* dest_dir 2 # "Synchronize" dest_dir to source_dir 3 #+ by copying over all newer and not previously existing files.</PRE></TD></TR></TABLE></P></TD></TR></TABLE></DIV></DD><DT><ANAME="MVREF"></A><BCLASS="COMMAND">mv</B></DT><DD><P>This is the file <ICLASS="FIRSTTERM">move</I> command. It is equivalent to a combination of <BCLASS="COMMAND">cp</B> and <BCLASS="COMMAND">rm</B>. It may be used to move multiple files to a directory, or even to rename a directory. For some examples of using <BCLASS="COMMAND">mv</B> in a script, see <AHREF="parameter-substitution.html#RFE">Example 9-20</A> and <AHREF="contributed-scripts.html#RN">Example A-2</A>.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>When used in a non-interactive script, <BCLASS="COMMAND">mv</B> takes the <TTCLASS="OPTION">-f</TT> (<ICLASS="FIRSTTERM">force</I>) option to bypass user input.</P><P>When a directory is moved to a preexisting directory, it becomes a subdirectory of the destination directory.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>mv source_directory target_directory</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ls -lF target_directory</B></TT> <TTCLASS="COMPUTEROUTPUT">total 1 drwxrwxr-x 2 bozo bozo 1024 May 28 19:20 source_directory/</TT> </PRE></TD></TR></TABLE> </P></TD></TR></TABLE></DIV></DD><DT><ANAME="RMREF"></A><BCLASS="COMMAND">rm</B></DT><DD><P>Delete (remove) a file or files. The <TTCLASS="OPTION">-f</TT> option forces removal of even readonly files, and is useful for bypassing user input in a script.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><ANAME="DASHREM"></A
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -