首页 > 代码库 > linux学习之shell脚本 ------- 控制流结构
linux学习之shell脚本 ------- 控制流结构
[本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020]
今天开始学一些同其他高级语言一样的shell流控制结构
流控制语句:
1. if语句
语句格式:
if condition1 then command1 else condition2 then command2 else command3 fi注:if语句必须以fi终止。
如果没有condition2,则if语句可以简化为如下:
if condition then command1 else command2 fi或:
if condition then command1 fi
例子:
if_test.sh
#!/bin/bash #if_test echo -n "please input two number(a and b):" read a b if [ $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.sh jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./if_test.sh please input two number(a and b):10 10 10 ia equal to 10 jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./if_test.sh please input two number(a and b):10 12 10 is less than 12 jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./if_test.sh please input two number(a and b):8 2 8 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_test echo -n "Enter a number from 1 to 3:" read n case $n in 1) 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:1 You select 1 jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:2 You select 2 jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./case_test.sh Enter a number from 1 to 3:3 You select 3 jesson@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:20 You select *
3. for循环
格式:
for 变量名 in 列表
do
命令1
命令2
done
例子:
for_test1.sh
#!/bin/bash #for_test for loop in 1 2 3 4 5 do echo $loop done运行结果:
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./for_test1.sh 1 2 3 4 5for_test2.sh
#!/bin/bash #for_test2 for loop in orange red blue gray do echo $loop done运行结果:
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./for_test2.sh orange red blue grayfor_test3.sh
#!/bin/bash #for_test3 for loop in `cat name.txt` do echo $loop done运行结果:
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ cat name.txt jesson cherry jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./for_test3.sh jesson cherry
4. until循环
格式:
until 条件
do
命令
done
until_test.sh
#!/bin/bash #until_test i=10 until [ $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 10 9 8 7 6 5 4 3 2 1 0
5. while循环
格式:
while 命令
do
命令
.......
done
例子:
while_test1.sh
#!/bin/bash #while_test1 while echo -n "输入你喜欢的明星:";read Mingxing do 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_test while read NAME do echo $NAME done <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 jesson cherry
6. break和continue
break [n]
-退出循环
-如果是在一个嵌入循环里,可以指定n来跳出的循环个数。
continue
-跳过循环步
注意,这和其他高级语言中的一样,continue命令类似于break,只有一点差别,它不会跳出循环,而是路过当前循环步。
例子:
break_test.sh
#!/bin/bash #break_test.sh while : 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; ;; esac done给予权限,执行如下:
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):1 You enter anumber between 1 and 5 Enter any number(1,...,5):2 You enter anumber between 1 and 5 Enter any number(1,...,5):3 You enter anumber between 1 and 5 Enter any number(1,...,5):4 You enter anumber between 1 and 5 Enter any number(1,...,5):5 You enter anumber between 1 and 5 Enter any number(1,...,5):6 Wrong number,Bye
continue_test.sh
#!/bin/bash #continue_break_test while : 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 esac done同样,给予可执行权限,执行
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):2 You enter a number between 1 and 5 Enter any number(1,...,5):3 You enter a number between 1 and 5 Enter any number(1,...,5):4 You enter a number between 1 and 5 Enter any number(1,...,5):5 You enter a number between 1 and 5 Enter any number(1,...,5):8 Wrong number,continue(y/n)?:y Enter any number(1,...,5):1 You enter a number between 1 and 5 Enter any number(1,...,5):79 Wrong number,continue(y/n)?:n
linux学习之shell脚本 ------- 控制流结构