--------------------------------------------------------------------------------
作者:joyo
与其他OS下的编程语言一样,UNIX中的流程控制语句也是构成应用程序的基石。因此笔者认为有必要了解一下UNIX中的流程控制语句,如果你致力于走进UNIX程序世界的话。在介绍流程控制之前,我们先来看看test命令。test命令的参数是条件判断式,当条件为真时则传回非零值,而条件为假时则传回零。在所有的流程控制都必须用到test命令来判断真伪。
test $# = 0
如果执行这个程序没有参数时,会传回非零值代表"$# = 0"这个条件成立。反之则会传回零。
以下介绍各种流程控制:
1. if then
语法以及流程图如下
│ FALSE
<condition>—┐
│TRUE │
then-commands │
├————┘
│
if (condition)
then
then-commands
fi
condition 是一个test命令。下文所介绍的各种流程中的condition如无特殊说明都是test命令。
┌———————————┐
│if (test $# != 0) │
│ then │
│ echo Arg1: $1 │
│fi │
└———————————┘
$ chkarg Hello
Arg1: Hello
$ chkarg
$
2. if then else
语法以及流程图如下
│ FALSE
<condition>—————┐
│TRUE │
then-commands else-commands
├————————┘
│
if (condition)
then-commands
else
fi
3. if then elif
语法以及流程图如下
│ FALSE
<condition1>—┐
│TRUE │ FALSE
commands1 <condition2>—┐
│TRUE │ │
│ commands2 commands3
├—————┴——— ┘
│
if (condition1)
then
commands1
elif (condition2)
then
commands2
else
commands3
commands3
fi
echo 'word 1: c'
read word1
echo 'word 2: c'
read word2
echo 'word 3: c'
read word3
if (test "$word1" = "$word2" -a "$word2" = "$word3")
then
echo 'Match: words 1, 2, & 3'
elif (test "$word1" = "$word2")
then
echo 'Match: words 1 & 2'
elif (test "$word1" = "$word3")
then
echo 'Match: words 1 & 3'
elif (test "$word2" = "$word3")
then
echo 'Match: words 2 & 3'
else
echo 'No match'
fi
4. for in
语法以及流程图如下
│ FALSE
┌—<arg-list还有东西吗?>—┐
│ │TRUE │
│ 从arg-list取得一项 │
│ 放到变数var │
│ │ │
│ commands │
└——————┘ │
┌———————┘
│
for var in arg-list
do
commands
done
┌———————————┐
│for a in xx yy zz │
│ do │
│ echo $a │
│done │
└———————————┘
结果如下:
xx
yy
yy
zz
5. for
语法以及流程图如下
│ FALSE
┌—<参数中还有东西吗?>—┐
│ │TRUE │
│ 从参数中取得一项 │
│ 放到变数var │
│ │ │
│ commands │
└—————┘ │
┌———————┘
│
for var
do
commands
done
┌———————————┐
│for a │
│ do │
│ echo $a │
│done │
└———————————┘
$lstarg xx yy zz
xx
yy
yy
zz
6. while
语法以及流程图如下
│ FALSE
┌—<condition>—┐
│ │TRUE │
│ commands │
└————┘ │
┌————┘
│
while (condition)
do
commands
done
┌———————————————┐
│number=0 │
│while (test $number -lt 10) │
│ do │
│ echo "$numberc" │
│ number=`expr $number + 1` │
│done │
│echo │
└———————————————┘
结果如下:
0123456789
7. until
语法以及流程图如下
│ TRUE
┌—<condition>—┐
│ │FALSE │
│ commands │
└————┘ │
┌————┘
│
until (condition)
do
commands
done
它和while 的不同只在於while 是在条件为真时执行回圈,而until 是在条件为假时执行回圈。
8. break及continue
这两者是用於for, while, until 等回圈控制下。break 会跳至done后面执行,而continue会跳至done执行,继续执行回圈。
9. case
语法以及流程图如下
│ TRUE
<str=pat1>————commands1—┐
│FALSE TRUE │
<str=pat2>————commands2—┤
│FALSE TRUE │
<str=pat3>————commands3—┤
│FALSE │
├————————————┘
│
case str in
pat1) commands1;;
pat2) commands2;;
pat3) commands3;;
esac
而pat 除了可以指定一些确定的字串,也可以指定字串的集合,如下:
* 任意字串
? 任意字元
[abc] a, b, 或c三字元其中之一
[a-n] 从a到n的任一字元
| 多重选择
┌———————————————┐
│echo 'Enter A, B, or C: c' │
│read letter │
│case $letter in │
│ A|a) echo 'You entered A.';;│
│ B|b) echo 'You entered B.';;│
│ C|c) echo 'You entered C.';;│
│ *) echo 'Not A, B, or C';; │
│esac │
└———————————————┘
10. 函数
格式如下
function-name()
{
commands
}
而要呼叫此函数,就像在命令列下直接下命令一般。
jysww 回复于:2003-02-19 23:51:27 |
UP! |
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/