linux学习之shell脚本 ------- 控制流结构
来源:互联网 发布:网络的攻击与防范 编辑:程序博客网 时间:2024/05/02 00:40
[本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020]
今天开始学一些同其他高级语言一样的shell流控制结构
流控制语句:
1. if语句
语句格式:
if condition1 then command1else condition2then command2else command3fi注:if语句必须以fi终止。
如果没有condition2,则if语句可以简化为如下:
if conditionthen command1else command2fi或:
if conditionthen command1fi
例子:
if_test.sh
#!/bin/bash#if_testecho -n "please input two number(a and b):"read a bif [ $a -lt $b ]then echo "$a is less than $b"elif [ $a -gt $b ]then echo "$a is greater than $b"else echo "$a ia equal to $b"fi
给予可执行权限,执行该脚本:
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx if_test.shjesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./if_test.sh please input two number(a and b):10 1010 ia equal to 10jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./if_test.sh please input two number(a and b):10 1210 is less than 12jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./if_test.sh please input two number(a and b):8 28 is greater than 2注意,其中用于了比较运算符,更加详细的比较运算符,请参考man test。
2. case语句
语句格式:
case value in
模式1)
command1
;;
模式2)
command2
;;
esac
注意:
1case取值后面必须为单词in,每一模式必须以右括号结束,取值可以为变量或者常数。
2 模式匹配符*表示任意字符;?表示任意单字符;[...]表示类或范围中任意字符。
例子:
case_test.sh
#!/bin/bash#case_testecho -n "Enter a number from 1 to 3:"read ncase $n in1) echo "You select 1" ;;2) echo "You select 2" ;;3) echo "You select 3" ;;*) echo "You select *" ;;esac
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx case_test.sh jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:1You select 1jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:2You select 2jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:3You select 3jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:*You select *jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:20You select *
3. for循环
格式:
for 变量名 in 列表
do
命令1
命令2
done
例子:
for_test1.sh
#!/bin/bash#for_testfor loop in 1 2 3 4 5do echo $loopdone运行结果:
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./for_test1.sh 12345for_test2.sh
#!/bin/bash#for_test2for loop in orange red blue graydo echo $loopdone运行结果:
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./for_test2.sh orangeredbluegrayfor_test3.sh
#!/bin/bash#for_test3for loop in `cat name.txt`do echo $loopdone运行结果:
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ cat name.txt jessoncherryjesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./for_test3.sh jessoncherry
4. until循环
格式:
until 条件
do
命令
done
until_test.sh
#!/bin/bash#until_testi=10until [ $i -lt 0 ]do echo $i let i=i-1 # ((i=$i-1)) #i=$[i-1]done给予权限,执行结果如下:
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx until_test.sh jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./until_test.sh 109876543210
5. while循环
格式:
while 命令
do
命令
.......
done
例子:
while_test1.sh
#!/bin/bash#while_test1while echo -n "输入你喜欢的明星:";read Mingxingdo echo "$Mingxing 是你喜欢的明星"done给予权限,执行:
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx while_test1.sh jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./while_test1.sh 输入你喜欢的明星:周杰周杰 是你喜欢的明星输入你喜欢的明星:孙楠孙楠 是你喜欢的明星while_test2.sh
#!/bin/bash#while_testwhile read NAMEdo echo $NAMEdone <name.txt给予权限,执行:
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx while_test2.sh jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./while_test2.sh jessoncherry
6. break和continue
break [n]
-退出循环
-如果是在一个嵌入循环里,可以指定n来跳出的循环个数。
continue
-跳过循环步
注意,这和其他高级语言中的一样,continue命令类似于break,只有一点差别,它不会跳出循环,而是路过当前循环步。
例子:
break_test.sh
#!/bin/bash#break_test.shwhile :do echo -n "Enter any number(1,...,5):" read ANS case $ANS in 1|2|3|4|5) echo "You enter anumber between 1 and 5" ;; *) echo "Wrong number,Bye" break; ;; esacdone给予权限,执行如下:
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx break_test.sh jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./break_test.sh Enter any number(1,...,5):1You enter anumber between 1 and 5Enter any number(1,...,5):2You enter anumber between 1 and 5Enter any number(1,...,5):3You enter anumber between 1 and 5Enter any number(1,...,5):4You enter anumber between 1 and 5Enter any number(1,...,5):5You enter anumber between 1 and 5Enter any number(1,...,5):6Wrong number,Bye
continue_test.sh
#!/bin/bash#continue_break_testwhile :do echo -n "Enter any number(1,...,5):" read ANS case $ANS in 1|2|3|4|5) echo "You enter a number between 1 and 5" ;; *) echo -n "Wrong number,continue(y/n)?:" read IS_CONTINUE case $IS_CONTINUE in y|yes|Y|Yes) continue ;; *) break ;; esac esacdone同样,给予可执行权限,执行
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx continue_test.sh jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./continue_test.sh Enter any number(1,...,5):2You enter a number between 1 and 5Enter any number(1,...,5):3You enter a number between 1 and 5Enter any number(1,...,5):4You enter a number between 1 and 5Enter any number(1,...,5):5You enter a number between 1 and 5Enter any number(1,...,5):8Wrong number,continue(y/n)?:yEnter any number(1,...,5):1You enter a number between 1 and 5Enter any number(1,...,5):79Wrong number,continue(y/n)?:n
0 0
- linux学习之shell脚本 ------- 控制流结构
- Linux Shell 脚本编程(4)—控制流结构
- linux shell 学习之第15章 控制脚本
- shell脚本中的控制流结构
- shell脚本中的控制流结构
- Linux Shell笔记之脚本控制
- Linux学习之shell脚本
- linux之shell脚本学习
- Shell脚本学习之控制语句
- Linux重新学习--shell script--控制结构
- linux学习之shell脚本 ------- shell函数
- Shell脚本结构流程控制
- LINUX SHELL控制结构
- shell学习--控制结构
- Linux 系统学习 之 shell脚本学习
- linux学习之shell脚本 ------- 基础
- linux学习之shell脚本 ------- 变量
- linux学习之shell脚本 ------- 文本过滤
- Field类的setAccessible()方法
- iOS开发- tintColor 与backgroundColor区别研究
- Win7下modelsim编译Xilinx库
- 职业院校人体解剖学实验室文化建设研究
- 第2章 2.10 Servlet介绍
- linux学习之shell脚本 ------- 控制流结构
- JVM学习笔记(一)------基本结构
- Java虚拟机(六):Java编程和执行过程
- 运算符重载实例——Nybble
- CSS从大图片上截取小图标的使用
- USACO 5.3 Network of Schools
- Android 自定义View (一)
- 寒假项目1-动态链表体验(改造)(5)
- 人体解剖学标本长廊的构成、管理及其优势