分类: 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程序设计第三版》,详细内容请看原书