Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Linux shell入门之流程控制语句

1.case
脚本:#!/bin/bash#a test about casecase $1 in "lenve")echo "input lenve";; "hello")echo "input hello";; [a-zA-Z]) echo "It"s a letter";; [0-9]) echo "It"s a number";;esac执行效果:

2.while
脚本(注意=两端不能有空格):#!/bin/bash#a test about whilea=1while [ $a -lt 10 ]doecho "hello world!${a}"a=`expr $a + 1`done输出:

3.until循环类似于while循环,不同的是until是判断条件为false时才会执行
#!/bin/bash#a test about untila=11until [ $a -lt 10 ]doecho "hello world!${a}"a=`expr $a + 1`done这是一个无限死循环,输出从hello world11到hello world无穷大。
4.break与continue
continue脚本#!/bin/bash#a test about continuea=1while [ $a -lt 10 ]doif [ $a -eq 5 ]then a=`expr $a + 1` continueelseecho "hello world!${a}"fia=`expr $a + 1`done结果:break脚本:#!/bin/bash#a test about breaka=1while [ $a -lt 10 ]doif [ $a -eq 5 ]then a=`expr $a + 1` breakelseecho "hello world!${a}"fia=`expr $a + 1`done运行结果:

5.shift指令,参数左移,每执行一次,参数序列顺次左移一个位置,$#的位置减1。此指令可用来分别处理每个参数,移出去的参数不可再用。
一个求和的例子:#!/bin/bash#a test about shiftif [ $# -le 0 ]thenecho "there is no parameters"exit 0fisum=0while [ $# -gt 0 ]dosum=`expr $sum + $1`shiftdoneecho $sum千万注意=两端不能有空格
运行结果:
Linux Shell在while中用read从键盘输入  http://www.linuxidc.com/Linux/2015-06/118831.htmLinux Shell 程序调试  http://www.linuxidc.com/Linux/2015-07/119880.htmLinux Shell脚本面试25问  http://www.linuxidc.com/Linux/2015-04/116474.htmLinux/Unix Shell 参数传递到SQL脚本 http://www.linuxidc.com/Linux/2013-03/80568.htmShell脚本中参数传递方法介绍 http://www.linuxidc.com/Linux/2012-08/69155.htmShell脚本传递命令行参数 http://www.linuxidc.com/Linux/2012-01/52192.htmLinux Shell 通配符、转义字符、元字符、特殊字符 http://www.linuxidc.com/Linux/2014-10/108111.htm 本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-08/121390.htm