您的位置:首页 > 其它

【学神-RHEL6.5】1-18case for while语句

2015-12-09 08:41 351 查看

case for while语句

所讲内容:
case: 流控制语句循环语句for…done和while语句使用(()) 扩展shell中算数运算算使用方法循环语句嵌套跳出循环:break和continuecase:流控制语句

1、case语句作用
case语句用于多种情况的条件判断,当满足某种条件时,就会执行条件下的语句
2、case语法
case 变量 in
字符串1)
语句列表1
;;
字符串2)
语句列表2
;;
….
字符串n)
语句列表n
;;
*)
默认执行的语句
;;
esac
3、case语句执行流程



4、case语句举例
[root@test~]#vi testcase.sh#根据不同的输入,显示不同的结果
#!/bin/bash
#Thisis a test case shell
read-p "please input a number (1-4):" num
case$num in
1)
echo"your input number is 1"
;;
2)
echo"your input number is 2"
;;
3)
echo"your input number is 3"
;;
4)
echo"your input number is 4"
;;
*)
echo"your input number is wrong!"
;;
Esac
[root@test~]#sh testcase.sh#测试结果
pleaseinput a number (1-4):1
yourinput number is 1
[root@test~]#sh testcase.sh
pleaseinput a number (1-4):6
yourinput number is wrong!

循环语句

1、for…done语句
1) for…done语句执行过程
每次循环都会在列表中取值,然后执行do后面的语句,直至列表中的所有值全部取完后循环结束
2)for…done语句语法
for 变量 in 取值列表
do
语句列表
done

##取值列表可以是文件、范围或者指定的值
3)for…done语句举例
(1) 取值列表为文件
[root@test~]#echo aaabbb ccc ddd> test.txt
[root@test~]#vi file_for1.sh#在列表中取值,然后打印出来
#!/bin/bash
#Thisis a test for..done shell1
file=`cat/root/test.txt`
foriin $file
do
echo$i
done
[root@test~]#sh file_for1.sh#测试结果
aaa
bbb
ccc
ddd
(2) 取值列表为范围
[root@test~]#vi file_for2.sh#测试网段内主机存活情况
#!/bin/bash
#Thisis a test for..done shell2
PREFIX=10.0.1.
foriin {200..210}
do
echo-n "${PREFIX}$i "
ping-c1 -i0.1 ${PREFIX}${i} >/dev/null2>&1
if ["$?" -eq0 ];then
echo"OK"
else
echo"Failed"
fi
done
[root@test~]#sh file_for2.sh#测试结果
10.0.1.200OK
10.0.1.201OK
10.0.1.202Failed
10.0.1.203Failed
10.0.1.204Failed
10.0.1.205OK
10.0.1.206OK
10.0.1.207OK
10.0.1.208Failed
10.0.1.209Failed
10.0.1.210Failed
还可以用下面这种方式表示
[root@test~]#vi file_for3.sh#计算1到100的和
#!/bin/bash
#Thisis a test for..done shell3
ans=0
for((i=1;i<=100;i++))
do
letans+=$i
done
echo$ans
[root@test~]#sh file_for3.sh#测试结果
5050
(3)取值列表为指定的值
[root@test~]#vi file_for4.sh #循环打印列表内的数值
#!/bin/bash
##Thisis a test for..done shell4
foriin "123" "456" "789"
do
echo$i
done
[root@test~]#sh file_for4.sh#测试结果
123
456
789
2、while语句
1)while语句执行过程
每次循环都会判断while后面的条件是否成立,如果成立执行do后面的语句,如果不成立则循环结束
2)while语法
while 条件 ##条件可以使用布尔值
do
语句列表
done
3)while语句举例
[root@test~]#vi while.sh#循环打印满足条件的结果
#!/bin/bash
#Thisis a test while shell
x=1
while[ $x -le 10 ]
do
echo$x
letx=x+1
done
[root@test~]#sh while.sh#测试结果
1
2
3
4
5
6
7
8
9
10

使用(()) 扩展shell中算数运算使用方法

1、双括号的作用
1)整数扩展。这种扩展计算是整数型的计算,不支持浮点型。((exp))结构扩展并计算一个算术表达式的值,如果表达式的结果为0,那么返回的退出状态码为1,或者 是"假",而一个非零值的表达式所返回的退出状态码将为0,或者是"true"。若是逻辑判断,表达式exp为真则为1,假则为0。
2)只要括号中的运算符、表达式符合C语言运算规则,都可用在$((exp))中,甚至是三目运算符。作不同进位(如二进制、八进制、十六进制)运算时,输出结果全都自动转化成了十进制。如:echo $((16#5f)) 结果为95 (16进位转十进制)
3)单纯用 (( )) 也可重定义变量值,比如 a=5; ((a++)) 可将 $a 重定义为6
4)常用于算术运算比较,双括号中的变量可以不使用$符号前缀。括号内支持多个表达式用逗号分开。只要括号中的表达式符合C语言运算规则,比如可以直接使用for((i=0;i<5;i++)), 如果不使用双括号, 则为for i in `seq 0 4`或者for i in {0..4}。再如可以直接使用if (($i<5)), 如果不使用双括号, 则为if [ $i -lt 5 ]。
2、双括号的语法
((表达式1;表达式2…))
3、双括号举例
[root@test~]#vi shuangkuohao.sh ##列出小于100的2的幂值
#!/bin/bash
#Thisis a test (()) shell
echo"The while loop example."
echo
VAR1=1
while((VAR1<100))
do
echo"Value of the variable is : $VAR1"
((VAR1=VAR1*2))
done
echo
echo"The loop execution is finished"
[root@test~]#shshuangkuohao.sh#测试结果
Thewhile loop example.

Valueof the variable is : 1
Valueof the variable is : 2
Valueof the variable is : 4
Valueof the variable is : 8
Valueof the variable is : 16
Valueof the variable is : 32
Valueof the variable is : 64

Theloop execution is finished

循环语句嵌套

举例:
[root@test~]#visanjiao.sh#循环嵌套用指定符号打印出三角形
#!/bin/bash
#Thisis a test Loop nest shell
read-p "Please enter the number of rows to print:" line
read-p "Please enter the characters Print:" char
a=1
while[ $a -le $line ] #外循环控制打印出的行数
do b=1
while [ $b -le $a ] #内循环控制每行打印的个数
do echo -n "$char" #-n为输出的内容不换行
b=` expr $b + 1 `
done
echo
a=`expr$a + 1`
done
[root@test~]#shsanjiao.sh#测试结果
Pleaseenter the number of rows to print:5#输入打印的行数
Pleaseenter the characters Print:* #输入要打印的符号
*
**
***
****
*****
跳出循环:break和continue

1、break和continue在循环语句中的作用
break:跳出整个循环
continue:跳出本次循环,进行下次循环
举例:
[root@test~]#vitest.sh#输入一个指定的数字,如果输入错误则重新输入,只有在输入正确的数字才会退出
#!/bin/bash
#Thisis test break continue shell
while[ ture ]
do
read-p "please input a number:" num
if [$num -eq 0 ] ; then
echo"you input is ok!"
break
else
echo"please input again!"
continue
fi
done
[root@test~]#shtest.sh#测试结果
pleaseinput a number:1
pleaseinput again!
pleaseinput a number:2
pleaseinput again!
pleaseinput a number:0
youinput is ok!
学习过程中如果问题,请留言。更多内容请加:
学神IT-linux讲师-RM老师QQ:2805537762
学神IT-戚老师QQ:3341251313
学神IT-旭斌QQ:372469347
学神IT教育RHEL7交流群:468845589

学神教育学神IT-1508班-VIP-立伟编译提供

本文出自 “学神IT教育-讲师MK” 博客,请务必保留此出处http://xuegodlinux.blog.51cto.com/10844319/1720985
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: