本节主要内容
- while循环控制结构
- if条件判断
-
until循环控制结构
1. while循环控制结构
本节例子来源:http://blog.chinaunix.net/uid-25880122-id-2901409.html
语法格式:
while expression
do
command
command
done
(1)计数器格式
适用于循环次数已知或固定时
root@sparkslave02:~/ShellLearning/Chapter13# vim whileLoop.sh
.#!/bin/bash
i=1
while(($i<5))
do
echo $i
let i++
done
root@sparkslave02:~/ShellLearning/Chapter13# chmod a+x whileLoop.sh
root@sparkslave02:~/ShellLearning/Chapter13# ./whileLoop.sh
1
2
3
4
(2)标志符结束while循环
root@sparkslave02:~/ShellLearning/Chapter13# vim flagWhileLoop.sh
#!/bin/bash
echo "Please input the num (1~~10): "
#接受用户输入
read num
while [[ $num != 4 ]]
do
#if语句,后面详细介绍,这里判断是否小于4
if [ $num -lt 4 ]
then
echo "Too small ,Try again.."
read num
#判断是否大于4
elif [ $num -gt 4 ]
then
echo "Too big ,Try again.. "
read num
else
exit 0
fi
done
echo "Yes ,you are right !!"
root@sparkslave02:~/ShellLearning/Chapter13# chmod a+x flagWhileLoop.sh root@sparkslave02:~/ShellLearning/Chapter13# ./flagWhileLoop.sh
Please input the num (1~~10):
4
Yes ,you are right !!
## 2. if条件判断##
参考:http://blog.chinaunix.net/uid-20735106-id-3434959.html
shell 脚本中的if条件判断功能十分强大,但使用也异常复杂,其语法格式:
if 条件
then
Command
else
Command
#if条件判断的结束,用反拼表示
fi
最常用的判断为:判断字符串、判断数字、判断文件及逻辑判断等
(1)判断字符串
1.if [ str1=str2 ];then fi ----当两个字符串相同时返回真
2.if [ str1!=str2 ];then fi ----当两个字符串不相等时返回真
3.if [ -n str1 ];then fi ----当字符串的长度大于0时返回真 (判断变量是否有值)
4.if [ -z str1 ];then fi ----当字符串的长度为0时返回真
root@sparkslave02:~/ShellLearning/Chapter13# vim if01.sh
str1="hello"
str2="hell"
#判断两字符串是否相等
if [ str1=str2 ]
then
echo "equal"
fi
#判断两字符串是否不等
if [ str1!=str2 ]
then
echo "not equal"
fi
#判断字符串长度是否大于0
if [ -n str1 ]
then
echo "the length of str1 is not zero"
fi
#判断字符串长度是否等于0
if [ -z str1 ]
then
echo "the length of str1 is not zero, it can't be executed"
fi
root@sparkslave02:~/ShellLearning/Chapter13# ./if01.sh
not equal
the length of str1 is not zero
(2)判断数字
1.int1 -eq int2 --相等
2.int1 -ne int2 --不相等
3.int1 -gt int2 --大于
4.int1 -ge int2 --大于等于
5.int1 -lt int2 --小于
6.int1 -le int2 --小于等于
使用示例:
root@sparkslave02:~/ShellLearning/Chapter13# vim if02.sh
i=2
j=3
if [ $i -lt $j ]
then
echo "i is less than j"
fi
if [ $j -gt $i ]
then
echo "j is great than i"
fi
root@sparkslave02:~/ShellLearning/Chapter13# chmod a+x if02.sh
root@sparkslave02:~/ShellLearning/Chapter13# ./if02.sh
i is less than j
j is great than i
(3)判断文件
文件判断常用命令如下:
1. -r file --用户可读为真
2. -w file --用户可写为真
3. -x file --用户可执行为真
4. -f file --文件存在且为正规文件为真
5. -d file --如果是存在目录为真
6. -c file --文件存在且为字符设备文件
7. -b file --文件存在且为块设备文件
8. -s file --文件大小为非0为真,可以判断文件是否为空
9. -e file --如果文件存在为真
使用示例:
root@sparkslave02:~/ShellLearning/Chapter13# vim if03.sh
root@sparkslave02:~/ShellLearning/Chapter13# chmod a+x if03.sh
#判断文件是否存在
if [ -f if03.sh ]
then
echo "if03.sh exists!!"
fi
#判断目录是否存在
if [ -d ../Chapter13 ]
then
echo "directory Chapter13 exists!!"
fi
root@sparkslave02:~/ShellLearning/Chapter13# ./if03.sh
if03.sh exists!!
directory Chapter13 exists!!
(4)逻辑判断
逻辑判断主要有下面三个命令
1. -a --与
2. -o --或
3. ! --非
使用示例:
root@sparkslave02:~/ShellLearning/Chapter13# vim if04.sh
#判断if04.sh文件与目录Chapter13是否同时存在,同时存在则为真
if [ -f if04.sh -a -d ../Chapter13 ]
then
echo "file if04.sh and directory Chapter13 both exists!!!"
fi
root@sparkslave02:~/ShellLearning/Chapter13# chmod a+x if04.sh
root@sparkslave02:~/ShellLearning/Chapter13# ./if04.sh
file if04.sh and directory Chapter13 both exists!!!
(5)if [] then else fi的用法
前面给出的例子都是if [] then fi的形式,这里再给出if [] then else fi的用法
root@sparkslave02:~/ShellLearning/Chapter13# vim if05.sh
i=7
if [ $i -lt 6 ]
then
echo "i is less than 6"
else
echo "i is great than or equal 6"
fi
root@sparkslave02:~/ShellLearning/Chapter13# chmod a+x if05.sh
root@sparkslave02:~/ShellLearning/Chapter13# ./if05.sh
i is great than or equal 6
(6)if [] then elif then else fi的用法
多种判断,看示例代码就能明白:
root@sparkslave02:~/ShellLearning/Chapter13# cp if05.sh if06.sh
root@sparkslave02:~/ShellLearning/Chapter13# vim if06.sh
i=7
if [ $i -le 6 ]
then
echo "i is less than 6"
elif [ $i -eq 7 ]
then
echo "i equal 7"
else
echo "i is great than 7"
fi
root@sparkslave02:~/ShellLearning/Chapter13# ./if06.sh
i equal 7
## 3. until循环控制结构##
语法格式:
until condition
do
command
done
使用示例:
root@sparkslave02:~/ShellLearning/Chapter13# vim until01.sh
i=0
until [ $i -gt 2 ]
do
let i+=1
echo "i=$i"
done
root@sparkslave02:~/ShellLearning/Chapter13# chmod a+x until01.sh
root@sparkslave02:~/ShellLearning/Chapter13# ./until01.sh
i=1
i=2
i=3