📄 textproc.html
字号:
30 31 for filename in * # Not necessary to use basename, 32 # since "*" won't return any file containing "/". 33 do n=`echo "$filename/" | tr '[:upper:]' '[:lower:]'` 34 # POSIX char set notation. 35 # Slash added so that trailing newlines are not 36 # removed by command substitution. 37 # Variable substitution: 38 n=${n%/} # Removes trailing slash, added above, from filename. 39 [[ $filename == $n ]] || mv "$filename" "$n" 40 # Checks if filename already lowercase. 41 done 42 43 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 #!/bin/bash 2 # Du.sh: DOS to UNIX text file converter. 3 4 E_WRONGARGS=65 5 6 if [ -z "$1" ] 7 then 8 echo "Usage: `basename $0` filename-to-convert" 9 exit $E_WRONGARGS 10 fi 11 12 NEWFILENAME=$1.unx 13 14 CR='\015' # Carriage return. 15 # 015 is octal ASCII code for CR. 16 # Lines in a DOS text file end in CR-LF. 17 # Lines in a UNIX text file end in LF only. 18 19 tr -d $CR < $1 > $NEWFILENAME 20 # Delete CR's and write to new file. 21 22 echo "Original DOS text file is \"$1\"." 23 echo "Converted UNIX text file is \"$NEWFILENAME\"." 24 25 exit 0 26 27 # Exercise: 28 # -------- 29 # 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 #!/bin/bash 2 # rot13.sh: Classic rot13 algorithm, 3 # encryption that might fool a 3-year old. 4 5 # Usage: ./rot13.sh filename 6 # or ./rot13.sh <filename 7 # or ./rot13.sh and supply keyboard input (stdin) 8 9 cat "$@" | tr 'a-zA-Z' 'n-za-mN-ZA-M' # "a" goes to "n", "b" to "o", etc. 10 # The 'cat "$@"' construction 11 #+ permits getting input either from stdin or from files. 12 13 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 #!/bin/bash 2 # crypto-quote.sh: Encrypt quotes 3 4 # Will encrypt famous quotes in a simple monoalphabetic substitution. 5 # The result is similar to the "Crypto Quote" puzzles 6 #+ seen in the Op Ed pages of the Sunday paper. 7 8 9 key=ETAOINSHRDLUBCFGJMQPVWZYXK 10 # The "key" is nothing more than a scrambled alphabet. 11 # Changing the "key" changes the encryption. 12 13 # The 'cat "$@"' construction gets input either from stdin or from files. 14 # If using stdin, terminate input with a Control-D. 15 # Otherwise, specify filename as command-line parameter. 16 17 cat "$@" | tr "a-z" "A-Z" | tr "A-Z" "$key" 18 # | to uppercase | encrypt 19 # Will work on lowercase, uppercase, or mixed-case quotes. 20 # Passes non-alphabetic characters through unchanged. 21 22 23 # Try this script with something like: 24 # "Nothing so needs reforming as other people's habits." 25 # --Mark Twain 26 # 27 # Output is: 28 # "CFPHRCS QF CIIOQ MINFMBRCS EQ FPHIM GIFGUI'Q HETRPQ." 29 # --BEML PZERC 30 31 # To reverse the encryption: 32 # cat "$@" | tr "$key" "A-Z" 33 34 35 # This simple-minded cipher can be broken by an average 12-year old 36 #+ using only pencil and paper. 37 38 exit 0 39 40 # Exercise: 41 # -------- 42 # Modify the script so that it will either encrypt or decrypt, 43 #+ 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 #!/bin/bash 2 3 WIDTH=40 # 40 columns wide. 4 5 b=`ls /usr/local/bin` # Get a file listing... 6 7 echo $b | fmt -w $WIDTH 8 9 # Could also have been done by 10 # echo $b | fold - -s -w $WIDTH 11 12 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 #!/bin/bash 2 # This is a slight modification of the example file in the "column" man page. 3 4 5 (printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \ 6 ; ls -l | sed 1d) | column -t 7 8 # The "sed 1d" in the pipe deletes the first line of output, 9 #+ which would be "total N", 10 #+ where "N" is the total number of files found by "ls -l". 11 12 # The -t option to "column" pretty-prints a table. 13 14 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 <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 #!/bin/bash 2 # line-number.sh 3 4 # This script echoes itself twice to stdout with its lines numbered. 5 6 # 'nl' sees this as line 4 since it does not number blank lines. 7 # 'cat -n' sees the above line as number 6. 8 9 nl `basename $0` 10 11 echo; echo # Now, let's try it with 'cat -n' 12 13 cat -n `basename $0` 14 # The difference is that 'cat -n' numbers the blank lines. 15 # Note that 'nl -ba' will also do so. 16 17 exit 0 18 # -----------------------------------------------------------------</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 + -