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

📄 textproc.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
  30&nbsp;  31&nbsp;for filename in *    # Not necessary to use basename,  32&nbsp;                     # since "*" won't return any file containing "/".  33&nbsp;do n=`echo "$filename/" | tr '[:upper:]' '[:lower:]'`  34&nbsp;#                             POSIX char set notation.  35&nbsp;#                    Slash added so that trailing newlines are not  36&nbsp;#                    removed by command substitution.  37&nbsp;   # Variable substitution:  38&nbsp;   n=${n%/}          # Removes trailing slash, added above, from filename.  39&nbsp;   [[ $filename == $n ]] || mv "$filename" "$n"  40&nbsp;                     # Checks if filename already lowercase.  41&nbsp;done  42&nbsp;  43&nbsp;exit $?</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="TRD2U"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="DU"></A><P><B>Example 15-22. <ICLASS="FIRSTTERM">du</I>: DOS to UNIX text file conversion.</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Du.sh: DOS to UNIX text file converter.   3&nbsp;   4&nbsp;E_WRONGARGS=65   5&nbsp;   6&nbsp;if [ -z "$1" ]   7&nbsp;then   8&nbsp;  echo "Usage: `basename $0` filename-to-convert"   9&nbsp;  exit $E_WRONGARGS  10&nbsp;fi  11&nbsp;  12&nbsp;NEWFILENAME=$1.unx  13&nbsp;  14&nbsp;CR='\015'  # Carriage return.  15&nbsp;           # 015 is octal ASCII code for CR.  16&nbsp;           # Lines in a DOS text file end in CR-LF.  17&nbsp;           # Lines in a UNIX text file end in LF only.  18&nbsp;  19&nbsp;tr -d $CR &#60; $1 &#62; $NEWFILENAME  20&nbsp;# Delete CR's and write to new file.  21&nbsp;  22&nbsp;echo "Original DOS text file is \"$1\"."  23&nbsp;echo "Converted UNIX text file is \"$NEWFILENAME\"."  24&nbsp;  25&nbsp;exit 0  26&nbsp;  27&nbsp;# Exercise:  28&nbsp;# --------  29&nbsp;# Change the above script to convert from UNIX to DOS.</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="ROT13"></A><P><B>Example 15-23. <ICLASS="FIRSTTERM">rot13</I>: ultra-weak encryption.</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# rot13.sh: Classic rot13 algorithm,   3&nbsp;#           encryption that might fool a 3-year old.   4&nbsp;   5&nbsp;# Usage: ./rot13.sh filename   6&nbsp;# or     ./rot13.sh &#60;filename   7&nbsp;# or     ./rot13.sh and supply keyboard input (stdin)   8&nbsp;   9&nbsp;cat "$@" | tr 'a-zA-Z' 'n-za-mN-ZA-M'   # "a" goes to "n", "b" to "o", etc.  10&nbsp;#  The 'cat "$@"' construction  11&nbsp;#+ permits getting input either from stdin or from files.  12&nbsp;  13&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="CRYPTOQUOTE"></A><P><B>Example 15-24. Generating <SPANCLASS="QUOTE">"Crypto-Quote"</SPAN> Puzzles</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# crypto-quote.sh: Encrypt quotes   3&nbsp;   4&nbsp;#  Will encrypt famous quotes in a simple monoalphabetic substitution.   5&nbsp;#  The result is similar to the "Crypto Quote" puzzles   6&nbsp;#+ seen in the Op Ed pages of the Sunday paper.   7&nbsp;   8&nbsp;   9&nbsp;key=ETAOINSHRDLUBCFGJMQPVWZYXK  10&nbsp;# The "key" is nothing more than a scrambled alphabet.  11&nbsp;# Changing the "key" changes the encryption.  12&nbsp;  13&nbsp;# The 'cat "$@"' construction gets input either from stdin or from files.  14&nbsp;# If using stdin, terminate input with a Control-D.  15&nbsp;# Otherwise, specify filename as command-line parameter.  16&nbsp;  17&nbsp;cat "$@" | tr "a-z" "A-Z" | tr "A-Z" "$key"  18&nbsp;#        |  to uppercase  |     encrypt         19&nbsp;# Will work on lowercase, uppercase, or mixed-case quotes.  20&nbsp;# Passes non-alphabetic characters through unchanged.  21&nbsp;  22&nbsp;  23&nbsp;# Try this script with something like:  24&nbsp;# "Nothing so needs reforming as other people's habits."  25&nbsp;# --Mark Twain  26&nbsp;#  27&nbsp;# Output is:  28&nbsp;# "CFPHRCS QF CIIOQ MINFMBRCS EQ FPHIM GIFGUI'Q HETRPQ."  29&nbsp;# --BEML PZERC  30&nbsp;  31&nbsp;# To reverse the encryption:  32&nbsp;# cat "$@" | tr "$key" "A-Z"  33&nbsp;  34&nbsp;  35&nbsp;#  This simple-minded cipher can be broken by an average 12-year old  36&nbsp;#+ using only pencil and paper.  37&nbsp;  38&nbsp;exit 0  39&nbsp;  40&nbsp;#  Exercise:  41&nbsp;#  --------  42&nbsp;#  Modify the script so that it will either encrypt or decrypt,  43&nbsp;#+ depending on command-line argument(s).</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="TRVARIANTS"></A></P><TABLECLASS="SIDEBAR"BORDER="1"CELLPADDING="5"><TR><TD><DIVCLASS="SIDEBAR"><ANAME="AEN10567"></A><P><B><ICLASS="FIRSTTERM">tr</I> variants</B></P><P>	    The <BCLASS="COMMAND">tr</B> utility has two historic	    variants. The BSD version does not use brackets	    (<TTCLASS="USERINPUT"><B>tr a-z A-Z</B></TT>), but the SysV one does	    (<TTCLASS="USERINPUT"><B>tr '[a-z]' '[A-Z]'</B></TT>). The GNU version	    of <BCLASS="COMMAND">tr</B> resembles the BSD one.	    </P></DIV></TD></TR></TABLE></DD><DT><ANAME="FOLDREF"></A><BCLASS="COMMAND">fold</B></DT><DD><P>A filter that wraps lines of input to a specified width.	      This is especially useful with the <TTCLASS="OPTION">-s</TT>	      option, which breaks lines at word spaces (see <AHREF="textproc.html#EX50">Example 15-25</A> and <AHREF="contributed-scripts.html#MAILFORMAT">Example A-1</A>).</P></DD><DT><ANAME="FMTREF"></A><BCLASS="COMMAND">fmt</B></DT><DD><P>Simple-minded file formatter, used as a filter in a	      pipe to <SPANCLASS="QUOTE">"wrap"</SPAN> long lines of text	      output.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX50"></A><P><B>Example 15-25. Formatted file listing.</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;WIDTH=40                    # 40 columns wide.   4&nbsp;   5&nbsp;b=`ls /usr/local/bin`       # Get a file listing...   6&nbsp;   7&nbsp;echo $b | fmt -w $WIDTH   8&nbsp;   9&nbsp;# Could also have been done by  10&nbsp;#    echo $b | fold - -s -w $WIDTH  11&nbsp;   12&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>See also <AHREF="moreadv.html#EX41">Example 15-5</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>A powerful alternative to <BCLASS="COMMAND">fmt</B> is	      Kamil Toman's <BCLASS="COMMAND">par</B>	      utility, available from <AHREF="http://www.cs.berkeley.edu/~amc/Par/"TARGET="_top">http://www.cs.berkeley.edu/~amc/Par/</A>.	      </P></TD></TR></TABLE></DIV></DD><DT><ANAME="COLREF"></A><BCLASS="COMMAND">col</B></DT><DD><P>This deceptively named filter removes reverse line feeds	      from an input stream. It also attempts to replace	      whitespace with equivalent tabs. The chief use of	      <BCLASS="COMMAND">col</B> is in filtering the output	      from certain text processing utilities, such as	      <BCLASS="COMMAND">groff</B> and <BCLASS="COMMAND">tbl</B>.</P></DD><DT><ANAME="COLUMNREF"></A><BCLASS="COMMAND">column</B></DT><DD><P>Column formatter. This filter transforms list-type	      text output into a <SPANCLASS="QUOTE">"pretty-printed"</SPAN> table	      by inserting tabs at appropriate places.</P><DIVCLASS="EXAMPLE"><HR><ANAME="COL"></A><P><B>Example 15-26. Using <ICLASS="FIRSTTERM">column</I> to format a directory	        listing</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# This is a slight modification of the example file in the "column" man page.   3&nbsp;   4&nbsp;   5&nbsp;(printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \   6&nbsp;; ls -l | sed 1d) | column -t   7&nbsp;   8&nbsp;#  The "sed 1d" in the pipe deletes the first line of output,   9&nbsp;#+ which would be "total        N",  10&nbsp;#+ where "N" is the total number of files found by "ls -l".  11&nbsp;  12&nbsp;# The -t option to "column" pretty-prints a table.  13&nbsp;  14&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="COLRMREF"></A><BCLASS="COMMAND">colrm</B></DT><DD><P>Column removal filter. This removes columns (characters)	      from a file and writes the file, lacking the range of	      specified columns, back to <TTCLASS="FILENAME">stdout</TT>.	      <TTCLASS="USERINPUT"><B>colrm 2 4 &#60;filename</B></TT> removes the	      second through fourth characters from each line of the	      text file <TTCLASS="FILENAME">filename</TT>.</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>If the file contains tabs or nonprintable	      characters, this may cause unpredictable	      behavior. In such cases, consider using	      <AHREF="textproc.html#EXPANDREF">expand</A> and	      <BCLASS="COMMAND">unexpand</B> in a pipe preceding	      <BCLASS="COMMAND">colrm</B>.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="NLREF"></A><BCLASS="COMMAND">nl</B></DT><DD><P>Line numbering filter: <TTCLASS="USERINPUT"><B>nl filename</B></TT>	    lists <TTCLASS="FILENAME">filename</TT> to	    <TTCLASS="FILENAME">stdout</TT>, but inserts consecutive	    numbers at the beginning of each non-blank line. If	    <TTCLASS="FILENAME">filename</TT> omitted, operates on	    <TTCLASS="FILENAME">stdin.</TT></P><P>The output of <BCLASS="COMMAND">nl</B> is very similar to	      <TTCLASS="USERINPUT"><B>cat -b</B></TT>, since, by default	      <BCLASS="COMMAND">nl</B> does not list blank lines.</P><DIVCLASS="EXAMPLE"><HR><ANAME="LNUM"></A><P><B>Example 15-27. <ICLASS="FIRSTTERM">nl</I>: A self-numbering script.</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# line-number.sh   3&nbsp;   4&nbsp;# This script echoes itself twice to stdout with its lines numbered.   5&nbsp;   6&nbsp;# 'nl' sees this as line 4 since it does not number blank lines.   7&nbsp;# 'cat -n' sees the above line as number 6.   8&nbsp;   9&nbsp;nl `basename $0`  10&nbsp;  11&nbsp;echo; echo  # Now, let's try it with 'cat -n'  12&nbsp;  13&nbsp;cat -n `basename $0`  14&nbsp;# The difference is that 'cat -n' numbers the blank lines.  15&nbsp;# Note that 'nl -ba' will also do so.  16&nbsp;  17&nbsp;exit 0  18&nbsp;# -----------------------------------------------------------------</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="PRREF"></A><BCLASS="COMMAND">pr</B></DT><DD><P>Print formatting filter. This will paginate files	      (or <TTCLASS="FILENAME">stdout</TT>) into sections suitable for	      hard copy printing or viewing on screen.	Various options	      permit row and column manipulation, joining lines, setting	      margins, numbering lines, adding page headers, and merging	      files, among other things. The <BCLASS="COMMAND">pr</B>	      command combines much of the functionality of	      <BCLASS="COMMAND">nl</B>, <BCLASS="COMMAND">paste</B>,	      <BCLASS="COMMAND">fold</B>, <BCLASS="COMMAND">column</B>, and	      <BCLASS="COMMAND">expand</B>.</P><P><TTCLASS="USERINPUT"><B>pr -o 5 --width=65 fileZZZ | more</B></TT>	     gives a nice paginated listing to screen of	     <TTCLASS="FILENAME">fileZZZ</TT> with margins set at 5 and	     65.</P><P>A particularly useful option is <TTCLASS="OPTION">-d</TT>,	      forcing double-spacing (same effect as <BCLASS="COMMAND">sed	      -G</B>).</P></DD><DT><ANAME="GETTEXTREF"></A><BCLASS="COMMAND">gettext</B></DT><DD><P>The GNU <BCLASS="COMMAND">gettext</B> package is a set of	      utilities for <AHREF="localization.html">localizing</A>	      and translating the text output of programs into foreign	      languages. While originally intended for C programs, it	      now supports quite a number of programming and scripting	      languages.</P><P>The  <BCLASS="COMMAND">gettext</B>	      <SPANCLASS="emphasis"><ICLASS="EMPHASIS">program</I></SPAN> works on shell scripts. See	      the <TTCLASS="REPLACEABLE"><I>info page</I></TT>.</P></DD><DT><ANAME="MSGFMTREF"></A><BCLASS="COMMAND">msgfmt</B></DT><DD><P>A program for generating binary	      message catalogs. It is used for <AHREF="localization.html">localization</A>.</P></DD><DT><ANAME="ICONVREF"></A><BCLASS="COMMAND">iconv</B></DT><DD><P>A utility for converting file(s) to a different encoding

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -