<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>捐献的脚本</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="高级Bash脚本编程指南"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="参考文献"
HREF="biblio.html"><LINK
REL="NEXT"
TITLE="参考卡片"
HREF="refcards.html"></HEAD
><BODY
CLASS="APPENDIX"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>高级Bash脚本编程指南: 一本深入学习shell脚本艺术的书籍</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="biblio.html"
ACCESSKEY="P"
>前一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="refcards.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="APPENDIX"
><H1
><A
NAME="CONTRIBUTED-SCRIPTS"
></A
>Appendix A. 捐献的脚本</H1
><P
>这些脚本展示了一些有趣的shell编程技术, 但是它们并不适合放入本文档的文本讲解中.
不过它们还是非常有用, 运行和分析它们都是很有意思的事. </P
><P
>译者: 这里留给那些有能力而且有多余时间的读者来详读, 个人认为翻译这些注释有点画蛇添足. </P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="MAILFORMAT"
></A
><P
><B
>例子 A-1. <B
CLASS="COMMAND"
>mailformat</B
>: 格式化一个e-mail消息</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><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
></FONT
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="RN"
></A
><P
><B
>例子 A-2. <B
CLASS="COMMAND"
>rn</B
>: 一个非常简单的文件重命名工具</B
></P
><P
>这个脚本是<A
HREF="textproc.html#LOWERCASE"
>例子 12-19</A
>的一个修改版. </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><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
></FONT
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="BLANKRENAME"
></A
><P
><B
>例子 A-3. <B
CLASS="COMMAND"
>blank-rename</B
>: 重命名包含空白的文件名</B
></P
><P
>这是上一个脚本的简化版. </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><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
></FONT
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="ENCRYPTEDPW"
></A
><P
><B
>例子 A-4. <B
CLASS="COMMAND"
>encryptedpw</B
>: 使用一个本地加密口令, 上传到一个ftp服务器. </B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2
3 # Example "ex72.sh" modified to use encrypted password.
4
5 # Note that this is still rather insecure,
6 #+ since the decrypted password is sent in the clear.
7 # Use something like "ssh" if this is a concern.
8
9 E_BADARGS=65
10
11 if [ -z "$1" ]
12 then
13 echo "Usage: `basename $0` filename"
14 exit $E_BADARGS
15 fi
16
17 Username=bozo # Change to suit.
18 pword=/home/boz
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Linux下的Shell 编程 (291个子文件)
ChangeLog 561B
ChangeLog 477B
important.eps 9KB
warning.eps 8KB
caution.eps 8KB
prev.eps 8KB
home.eps 8KB
next.eps 8KB
up.eps 8KB
tip.eps 7KB
note.eps 7KB
toc-blank.eps 6KB
10.eps 6KB
4.eps 6KB
1.eps 6KB
3.eps 6KB
7.eps 6KB
2.eps 6KB
9.eps 6KB
8.eps 6KB
6.eps 6KB
5.eps 6KB
toc-minus.eps 6KB
toc-plus.eps 6KB
important.gif 1KB
note.gif 1KB
warning.gif 1KB
caution.gif 1KB
tip.gif 1KB
home.gif 995B
next.gif 964B
prev.gif 944B
10.gif 926B
up.gif 922B
9.gif 919B
6.gif 917B
8.gif 915B
3.gif 914B
5.gif 912B
4.gif 907B
2.gif 907B
7.gif 903B
1.gif 890B
toc-plus.gif 846B
toc-minus.gif 843B
toc-blank.gif 95B
contributed-scripts.html 317KB
system.html 99KB
internal.html 73KB
textproc.html 62KB
special-chars.html 62KB
filearchiv.html 56KB
arrays.html 56KB
internalvariables.html 49KB
index.html 43KB
communications.html 39KB
extmisc.html 36KB
writingscripts.html 29KB
parameter-substitution.html 28KB
loops1.html 27KB
assortedtips.html 27KB
moreadv.html 27KB
biblio.html 27KB
here-docs.html 25KB
sample-bashrc.html 25KB
debugging.html 25KB
complexfunct.html 25KB
mathc.html 23KB
randomvar.html 23KB
string-manipulation.html 22KB
gotchas.html 21KB
basic.html 20KB
commandsub.html 19KB
testconstructs.html 19KB
refcards.html 19KB
ops.html 19KB
x13673.html 18KB
intro_regular.html 18KB
x13644.html 18KB
x13634.html 18KB
x13642.html 18KB
x13636.html 18KB
x13635.html 18KB
x13632.html 18KB
x13639.html 18KB
x13653.html 18KB
x13650.html 18KB
x13662.html 18KB
x13645.html 18KB
x13661.html 18KB
testbranch.html 16KB
x6756.html 16KB
x6731.html 15KB
x6718.html 15KB
x6728.html 15KB
x6730.html 15KB
x6744.html 15KB
x6745.html 15KB
x6736.html 15KB
x6722.html 15KB
共 291 条
- 1
- 2
- 3
资源评论
DavidYuxuan
- 粉丝: 12
- 资源: 36
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功