📄 extmisc.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Miscellaneous 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="External Filters, Programs and Commands"HREF="external.html"><LINKREL="PREVIOUS"TITLE="Math Commands"HREF="mathc.html"><LINKREL="NEXT"TITLE="System and Administrative Commands"HREF="system.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="SECT1"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="mathc.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 15. External Filters, Programs and Commands</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="system.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="EXTMISC"></A>15.9. Miscellaneous Commands</H1><DIVCLASS="VARIABLELIST"><P><B><ANAME="MISCCOMMANDLISTING1"></A>Command that fit in no special category</B></P><DL><DT><ANAME="JOTREF"></A><BCLASS="COMMAND">jot</B>, <ANAME="SEQREF"></A><BCLASS="COMMAND">seq</B></DT><DD><P>These utilities emit a sequence of integers, with a user-selectable increment.</P><P>The default separator character between each integer is a newline, but this can be changed with the <TTCLASS="OPTION">-s</TT> option.</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>seq 5</B></TT> <TTCLASS="COMPUTEROUTPUT">1 2 3 4 5</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>seq -s : 5</B></TT> <TTCLASS="COMPUTEROUTPUT">1:2:3:4:5</TT> </PRE></TD></TR></TABLE> </P><P>Both <BCLASS="COMMAND">jot</B> and <BCLASS="COMMAND">seq</B> come in handy in a <AHREF="loops.html#FORLOOPREF1">for loop</A>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX53"></A><P><B>Example 15-52. Using <ICLASS="FIRSTTERM">seq</I> to generate loop arguments</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # Using "seq" 3 4 echo 5 6 for a in `seq 80` # or for a in $( seq 80 ) 7 # Same as for a in 1 2 3 4 5 ... 80 (saves much typing!). 8 # May also use 'jot' (if present on system). 9 do 10 echo -n "$a " 11 done # 1 2 3 4 5 ... 80 12 # Example of using the output of a command to generate 13 # the [list] in a "for" loop. 14 15 echo; echo 16 17 18 COUNT=80 # Yes, 'seq' also accepts a replaceable parameter. 19 20 for a in `seq $COUNT` # or for a in $( seq $COUNT ) 21 do 22 echo -n "$a " 23 done # 1 2 3 4 5 ... 80 24 25 echo; echo 26 27 BEGIN=75 28 END=80 29 30 for a in `seq $BEGIN $END` 31 # Giving "seq" two arguments starts the count at the first one, 32 #+ and continues until it reaches the second. 33 do 34 echo -n "$a " 35 done # 75 76 77 78 79 80 36 37 echo; echo 38 39 BEGIN=45 40 INTERVAL=5 41 END=80 42 43 for a in `seq $BEGIN $INTERVAL $END` 44 # Giving "seq" three arguments starts the count at the first one, 45 #+ uses the second for a step interval, 46 #+ and continues until it reaches the third. 47 do 48 echo -n "$a " 49 done # 45 50 55 60 65 70 75 80 50 51 echo; echo 52 53 exit 0</PRE></TD></TR></TABLE><HR></DIV><P>A simpler example:</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 # Create a set of 10 files, 2 #+ named file.1, file.2 . . . file.10. 3 COUNT=10 4 PREFIX=file 5 6 for filename in `seq $COUNT` 7 do 8 touch $PREFIX.$filename 9 # Or, can do other operations, 10 #+ such as rm, grep, etc. 11 done</PRE></TD></TR></TABLE></P><DIVCLASS="EXAMPLE"><HR><ANAME="LETTERCOUNT"></A><P><B>Example 15-53. Letter Count"</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # letter-count.sh: Counting letter occurrences in a text file. 3 # Written by Stefano Palmeri. 4 # Used in ABS Guide with permission. 5 # Slightly modified by document author. 6 7 MINARGS=2 # Script requires at least two arguments. 8 E_BADARGS=65 9 FILE=$1 10 11 let LETTERS=$#-1 # How many letters specified (as command-line args). 12 # (Subtract 1 from number of command line args.) 13 14 15 show_help(){ 16 echo 17 echo Usage: `basename $0` file letters 18 echo Note: `basename $0` arguments are case sensitive. 19 echo Example: `basename $0` foobar.txt G n U L i N U x. 20 echo 21 } 22 23 # Checks number of arguments. 24 if [ $# -lt $MINARGS ]; then 25 echo 26 echo "Not enough arguments." 27 echo 28 show_help 29 exit $E_BADARGS 30 fi 31 32 33 # Checks if file exists. 34 if [ ! -f $FILE ]; then 35 echo "File \"$FILE\" does not exist." 36 exit $E_BADARGS 37 fi 38 39 40 41 # Counts letter occurrences . 42 for n in `seq $LETTERS`; do 43 shift 44 if [[ `echo -n "$1" | wc -c` -eq 1 ]]; then # Checks arg. 45 echo "$1" -\> `cat $FILE | tr -cd "$1" | wc -c` # Counting. 46 else 47 echo "$1 is not a single char." 48 fi 49 done 50 51 exit $? 52 53 # This script has exactly the same functionality as letter-count2.sh, 54 #+ but executes faster. 55 # Why?</PRE></TD></TR></TABLE><HR></DIV><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>Somewhat more capable than <ICLASS="FIRSTTERM">seq</I>, <BCLASS="COMMAND">jot</B> is a classic UNIX utility that is not normally included in a standard Linux distro. However, the source <ICLASS="FIRSTTERM">rpm</I> is available for download from the <AHREF="http://www.mit.edu/afs/athena/system/rhlinux/athena-9.0/free/SRPMS/athena-jot-9.0-3.src.rpm"TARGET="_top"> MIT repository</A>.</P><P><ANAME="JOTRANDOM"></A></P><P>Unlike <ICLASS="FIRSTTERM">seq</I>, <BCLASS="COMMAND">jot</B> can generate a sequence of random numbers, using the <TTCLASS="OPTION">-r</TT> option.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>jot -r 3 999</B></TT> <TTCLASS="COMPUTEROUTPUT">1069 1272 1428</TT></PRE></TD></TR></TABLE></P></TD></TR></TABLE></DIV></DD><DT><ANAME="GETOPTY"></A><BCLASS="COMMAND">getopt</B></DT><DD><P>The <BCLASS="COMMAND">getopt</B> command parses command-line options preceded by a <AHREF="special-chars.html#DASHREF">dash</A>. This external command corresponds to the <AHREF="internal.html#GETOPTSX">getopts</A> Bash builtin. Using <BCLASS="COMMAND">getopt</B> permits handling long options by means of the <TTCLASS="OPTION">-l</TT> flag, and this also allows parameter reshuffling.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX33A"></A><P><B>Example 15-54. Using <ICLASS="FIRSTTERM">getopt</I> to parse command-line options</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # Using getopt 3 4 # Try the following when invoking this script: 5 # sh ex33a.sh -a 6 # sh ex33a.sh -abc 7 # sh ex33a.sh -a -b -c 8 # sh ex33a.sh -d 9 # sh ex33a.sh -dXYZ 10 # sh ex33a.sh -d XYZ 11 # sh ex33a.sh -abcd 12 # sh ex33a.sh -abcdZ 13 # sh ex33a.sh -z 14 # sh ex33a.sh a 15 # Explain the results of each of the above. 16 17 E_OPTERR=65 18 19 if [ "$#" -eq 0 ] 20 then # Script needs at least one command-line argument. 21 echo "Usage $0 -[options a,b,c]" 22 exit $E_OPTERR 23 fi 24 25 set -- `getopt "abcd:" "$@"` 26 # Sets positional parameters to command-line arguments. 27 # What happens if you use "$*" instead of "$@"? 28 29 while [ ! -z "$1" ] 30 do 31 case "$1" in 32 -a) echo "Option \"a\"";; 33 -b) echo "Option \"b\"";; 34 -c) echo "Option \"c\"";; 35 -d) echo "Option \"d\" $2";; 36 *) break;; 37 esac 38 39 shift 40 done 41 42 # It is usually better to use the 'getopts' builtin in a script. 43 # See "ex33.sh." 44 45 exit 0</PRE></TD></TR></TABLE><HR></DIV><P>See <AHREF="string-manipulation.html#GETOPTSIMPLE">Example 9-14</A> for a simplified emulation of <BCLASS="COMMAND">getopt</B>.</P></DD><DT><ANAME="RUNPARTSREF"></A><BCLASS="COMMAND">run-parts</B></DT><DD><P>The <BCLASS="COMMAND">run-parts</B> command <ANAME="AEN12881"HREF="#FTN.AEN12881">[1]</A> executes all the scripts in a target directory, sequentially in ASCII-sorted filename order. Of course, the scripts need to have execute permission.</P><P>The <AHREF="system.html#CRONREF">cron</A> <AHREF="communications.html#DAEMONREF">daemon</A> invokes <BCLASS="COMMAND">run-parts</B> to run the scripts in the <TTCLASS="FILENAME">/etc/cron.*</TT> directories.</P></DD><DT><ANAME="YESREF"></A><BCLASS="COMMAND">yes</B></DT><DD><P>In its default behavior the <BCLASS="COMMAND">yes</B> command feeds a continuous string of the character <TTCLASS="COMPUTEROUTPUT">y</TT> followed by a line feed to <TTCLASS="FILENAME">stdout</TT>. A <BCLASS="KEYCAP">control</B>-<BCLASS="KEYCAP">c</B> terminates the run. A different output string may be specified, as in <TTCLASS="USERINPUT"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -