中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 操作系统 > SCO_Unix
Unix Shell 中的流程控制语句
作者:未知 时间:2005-09-13 14:55 出处:ChinaUnix.net 责编:chinaitpower
              摘要:Unix Shell 中的流程控制语句

  

--------------------------------------------------------------------------------
 
作者: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!

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有