1 #!/bin/sh
2 mailfolder=/var/spool/mail/james
3 [ -r "$mailfolder" ]||{ echo "Can not read $mailfolder" ; exit 1; }
4 echo "$mailfolder has mail from:"
5 grep "^From " $mailfolder
6 其中 “^From“ 表示以 From 开头的
1.5、流程语句
1.5.1、if 语句
1 if [ 逻辑表达式 ]; then
2 #command code
3 elif [ 逻辑表达式 ]; then
4 #commandcode
5 else
6 #commandcode
7 fi
如果您为了简洁,想把 then 和 if 放在一行,那就要这样写了:if [ expression ]; then。即在 then 前加一个“;”号。
1.5.2、case语句
case string1 in
str1 ) commands1;;
str2 ) commands2;;
*) commands3;;
esac
例子:
#file lf.gz
lf.gz: gzip compressed data, deflated, original filename,
last modified: Mon Aug 27 23:09:18 2001, os: Unix
脚本:
1 #!/bin/sh
2 ftype=`file "$1"`
3 case "$ftype" in
4 "$1: Zip archive"*) unzip "$1" ;;
5 "$1: gzip compressed"*) gunzip "$1" ;;
6 "$1: bzip2 compressed"*)bunzip2 "$1" ;;
7 *) # * 通配符 代表其他
8 error "File $1 can not be uncompressed with smartzip";;
9 esac
例子:
1 #!/bin/bash
2 echo "Hit a key, then hit return."
3 read Keypress
4
5 case "$Keypress" in
6 [a-z] ) echo "Lowercase letter";;
7 [A-Z] ) echo "Uppercase letter";;
8 [0-9] ) echo "Digit";;
9 * ) echo "Punctuation, whitespace, or other";;
10 esac
11 exit 0
1.5.3、select 语句
尤其擅长于交互式使用。用户可以从一组不同的值中进行选择。
select var in … ; do
break
done
例子:
1 #!/bin/sh
2 echo "What is your favourite OS?"
3 select var in "Linux" "Gnu Hurd" "Free BSD" "Other"; do
4 break
5 done
6 echo "You have selected $var"
#下面是该脚本运行的结果:
What is your favourite OS?
1) Linux
2) Gnu Hurd
3) Free BSD
4) Other
#? 1
You have selected Linux
1.6、循环语句
1.6.1、for语句
for var in 数组列表; do
#command bolock
done
例子1:
1 #!/bin/bash
2 for var in A B C ; do
3 echo "var is $var"
4 done
1 count=1
2 while [ -n "$*"]
3 do
4 echo "this is a parameter number $count $1"
5 shift
6 count='expr $count + 1'
7 done
例子2:
1 while [ -n "$1" ]; do
2 case $1 in
3 -h) help;shift 1;; # function help is called
4 # 执行 help 函数 , shift 1 表示,移除第一个变量 $1 ,则第二个变为: $1
5 -f) opt_f=1;shift 1;; # variable opt_f is set
6 -l) opt_l=$2;shift 2;; # -l takes an argument -> shift by 2
7 --) shift;break;; # end of options
8 -*) echo "error: no such option $1. -h for help";exit 1;;
9 *) break;;
10 esac
11 done
1 while [ "$1"]
2 do
3 if [ "$1"="-i"] then
4 infile=” $2″
5 shift 2
6 else if [ "$1"="-o"] then
7 outfile=”$2″
8 shift 2
9 else
10 echo “Program $0 does not recognize option $1″
11 fi
12 done
1 #!/bin/sh
2 # vim: set sw=4 ts=4 et:
3 help()
4 {
5 cat < b2h -- convert binary to decimal
6 USAGE: b2h [-h] binarynum
7 OPTIONS: -h help text
8 EXAMPLE: b2h 111010
9 will return 58
10 HELP
11 exit 0
12 }
13
14
15 error()
16 { # print an error and exit
17 echo "$1"
18 exit 1
19 }
20
21 lastchar()
22 { # return the last character of a string in $rval
23 if [ -z "$1" ]; then
24 # empty string
25 rval=""
26 return
27 fi
28 # wc puts some space behind the output this is why we need sed:
29 numofchar=`echo -n "$1" wc -c sed ''s/ //g'' `
30 # now cut out the last char
31 rval=`echo -n "$1" cut -b $numofchar`
32 }
33
34 chop()
35 { # remove the last character in string and return it in $rval
36 if [ -z "$1" ]; then
37 # empty string
38 rval=""
39 return
40 fi
41 # wc puts some space behind the output this is why we need sed:
42 numofchar=`echo -n "$1" wc -c sed ''s/ //g'' `
43 if [ "$numofchar" = "1" ]; then
44 # only one char in string
45 rval=""
46 return
47 fi
48 numofcharminus1=`expr $numofchar "-" 1`
49 # now cut all but the last char:
50 rval=`echo -n "$1" cut -b 0-${numofcharminus1}`
51 }
52
53 while [ -n "$1" ]; do
54 case $1 in
55 -h) help;shift 1;; # function help is called
56 --) shift;break;; # end of options
57 -*) error "error: no such option $1. -h for help";;
58 *) break;;
59 esac
60 done
61
62 # The main program
63 sum=0
64 weight=1
65 # one arg must be given:
66 [ -z "$1" ] && help
67 binnum="$1"
68 binnumorig="$1"
69
70 while [ -n "$binnum" ]; do
71 lastchar "$binnum"
72 if [ "$rval" = "1" ]; then
73 sum=`expr "$weight" "+" "$sum"`
74 fi
75 # remove the last position in $binnum
76 chop "$binnum"
77 binnum="$rval"
78 weight=`expr "$weight" "*" 2`
79 done
80 echo "binary $binnumorig is decimal $sum"