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

📄 contributed-scripts.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Contributed Scripts</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="PREVIOUS"TITLE="Bibliography"HREF="biblio.html"><LINKREL="NEXT"TITLE="Reference Cards"HREF="refcards.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="APPENDIX"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="biblio.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="refcards.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="APPENDIX"><H1><ANAME="CONTRIBUTED-SCRIPTS"></A>Appendix A. Contributed Scripts</H1><P>These scripts, while not fitting into the text of this document, do    illustrate some interesting shell programming techniques. They are useful,    too. Have fun analyzing and running them.</P><DIVCLASS="EXAMPLE"><HR><ANAME="MAILFORMAT"></A><P><B>Example A-1. <ICLASS="FIRSTTERM">mailformat</I>: Formatting an e-mail      message</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><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></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="RN"></A><P><B>Example A-2. <ICLASS="FIRSTTERM">rn</I>: A simple-minded file renaming      utility</B></P><P>This script is a modification of <AHREF="textproc.html#LOWERCASE">Example 15-21</A>.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><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></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="BLANKRENAME"></A><P><B>Example A-3. <ICLASS="FIRSTTERM">blank-rename</I>: Renames filenames containing        blanks</B></P><P>This is an even simpler-minded version of previous script.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><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                      # Yes, this filename needs work.  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></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="ENCRYPTEDPW"></A><P><B>Example A-4. <ICLASS="FIRSTTERM">encryptedpw</I>: Uploading to an ftp site,      using a locally encrypted password</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><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></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="COPYCD"></A><P><B>Example A-5. <ICLASS="FIRSTTERM">copy-cd</I>: Copying a data CD</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><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.

⌨️ 快捷键说明

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