Shell 脚本基础学习 (四)

现在我们来讨论编写一个脚本的一般步骤。任何优秀的脚本都应该具有帮助和输入参数。并且写一个伪脚本(framework.sh),该脚本包含了大多数脚本都需要的框架结构,是一个非常不错的主意。这时候,在写一个新的脚本时我们只需要执行一下copy命令:

cp framework.sh myscript

然后再插入自己的函数。

让我们再看个例子:

二进制到十进制的转换

脚本 b2d 将二进制数 (比如 1101) 转换为相应的十进制数。这也是一个用expr命令进行数学运算的例子:

View Code
  1 #!/bin/sh 
2
3 # vim: set sw=4 ts=4 et:
4
5 help()
6
7 {
8
9 cat <
10
11 b2h -- convert binary to decimal
12
13 USAGE: b2h [-h] binarynum
14
15 OPTIONS: -h help text
16
17 EXAMPLE: b2h 111010
18
19 will return 58
20
21 HELP
22
23 exit 0
24
25 }
26
27 error()
28
29 {
30
31 # print an error and exit
32
33 echo "$1"
34
35 exit 1
36
37 }
38
39 lastchar()
40
41 {
42
43 # return the last character of a string in $rval
44
45 if [ -z "$1" ]; then
46
47 # empty string
48
49 rval=""
50
51 return
52
53 fi
54
55 # wc puts some space behind the output this is why we need sed:
56
57 numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `
58
59 # now cut out the last char
60
61 rval=`echo -n "$1" | cut -b $numofchar`
62
63 }
64
65 chop()
66
67 {
68
69 # remove the last character in string and return it in $rval
70
71 if [ -z "$1" ]; then
72
73 # empty string
74
75 rval=""
76
77 return
78
79 fi
80
81 # wc puts some space behind the output this is why we need sed:
82
83 numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `
84
85 if [ "$numofchar" = "1" ]; then
86
87 # only one char in string
88
89 rval=""
90
91 return
92
93 fi
94
95 numofcharminus1=`expr $numofchar "-" 1`
96
97 # now cut all but the last char:
98
99 rval=`echo -n "$1" | cut -b 0-${numofcharminus1}`
100
101 }
102
103 while [ -n "$1" ]; do
104
105 case $1 in
106
107 -h) help;shift 1;; # function help is called
108
109 --) shift;break;; # end of options
110
111 -*) error "error: no such option $1. -h for help";;
112
113 *) break;;
114
115 esac
116
117 done
118
119 # The main program
120
121 sum=0
122
123 weight=1
124
125 # one arg must be given:
126
127 [ -z "$1" ] && help
128
129 binnum="$1"
130
131 binnumorig="$1"
132
133 while [ -n "$binnum" ]; do
134
135 lastchar "$binnum"
136
137 if [ "$rval" = "1" ]; then
138
139 sum=`expr "$weight" "+" "$sum"`
140
141 fi
142
143 # remove the last position in $binnum
144
145 chop "$binnum"
146
147 binnum="$rval"
148
149 weight=`expr "$weight" "*" 2`
150
151 done
152
153 echo "binary $binnumorig is decimal $sum"

 

该脚本使用的算法是利用十进制和二进制数权值 (1,2,4,8,16,..),比如二进制"10"可以这样转换成十进制:

0 * 1 + 1 * 2 = 2

为了得到单个的二进制数我们是用了lastchar 函数。该函数使用wc –c计算字符个数,然后使用cut命令取出末尾一个字符。Chop函数的功能则是移除最后一个字符。

文件循环程序

或许您是想将所有发出的邮件保存到一个文件中的人们中的一员,但是在过了几个月以后,这个文件可能会变得很大以至于使对该文件的访问速度变慢。下面的 脚本rotatefile可

以解决这个问题。这个脚本可以重命名邮件保存文件(假设为outmail)为outmail.1,而对于 outmail.1就变成了outmail.2 等等等等...

View Code
 1 #!/bin/sh 
2
3 # vim: set sw=4 ts=4 et:
4
5 ver="0.1"
6
7 help()
8
9 {
10
11 cat <
12
13 rotatefile -- rotate the file name
14
15 USAGE: rotatefile [-h] filename
16
17 OPTIONS: -h help text
18
19 EXAMPLE: rotatefile out
20
21 This will e.g rename out.2 to out.3, out.1 to out.2, out to out.1
22
23 and create an empty out-file
24
25 The max number is 10
26
27 version $ver
28
29 HELP
30
31 exit 0
32
33 }
34
35 error()
36
37 {
38
39 echo "$1"
40
41 exit 1
42
43 }
44
45 while [ -n "$1" ]; do
46
47 case $1 in
48
49 -h) help;shift 1;;
50
51 --) break;;
52
53 -*) echo "error: no such option $1. -h for help";exit 1;;
54
55 *) break;;
56
57 esac
58
59 done
60
61 # input check:
62
63 if [ -z "$1" ] ; then
64
65 error "ERROR: you must specify a file, use -h for help"
66
67 fi
68
69 filen="$1"
70
71 # rename any .1 , .2 etc file:
72
73 for n in 9 8 7 6 5 4 3 2 1; do
74
75 if [ -f "$filen.$n" ]; then
76
77 p=`expr $n + 1`
78
79 echo "mv $filen.$n $filen.$p"
80
81 mv $filen.$n $filen.$p
82
83 fi
84
85 done
86
87 # rename the original file:
88
89 if [ -f "$filen" ]; then
90
91 echo "mv $filen $filen.1"
92
93 mv $filen $filen.1
94
95 fi
96
97 echo touch $filen
98
99 touch $filen


这个脚本是如何工作的呢?在检测用户提供了一个文件名以后,我们进行一个9到1的循环。文件9被命名为10,文件8重命名为9等等。循环完成之后,我们将原始文件命名为文件1

同时建立一个与原始文件同名的空文件。
调试

最简单的调试命令当然是使用echo命令。您可以使用echo在任何怀疑出错的地方打印任何变量值。这也是绝大多数的shell程序员要花费80%的时间来调试程序的原因。Shell程序的

好处在于不需要重新编译,插入一个echo命令也不需要多少时间。

shell也有一个真实的调试模式。如果在脚本"strangescript" 中有错误,您可以这样来进行调试:

sh -x strangescript

这将执行该脚本并显示所有变量的值。

shell还有一个不需要执行脚本只是检查语法的模式。可以这样使用:

sh -n your_script

这将返回所有语法错误

这里Linux shell脚本基础学习就全部结束了。感谢大家的支持。

posted @ 2012-04-07 23:48  Matrix54  阅读(261)  评论(0编辑  收藏  举报