分类: Python/Ruby
2012-06-04 13:14:09
3:控制结构
3.1 if语句
if语句非常简单,它对某个命令的执行结果进行测试,然后根据判断结果有条件的执行一句语句。
基本结构
if condition
then
statement
else
statement
fi
实例:
#!/bin/sh
echo "Is It Morning? answer yes or no"
read time
if [ "$time"="yes" ];then
echo "It is Morning !"
else
echo "It is Afternoon !"
fi
exit 0
执行结果:
lishuo@lishuo-Rev-1-0:~/桌面$ ./t
Is It Morning? answer yes or no
yes
It is Morning !
lishuo@lishuo-Rev-1-0:~/桌面$ ./t
Is It Morning? answer yes or no
no
It is Morning !
3.2 elif语句
elif语句可以在if判断失败之后继续进行判断,它的基本结构如下:
if condition ; then
statements
elif condition ; then
statements
else
statements
fi
实例:
#!/bin/sh
echo "Is It Morning ? Answer yes or no "
read time
if [ "$time" = "yes" ];then
echo "This is Morning !"
elif [ "$time" = "no" ];then
echo "This is Afternoon !"
else
echo "Please Type again !"
fi
exit 0
执行结果:
lishuo@lishuo-Rev-1-0:~/桌面$ ./t
Is It Morning ? Answer yes or no
yes
This is Morning !
3.3 for语句
for语句适合对一系列字符串进行循环处理。这些字符串可以在程序里被简单列出,更常见的做法是把它与shell的文件名扩展结果组合在一起使用。
基本结构
for variable in values
do
statements
done
variable会取遍values里面的每一个值。
实例:
#!/bin/sh
for foo in bar fud 43
do
echo "$foo"
done
exit 0
执行结果:
lishuo@lishuo-Rev-1-0:~/桌面$ ./t
bar
fud
43
3.4 while语句
for循环适合对一系列字符串进行循环处理,但在需要执行特定次数命令的场合,ehile更合适。
基本结构
while condition
do
statements
done
实例:
1:字符串循环
#!/bin/sh
echo "Please Enter the passwors !"
read word
while [ "$word" != "secret" ];do
echo "Password is worng ! Please Enter again !"
read word
done
echo "Welcome to us !"
exit 0
执行结果:
lishuo@lishuo-Rev-1-0:~/桌面$ ./t
Please Enter the passwors !
asde
Password is worng ! Please Enter again !
secret
Welcome to us !
2:执行特定次数的循环
#!/bin/sh
foo=1
while [ "$foo" -le 5 ];do
echo "this is $foo -le 5"
foo=$(($foo + 1))
done
echo "Welcome back !"
exit 0
执行结果:
lishuo@lishuo-Rev-1-0:~/桌面$ ./t
this is 1 -le 5
this is 2 -le 5
this is 3 -le 5
this is 4 -le 5
this is 5 -le 5
Welcome back !
3.5 until语句
until语句与while正好相反,有点类似与C语言里面的do,while结构。
基本结构
until condition
do
statements
done
实例:
#!/bin/sh
var=20
until [ "$var" -eq 15 ];do
echo "var is $var now !"
var=$(($var - 1))
done
echo "This is end !"
exit 0
运行结果:
lishuo@lishuo-Rev-1-0:~/桌面$ ./t
var is 20 now !
var is 19 now !
var is 18 now !
var is 17 now !
var is 16 now !
This is end !
3.6 case语句
基本结构
case variable in
pattern [ | pattern] ...) statements ;;
pattern [ | pattern] ...) statements ;;
....
esac
实例:
#!/bin/sh
echo "Is It Morning ? answer yes or no"
read time
while true ; do
case "$time" in
y | ye | yes | Y | YES) echo "Good Morning , sir !";;
[Nn] | [Nn][Oo]) echo "Good Afternoon , sir !";;
[Ee]) echo "jump to the end !" ; break;;
*) echo "worng answer ! kick you ares !";;
esac
read time
done
exit 0
执行结果:
lishuo@lishuo-Rev-1-0:~/桌面$ ./t
Is It Morning ? answer yes or no
yes
Good Morning , sir !
y
Good Morning , sir !
ye
Good Morning , sir !
n
Good Afternoon , sir !
no
Good Afternoon , sir !
nO
Good Afternoon , sir !
NO
Good Afternoon , sir !
N
Good Afternoon , sir !
Nq
worng answer ! kick you ares !
e
jump to the end !
3.7 &&和||
1:&&允许我们照这样的方式执行一系列的命令:只有在前面的命令都成功执行的情况下才执行后一条命令。
语法:
statement1 && statement2 && statement3 && ...
实例:
#!/bin/sh
touch file_one
rm -f file_two
if [ -f file_one ] && echo "file_one exits !" && [ -f file_two ] && echo "file_two exits !" ; then
echo "exec if"
else
echo "exec else"
fi
exit 0
执行结果:
lishuo@lishuo-Rev-1-0:~/桌面$ ./t
file_one exits !
exec else
2:||允许我们持续执行一系列的命令直到有一条命令成功为止,其后的命令将不在执行。
语法:
statement1 || statement2 || statement3 || ...
实例:
#!/bin/sh
touch file_one
rm -f file_two
if [ -f file_two ] || echo "file_two is not exit !" || [ -f file_one ] || echo "file_one is not exit !" ; then
echo "exec if"
else
echo "exec else"
fi
rm -f file_one
exit 0
执行结果:
lishuo@lishuo-Rev-1-0:~/桌面$ ./t
file_two is not exit !
exec if
3.8 {}
{}起到封装的作用,它可以把多条语句构造成一个语句块。
3.9 函数
shell函数的基本结构
function_name () {
statements
}
实例:
#!/bin/sh
foo(){
echo "This is a Function !"
}
echo "starting...."
foo
echo "ended !"
exit 0
执行结果:
lishuo@lishuo-Rev-1-0:~/桌面$ ./t
starting....
This is a Function !
ended !
这些内容是我平时阅读的笔记,源自《linux程序设计第三版》,详细内容请看原书