1、Linux Shell Bash 精彩脚本示例原文地址:http:/ shell 编程技术, 但是它们并不适合放入本文档的文本讲解中. 不过它们还是非常有用, 运行和分析它们都是很有意思的事. 译者: 这里留给那些有能力而且有多余时间的读者来详读, 个人认为翻译这些注释有点画蛇添足. 例子 A-1. mailformat: 格式化一个 e-mail 消息1 #!/bin/bash2 # mail-format.sh (ver. 1.1): Format e-mail messages.3 4 # Gets rid of carets, tabs, and also folds excessiv
2、ely long lines.5 6 # =7 # Standard Check for Script Argument(s)8 ARGS=19 E_BADARGS=6510 E_NOFILE=6611 12 if $# -ne $ARGS # Correct number of arguments passed to script?13 then14 echo “Usage: basename $0 filename“15 exit $E_BADARGS16 fi17 18 if -f “$1“ # Check if file exists.19 then20 file_name=$121
3、else22 echo “File “$1“ does not exist.“23 exit $E_NOFILE24 fi25 # =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 $MA
4、XWIDTH characters.39 sed “$sedscript“ $1 | fold -s -width=$MAXWIDTH40 # -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 journal45 #+ extolling a 164K MS Windows utility with similar functionality.46 #47 # An nic
5、e set of text processing utilities and an efficient48 #+ scripting language provide an alternative to bloated executables.49 50 exit 0例子 A-2. rn: 一个非常简单的文件重命名工具这个脚本是 例子 12-19 的一个修改版 . 1 #! /bin/bash2 #3 # Very simpleminded filename “rename“ utility (based on “lowercase.sh“).4 #5 # The “ren“ utility,
6、 by Vladimir Lanin (lanincsd2.nyu.edu),6 #+ does a much better job of this.7 8 9 ARGS=210 E_BADARGS=6511 ONE=1 # For getting singular/plural right (see below).12 13 if $# -ne “$ARGS“ 14 then15 echo “Usage: basename $0 old-pattern new-pattern“16 # As in “rn gif jpg“, which renames all gif files in wo
7、rking directory to jpg.17 exit $E_BADARGS18 fi19 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 do25 if -f “$filename“ # If finds match.26 then27 fname=basename $filename # Strip off path.28 n=echo $fname | sed
8、-e “s/$1/$2/“ # Substitute new for old in filename.29 mv $fname $n # Rename.30 let “number += 1“31 fi32 done 33 34 if “$number“ -eq “$ONE“ # For correct grammar.35 then36 echo “$number file renamed.“37 else 38 echo “$number files renamed.“39 fi 40 41 exit 042 43 44 # Exercises:45 # -46 # What type o
9、f files will this not work on?47 # How can this be fixed?48 #49 # Rewrite this script to process all the files in a directory50 #+ containing spaces in their names, and to rename them,51 #+ substituting an underscore for each space.例子 A-3. blank-rename: 重命名包含空白的文件名这是上一个脚本的简化版. 1 #! /bin/bash2 # blan
10、k-rename.sh3 #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.1
11、1 do12 echo “$filename“ | grep -q “ “ # Check whether filename13 if $? -eq $FOUND #+ contains space(s).14 then15 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 fi20 done 21 22
12、if “$number“ -eq “$ONE“ # For correct grammar.23 then24 echo “$number file renamed.“25 else 26 echo “$number files renamed.“27 fi 28 29 exit 0例子 A-4. encryptedpw: 使用一个本地加密口令, 上传到一个 ftp 服务器. 1 #!/bin/bash2 3 # Example “ex72.sh“ modified to use encrypted password.4 5 # Note that this is still rather i
13、nsecure,6 #+ since the decrypted password is sent in the clear.7 # Use something like “ssh“ if this is a concern.8 9 E_BADARGS=6510 11 if -z “$1“ 12 then13 echo “Usage: basename $0 filename“14 exit $E_BADARGS15 fi 16 17 Username=bozo # Change to suit.18 pword=/home/bozo/secret/password_encrypted.fil
14、e19 # File containing encrypted password.20 21 Filename=basename $1 # Strips pathname out of file name.22 23 Server=“XXX“24 Directory=“YYY“ # Change above to actual server name i=MAX_ITERATIONS; i+)36 do37 38 echo -n “$h“39 # 40 # tab41 42 let “remainder = h % 2“43 if “$remainder“ -eq 0 # Even?44 then45 let “h /= 2“ # Divide by 2.46 else47 let “h = h*3 + 1“ # Multiply by 3 and add 1.48 fi49 50 51 COLUMNS=10 # Output 10 values per line.52 let “line_break = i % $COLUMNS“53 if “$line_break“ -eq 0 54 then55 echo56 fi