<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Contributed Scripts</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="Bibliography"
HREF="biblio.html"><LINK
REL="NEXT"
TITLE="Reference Cards"
HREF="refcards.html"><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"><LINK
REL="stylesheet"
HREF="common/kde-common.css"
TYPE="text/css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"><META
HTTP-EQUIV="Content-Language"
CONTENT="en"><LINK
REL="stylesheet"
HREF="common/kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"><LINK
REL="stylesheet"
HREF="common/kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"></HEAD
><BODY
CLASS="APPENDIX"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#AA0055"
ALINK="#AA0000"
STYLE="font-family: sans-serif;"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="biblio.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="refcards.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="APPENDIX"
><H1
><A
NAME="CONTRIBUTED-SCRIPTS"
>Appendix A. Contributed Scripts</A
></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
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="MAILFORMAT"
></A
><P
><B
>Example A-1. <B
CLASS="COMMAND"
>mailformat</B
>: Formatting an e-mail message</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # mail-format.sh (ver. 1.1): Format e-mail messages.
3
4 # Gets rid of carets, tabs, and also folds excessively long lines.
5
6 # =================================================================
7 # Standard Check for Script Argument(s)
8 ARGS=1
9 E_BADARGS=65
10 E_NOFILE=66
11
12 if [ $# -ne $ARGS ] # Correct number of arguments passed to script?
13 then
14 echo "Usage: `basename $0` filename"
15 exit $E_BADARGS
16 fi
17
18 if [ -f "$1" ] # Check if file exists.
19 then
20 file_name=$1
21 else
22 echo "File \"$1\" does not exist."
23 exit $E_NOFILE
24 fi
25 # =================================================================
26
27 MAXWIDTH=70 # Width to fold excessively long lines to.
28
29 # ---------------------------------
30 # A variable can hold a sed script.
31 sedscript='s/^>//
32 s/^ *>//
33 s/^ *//
34 s/ *//'
35 # ---------------------------------
36
37 # Delete carets and tabs at beginning of lines,
38 #+ then fold lines to $MAXWIDTH characters.
39 sed "$sedscript" $1 | fold -s --width=$MAXWIDTH
40 # -s option to "fold"
41 #+ breaks lines at whitespace, if possible.
42
43
44 # This script was inspired by an article in a well-known trade journal
45 #+ extolling a 164K MS Windows utility with similar functionality.
46 #
47 # An nice set of text processing utilities and an efficient
48 #+ scripting language provide an alternative to bloated executables.
49
50 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="RN"
></A
><P
><B
>Example A-2. <B
CLASS="COMMAND"
>rn</B
>: A simple-minded file rename utility</B
></P
><P
>This script is a modification of <A
HREF="textproc.html#LOWERCASE"
>Example 12-19</A
>.</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #! /bin/bash
2 #
3 # Very simpleminded filename "rename" utility (based on "lowercase.sh").
4 #
5 # The "ren" utility, by Vladimir Lanin (lanin@csd2.nyu.edu),
6 #+ does a much better job of this.
7
8
9 ARGS=2
10 E_BADARGS=65
11 ONE=1 # For getting singular/plural right (see below).
12
13 if [ $# -ne "$ARGS" ]
14 then
15 echo "Usage: `basename $0` old-pattern new-pattern"
16 # As in "rn gif jpg", which renames all gif files in working directory to jpg.
17 exit $E_BADARGS
18 fi
19
20 number=0 # Keeps track of how many files actually renamed.
21
22
23 for filename in *$1* #Traverse all matching files in directory.
24 do
25 if [ -f "$filename" ] # If finds match...
26 then
27 fname=`basename $filename` # Strip off path.
28 n=`echo $fname | sed -e "s/$1/$2/"` # Substitute new for old in filename.
29 mv $fname $n # Rename.
30 let "number += 1"
31 fi
32 done
33
34 if [ "$number" -eq "$ONE" ] # For correct grammar.
35 then
36 echo "$number file renamed."
37 else
38 echo "$number files renamed."
39 fi
40
41 exit 0
42
43
44 # Exercises:
45 # ---------
46 # What type of files will this not work on?
47 # How can this be fixed?
48 #
49 # Rewrite this script to process all the files in a directory
50 #+ containing spaces in their names, and to rename them,
51 #+ substituting an underscore for each space.</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="BLANKRENAME"
></A
><P
><B
>Example A-3. <B
CLASS="COMMAND"
>blank-rename</B
>: renames filenames containing
blanks</B
></P
><P
>This is an even simpler-minded version of previous script.</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #! /bin/bash
2 # blank-rename.sh
3 #
4 # Substitutes underscores for blanks in all the filenames in a directory.
5
6 ONE=1 # For getting singular/plural right (see below).
7 number=0 # Keeps track of how many files actually renamed.
8 FOUND=0 # Successful return value.
9
10 for filename in * #Traverse all files in directory.
11 do
12 echo "$filename" | grep -q " " # Check whether filename
13 if [ $? -eq $FOUND ] #+ contains space(s).
14 then
15 fname=$filename # Strip off path.
16 n=`echo $fname | sed -e "s/ /_/g"` # Substitute underscore for blank.
17 mv "$fname" "$n" # Do the actual renaming.
18 let "number += 1"
19 fi
20 done
21
22 if [ "$number" -eq "$ONE" ] # For correct grammar.
23 then
24 echo "$number file renamed."
25 else
26 echo "$number files renamed."
27 fi
28
29 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="ENCRYPTEDPW"
></A
><P
><B
>Example A-4. <B
CLASS="COMMAND"
>encryptedpw</B
>: Uploading to an ftp site,
using a locally encrypted password</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bas
评论0