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

📄 extmisc.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!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&nbsp;#!/bin/bash   2&nbsp;# Using "seq"   3&nbsp;   4&nbsp;echo   5&nbsp;   6&nbsp;for a in `seq 80`  # or   for a in $( seq 80 )   7&nbsp;# Same as   for a in 1 2 3 4 5 ... 80   (saves much typing!).   8&nbsp;# May also use 'jot' (if present on system).   9&nbsp;do  10&nbsp;  echo -n "$a "  11&nbsp;done      # 1 2 3 4 5 ... 80  12&nbsp;# Example of using the output of a command to generate   13&nbsp;# the [list] in a "for" loop.  14&nbsp;  15&nbsp;echo; echo  16&nbsp;  17&nbsp;  18&nbsp;COUNT=80  # Yes, 'seq' also accepts a replaceable parameter.  19&nbsp;  20&nbsp;for a in `seq $COUNT`  # or   for a in $( seq $COUNT )  21&nbsp;do  22&nbsp;  echo -n "$a "  23&nbsp;done      # 1 2 3 4 5 ... 80  24&nbsp;  25&nbsp;echo; echo  26&nbsp;  27&nbsp;BEGIN=75  28&nbsp;END=80  29&nbsp;  30&nbsp;for a in `seq $BEGIN $END`  31&nbsp;#  Giving "seq" two arguments starts the count at the first one,  32&nbsp;#+ and continues until it reaches the second.  33&nbsp;do  34&nbsp;  echo -n "$a "  35&nbsp;done      # 75 76 77 78 79 80  36&nbsp;  37&nbsp;echo; echo  38&nbsp;  39&nbsp;BEGIN=45  40&nbsp;INTERVAL=5  41&nbsp;END=80  42&nbsp;  43&nbsp;for a in `seq $BEGIN $INTERVAL $END`  44&nbsp;#  Giving "seq" three arguments starts the count at the first one,  45&nbsp;#+ uses the second for a step interval,  46&nbsp;#+ and continues until it reaches the third.  47&nbsp;do  48&nbsp;  echo -n "$a "  49&nbsp;done      # 45 50 55 60 65 70 75 80  50&nbsp;  51&nbsp;echo; echo  52&nbsp;  53&nbsp;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&nbsp;#  Create a set of 10 files,   2&nbsp;#+ named file.1, file.2 . . . file.10.   3&nbsp;COUNT=10   4&nbsp;PREFIX=file   5&nbsp;   6&nbsp;for filename in `seq $COUNT`   7&nbsp;do   8&nbsp;  touch $PREFIX.$filename   9&nbsp;  #  Or, can do other operations,  10&nbsp;  #+ such as rm, grep, etc.  11&nbsp;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&nbsp;#!/bin/bash   2&nbsp;# letter-count.sh: Counting letter occurrences in a text file.   3&nbsp;# Written by Stefano Palmeri.   4&nbsp;# Used in ABS Guide with permission.   5&nbsp;# Slightly modified by document author.   6&nbsp;   7&nbsp;MINARGS=2          # Script requires at least two arguments.   8&nbsp;E_BADARGS=65   9&nbsp;FILE=$1  10&nbsp;  11&nbsp;let LETTERS=$#-1   # How many letters specified (as command-line args).  12&nbsp;                   # (Subtract 1 from number of command line args.)  13&nbsp;  14&nbsp;  15&nbsp;show_help(){  16&nbsp;	   echo  17&nbsp;           echo Usage: `basename $0` file letters    18&nbsp;           echo Note: `basename $0` arguments are case sensitive.  19&nbsp;           echo Example: `basename $0` foobar.txt G n U L i N U x.  20&nbsp;	   echo  21&nbsp;}  22&nbsp;  23&nbsp;# Checks number of arguments.  24&nbsp;if [ $# -lt $MINARGS ]; then  25&nbsp;   echo  26&nbsp;   echo "Not enough arguments."  27&nbsp;   echo  28&nbsp;   show_help  29&nbsp;   exit $E_BADARGS  30&nbsp;fi    31&nbsp;  32&nbsp;  33&nbsp;# Checks if file exists.  34&nbsp;if [ ! -f $FILE ]; then  35&nbsp;    echo "File \"$FILE\" does not exist."  36&nbsp;    exit $E_BADARGS  37&nbsp;fi  38&nbsp;  39&nbsp;  40&nbsp;  41&nbsp;# Counts letter occurrences .  42&nbsp;for n in `seq $LETTERS`; do  43&nbsp;      shift  44&nbsp;      if [[ `echo -n "$1" | wc -c` -eq 1 ]]; then             #  Checks arg.  45&nbsp;             echo "$1" -\&#62; `cat $FILE | tr -cd  "$1" | wc -c` #  Counting.  46&nbsp;      else  47&nbsp;             echo "$1 is not a  single char."  48&nbsp;      fi    49&nbsp;done  50&nbsp;  51&nbsp;exit $?  52&nbsp;  53&nbsp;#  This script has exactly the same functionality as letter-count2.sh,  54&nbsp;#+ but executes faster.  55&nbsp;#  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&nbsp;#!/bin/bash   2&nbsp;# Using getopt   3&nbsp;   4&nbsp;# Try the following when invoking this script:   5&nbsp;#   sh ex33a.sh -a   6&nbsp;#   sh ex33a.sh -abc   7&nbsp;#   sh ex33a.sh -a -b -c   8&nbsp;#   sh ex33a.sh -d   9&nbsp;#   sh ex33a.sh -dXYZ  10&nbsp;#   sh ex33a.sh -d XYZ  11&nbsp;#   sh ex33a.sh -abcd  12&nbsp;#   sh ex33a.sh -abcdZ  13&nbsp;#   sh ex33a.sh -z  14&nbsp;#   sh ex33a.sh a  15&nbsp;# Explain the results of each of the above.  16&nbsp;  17&nbsp;E_OPTERR=65  18&nbsp;  19&nbsp;if [ "$#" -eq 0 ]  20&nbsp;then   # Script needs at least one command-line argument.  21&nbsp;  echo "Usage $0 -[options a,b,c]"  22&nbsp;  exit $E_OPTERR  23&nbsp;fi    24&nbsp;  25&nbsp;set -- `getopt "abcd:" "$@"`  26&nbsp;# Sets positional parameters to command-line arguments.  27&nbsp;# What happens if you use "$*" instead of "$@"?  28&nbsp;  29&nbsp;while [ ! -z "$1" ]  30&nbsp;do  31&nbsp;  case "$1" in  32&nbsp;    -a) echo "Option \"a\"";;  33&nbsp;    -b) echo "Option \"b\"";;  34&nbsp;    -c) echo "Option \"c\"";;  35&nbsp;    -d) echo "Option \"d\" $2";;  36&nbsp;     *) break;;  37&nbsp;  esac  38&nbsp;  39&nbsp;  shift  40&nbsp;done  41&nbsp;  42&nbsp;#  It is usually better to use the 'getopts' builtin in a script.  43&nbsp;#  See "ex33.sh."  44&nbsp;  45&nbsp;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 + -