contributed-scripts.html

来自「BASH Shell 编程 经典教程 《高级SHELL脚本编程》中文版」· HTML 代码 · 共 2,144 行 · 第 1/5 页

HTML
2,144
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><HTML><HEAD><TITLE>捐献的脚本</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINKREL="HOME"TITLE="高级Bash脚本编程指南"HREF="index.html"><LINKREL="PREVIOUS"TITLE="参考文献"HREF="biblio.html"><LINKREL="NEXT"TITLE="参考卡片"HREF="refcards.html"></HEAD><BODYCLASS="APPENDIX"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">高级Bash脚本编程指南: 一本深入学习shell脚本艺术的书籍</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="biblio.html"ACCESSKEY="P">前一页</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="refcards.html"ACCESSKEY="N">下一页</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="APPENDIX"><H1><ANAME="CONTRIBUTED-SCRIPTS"></A>Appendix A. 捐献的脚本</H1><P>这些脚本展示了一些有趣的shell编程技术, 但是它们并不适合放入本文档的文本讲解中.     不过它们还是非常有用, 运行和分析它们都是很有意思的事. </P><P>译者: 这里留给那些有能力而且有多余时间的读者来详读, 个人认为翻译这些注释有点画蛇添足. </P><DIVCLASS="EXAMPLE"><HR><ANAME="MAILFORMAT"></A><P><B>例子 A-1. <BCLASS="COMMAND">mailformat</B>: 格式化一个e-mail消息</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;#!/bin/bash  2&nbsp;# mail-format.sh (ver. 1.1): Format e-mail messages.  3&nbsp;  4&nbsp;# Gets rid of carets, tabs, and also folds excessively long lines.  5&nbsp;  6&nbsp;# =================================================================  7&nbsp;#                 Standard Check for Script Argument(s)  8&nbsp;ARGS=1  9&nbsp;E_BADARGS=65 10&nbsp;E_NOFILE=66 11&nbsp; 12&nbsp;if [ $# -ne $ARGS ]  # Correct number of arguments passed to script? 13&nbsp;then 14&nbsp;  echo "Usage: `basename $0` filename" 15&nbsp;  exit $E_BADARGS 16&nbsp;fi 17&nbsp; 18&nbsp;if [ -f "$1" ]       # Check if file exists. 19&nbsp;then 20&nbsp;    file_name=$1 21&nbsp;else 22&nbsp;    echo "File \"$1\" does not exist." 23&nbsp;    exit $E_NOFILE 24&nbsp;fi 25&nbsp;# ================================================================= 26&nbsp; 27&nbsp;MAXWIDTH=70          # Width to fold excessively long lines to. 28&nbsp; 29&nbsp;# --------------------------------- 30&nbsp;# A variable can hold a sed script. 31&nbsp;sedscript='s/^&#62;// 32&nbsp;s/^  *&#62;// 33&nbsp;s/^  *// 34&nbsp;s/		*//' 35&nbsp;# --------------------------------- 36&nbsp; 37&nbsp;#  Delete carets and tabs at beginning of lines, 38&nbsp;#+ then fold lines to $MAXWIDTH characters. 39&nbsp;sed "$sedscript" $1 | fold -s --width=$MAXWIDTH 40&nbsp;                        #  -s option to "fold" 41&nbsp;                        #+ breaks lines at whitespace, if possible. 42&nbsp; 43&nbsp; 44&nbsp;#  This script was inspired by an article in a well-known trade journal 45&nbsp;#+ extolling a 164K MS Windows utility with similar functionality. 46&nbsp;# 47&nbsp;#  An nice set of text processing utilities and an efficient 48&nbsp;#+ scripting language provide an alternative to bloated executables. 49&nbsp; 50&nbsp;exit 0</PRE></FONT></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="RN"></A><P><B>例子 A-2. <BCLASS="COMMAND">rn</B>: 一个非常简单的文件重命名工具</B></P><P>这个脚本是<AHREF="textproc.html#LOWERCASE">例子 12-19</A>的一个修改版. </P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;#! /bin/bash  2&nbsp;#  3&nbsp;# Very simpleminded filename "rename" utility (based on "lowercase.sh").  4&nbsp;#  5&nbsp;#  The "ren" utility, by Vladimir Lanin (lanin@csd2.nyu.edu),  6&nbsp;#+ does a much better job of this.  7&nbsp;  8&nbsp;  9&nbsp;ARGS=2 10&nbsp;E_BADARGS=65 11&nbsp;ONE=1                     # For getting singular/plural right (see below). 12&nbsp; 13&nbsp;if [ $# -ne "$ARGS" ] 14&nbsp;then 15&nbsp;  echo "Usage: `basename $0` old-pattern new-pattern" 16&nbsp;  # As in "rn gif jpg", which renames all gif files in working directory to jpg. 17&nbsp;  exit $E_BADARGS 18&nbsp;fi 19&nbsp; 20&nbsp;number=0                  # Keeps track of how many files actually renamed. 21&nbsp; 22&nbsp; 23&nbsp;for filename in *$1*      #Traverse all matching files in directory. 24&nbsp;do 25&nbsp;   if [ -f "$filename" ]  # If finds match... 26&nbsp;   then 27&nbsp;     fname=`basename $filename`            # Strip off path. 28&nbsp;     n=`echo $fname | sed -e "s/$1/$2/"`   # Substitute new for old in filename. 29&nbsp;     mv $fname $n                          # Rename. 30&nbsp;     let "number += 1" 31&nbsp;   fi 32&nbsp;done    33&nbsp; 34&nbsp;if [ "$number" -eq "$ONE" ]                # For correct grammar. 35&nbsp;then 36&nbsp; echo "$number file renamed." 37&nbsp;else  38&nbsp; echo "$number files renamed." 39&nbsp;fi  40&nbsp; 41&nbsp;exit 0 42&nbsp; 43&nbsp; 44&nbsp;# Exercises: 45&nbsp;# --------- 46&nbsp;# What type of files will this not work on? 47&nbsp;# How can this be fixed? 48&nbsp;# 49&nbsp;#  Rewrite this script to process all the files in a directory 50&nbsp;#+ containing spaces in their names, and to rename them, 51&nbsp;#+ substituting an underscore for each space.</PRE></FONT></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="BLANKRENAME"></A><P><B>例子 A-3. <BCLASS="COMMAND">blank-rename</B>: 重命名包含空白的文件名</B></P><P>这是上一个脚本的简化版. </P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;#! /bin/bash  2&nbsp;# blank-rename.sh  3&nbsp;#  4&nbsp;# Substitutes underscores for blanks in all the filenames in a directory.  5&nbsp;  6&nbsp;ONE=1                     # For getting singular/plural right (see below).  7&nbsp;number=0                  # Keeps track of how many files actually renamed.  8&nbsp;FOUND=0                   # Successful return value.  9&nbsp; 10&nbsp;for filename in *         #Traverse all files in directory. 11&nbsp;do 12&nbsp;     echo "$filename" | grep -q " "         #  Check whether filename 13&nbsp;     if [ $? -eq $FOUND ]                   #+ contains space(s). 14&nbsp;     then 15&nbsp;       fname=$filename                      # Strip off path. 16&nbsp;       n=`echo $fname | sed -e "s/ /_/g"`   # Substitute underscore for blank. 17&nbsp;       mv "$fname" "$n"                     # Do the actual renaming. 18&nbsp;       let "number += 1" 19&nbsp;     fi 20&nbsp;done    21&nbsp; 22&nbsp;if [ "$number" -eq "$ONE" ]                 # For correct grammar. 23&nbsp;then 24&nbsp; echo "$number file renamed." 25&nbsp;else  26&nbsp; echo "$number files renamed." 27&nbsp;fi  28&nbsp; 29&nbsp;exit 0</PRE></FONT></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="ENCRYPTEDPW"></A><P><B>例子 A-4. <BCLASS="COMMAND">encryptedpw</B>: 使用一个本地加密口令, 上传到一个ftp服务器. </B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;#!/bin/bash  2&nbsp;  3&nbsp;# Example "ex72.sh" modified to use encrypted password.  4&nbsp;  5&nbsp;#  Note that this is still rather insecure,  6&nbsp;#+ since the decrypted password is sent in the clear.  7&nbsp;#  Use something like "ssh" if this is a concern.  8&nbsp;  9&nbsp;E_BADARGS=65 10&nbsp; 11&nbsp;if [ -z "$1" ] 12&nbsp;then 13&nbsp;  echo "Usage: `basename $0` filename" 14&nbsp;  exit $E_BADARGS 15&nbsp;fi   16&nbsp; 17&nbsp;Username=bozo           # Change to suit. 18&nbsp;pword=/home/bozo/secret/password_encrypted.file 19&nbsp;# File containing encrypted password. 20&nbsp; 21&nbsp;Filename=`basename $1`  # Strips pathname out of file name. 22&nbsp; 23&nbsp;Server="XXX" 24&nbsp;Directory="YYY"         # Change above to actual server name &#38; directory. 25&nbsp; 26&nbsp; 27&nbsp;Password=`cruft &#60;$pword`          # Decrypt password. 28&nbsp;#  Uses the author's own "cruft" file encryption package, 29&nbsp;#+ based on the classic "onetime pad" algorithm, 30&nbsp;#+ and obtainable from: 31&nbsp;#+ Primary-site:   ftp://ibiblio.org/pub/Linux/utils/file 32&nbsp;#+                 cruft-0.2.tar.gz [16k] 33&nbsp; 34&nbsp; 35&nbsp;ftp -n $Server &#60;&#60;End-Of-Session 36&nbsp;user $Username $Password 37&nbsp;binary 38&nbsp;bell 39&nbsp;cd $Directory 40&nbsp;put $Filename 41&nbsp;bye 42&nbsp;End-Of-Session 43&nbsp;# -n option to "ftp" disables auto-logon. 44&nbsp;# Note that "bell" rings 'bell' after each file transfer. 45&nbsp; 46&nbsp;exit 0</PRE></FONT></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="COPYCD"></A><P><B>例子 A-5. <BCLASS="COMMAND">copy-cd</B>: 拷贝一个数据CD</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;#!/bin/bash  2&nbsp;# copy-cd.sh: copying a data CD  3&nbsp;  4&nbsp;CDROM=/dev/cdrom                           # CD ROM device  5&nbsp;OF=/home/bozo/projects/cdimage.iso         # output file  6&nbsp;#       /xxxx/xxxxxxx/                     Change to suit your system.  7&nbsp;BLOCKSIZE=2048  8&nbsp;SPEED=2                                    # May use higher speed if supported.  9&nbsp;DEVICE=cdrom 10&nbsp;# DEVICE="0,0"    on older versions of cdrecord. 11&nbsp; 12&nbsp;echo; echo "Insert source CD, but do *not* mount it." 13&nbsp;echo "Press ENTER when ready. " 14&nbsp;read ready                                 # Wait for input, $ready not used. 15&nbsp; 16&nbsp;echo; echo "Copying the source CD to $OF." 17&nbsp;echo "This may take a while. Please be patient." 18&nbsp; 19&nbsp;dd if=$CDROM of=$OF bs=$BLOCKSIZE          # Raw device copy. 20&nbsp; 21&nbsp; 22&nbsp;echo; echo "Remove data CD." 23&nbsp;echo "Insert blank CDR." 24&nbsp;echo "Press ENTER when ready. " 25&nbsp;read ready                                 # Wait for input, $ready not used. 26&nbsp; 27&nbsp;echo "Copying $OF to CDR." 28&nbsp; 29&nbsp;cdrecord -v -isosize speed=$SPEED dev=$DEVICE $OF 30&nbsp;# Uses Joerg Schilling's "cdrecord" package (see its docs). 31&nbsp;# http://www.fokus.gmd.de/nthp/employees/schilling/cdrecord.html 32&nbsp; 33&nbsp; 34&nbsp;echo; echo "Done copying $OF to CDR on device $CDROM." 35&nbsp; 36&nbsp;echo "Do you want to erase the image file (y/n)? "  # Probably a huge file. 37&nbsp;read answer

⌨️ 快捷键说明

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