文档详情

Linux操作系统之第4章.ppt

发布:2016-12-13约1.11万字共79页下载文档
文本预览下载声明
例 #!/bin/bash for file in *.txt do if test –f $file then echo ----------------------------------------------------- echo $file echo -----------------------------------------------------“ cat $file fi done #end #!/bin/bash for p in 1 2 3 4 5 do echo $p done #end #!/bin/bash for ((p=1; p6; p++)) do ??? echo $p done #end 例: #!/bin/bash #This is example 3 about for. sum=0 for p in $* do let sum=$sum+$p done echo the total is $sum #end While循环语句 while expression do commands done 例:计算1~5的平方 #!/bin/bash let int=1 while [ $int -le 5 ] do let sq=$int * $int echo $sq let int=$int +1 done echo finished #end until循环语句 until expression do commands done break和continue break语句用于中断循环的执行,将程序流程移至循环语句结束之后的下一个命令。 continue语句则忽略之后的命令,将程序流程移至循环开始的地方。 break和continue语句都可以加上数字,以指示要跳出的循环数目。 while expression1 do while expression2 do : continue 2 : done done 例 模拟工业上的操作流程控制 #!/bin/bash for x in a b c d e f g h i do for y in 1 2 3 4 5 6 7 8 9 do echo current job is $x$y echo input n to do next job echo s to skip the other jobs in current level echo x to terminate all jobs read action if [ $action = n ] then echo do next job continue elif [ $action = s ] then echo skip the other jobs in current level continue 2 elif [ $action = x ] then echo terminate all jobs break 2 fi done done #end 函数的定义和使用 函数的定义 functionname () { Shellcommands } 函数的使用 函数的使用方法与外部命令一样,只要直接使用函数名即可。在使用函数时,一样可以传入参数。函数处理参数的方式与脚本文件处理命令行参数的方法是一样的。在函数中,$1是指传入
显示全部
相似文档