Shell 编程基础 - 流程控制
Shell 编程基础 - 流程控制
判断
字符串比较 | 说明 |
---|---|
-z | 是否为空 |
-n | 是否非空 |
= | == |
!= | != |
> | > |
< | < |
整数比较 | 说明 |
---|---|
-eq | == |
-gt | > |
-lt | < |
-ge | >= |
-le | <= |
-ne | != |
逻辑比较 | 说明 |
---|---|
! | ! |
-a | && |
-o | || |
if
if 是最简单的判断语句,可以针对测试结果做相应处理。
if expression; then
command
fi
例:根据输入的学生成绩打印对应的等级:大于等于80分的为A;大于等于60分、小于80分的为B、小于60分的为C。
[root@localhost shell]# cat eg.sh
#!/bin/bash
echo -n "请输入分数:"
read score
if [ $score -lt 60 ]; then
echo C
fi
if [ $score -ge 60 -a $score -lt 80 ]; then
echo B
fi
if [ $score -ge 80 ]; then
echo A
fi
[root@localhost shell]# bash eg.sh
请输入分数:95
A
[root@localhost shell]# bash eg.sh
请输入分数:75
B
[root@localhost shell]# bash eg.sh
请输入分数:45
C
if / else
if expression; then
command
else
command
fi
if / elif /else
if expression1; then
command
elif expression2; then
command
elif expression3; then
commmand
......
fi
修改之前的例
[root@localhost shell]# cat eg.sh
#!/bin/bash
echo -n "请输入分数:"
read score
if [ $score -lt 60 ]; then
echo C
elif [ $score -ge 60 -a $score -lt 80 ]; then
echo B
elif [ $score -ge 80 ]; then
echo A
fi
[root@localhost shell]# bash eg.sh
请输入分数:95
A
[root@localhost shell]# bash eg.sh
请输入分数:75
B
[root@localhost shell]# bash eg.sh
请输入分数:45
C
case
case VAR in
var1) command1 ;;
var2) command2 ;;
var3) command3 ;;
......
*) command ;;
esac
修改之前的例
[root@localhost shell]# cat eg.sh
#!/bin/bash
echo -n "请输入分数:"
read score
case $((score/10)) in
*[0-5]*) echo C ;;
*[67]*) echo B ;;
*) echo A ;;
esac
[root@localhost shell]# bash eg.sh
请输入分数:95
A
[root@localhost shell]# bash eg.sh
请输入分数:75
B
[root@localhost shell]# bash eg.sh
请输入分数:45
C
循环
Shell 中的循环主要有 for、while、until、select 几种。
for
for var in (list)
do
command
done
[root@localhost shell]# cat for.sh
#!/bin/bash
for var in A B C D E F G H U J K I M N
do
echo $var
done
[root@localhost shell]# bash for.sh
A
B
C
D
E
F
G
H
U
J
K
I
M
N
[root@localhost shell]# cat for.sh
#!/bin/bash
arr=(A B C D E F G H U J K I M N)
for var in ${arr[@]}
do
echo $var
done
[root@localhost shell]# bash for.sh
A
B
C
D
E
F
G
H
U
J
K
I
M
N
[root@localhost shell]# cat for.sh
#!/bin/bash
for var in {1..5}
do
echo $var
done
[root@localhost shell]# bash for.sh
1
2
3
4
5
[root@localhost shell]# cat for.sh
#!/bin/bash
sum=0
for var in $(seq 1 100)
do
sum=$((sum+var))
done
echo $sum
[root@localhost shell]# bash for.sh
5050
类 C for 循环
for ((expression1; expression2; expression3))
do
command
done
[root@localhost shell]# cat for.sh
#!/bin/bash
for ((i=0; i<10; i++))
do
echo -n $i
done
[root@localhost shell]# bash for.sh
0123456789
while
while expression
do
command
done
[root@localhost shell]# cat while.sh
#!/bin/bash
count=5
while [[ count -gt 0 ]]
do
echo $count
let count--
done
[root@localhost shell]# bash while.sh
5
4
3
2
1
until
until expression
do
command
done
直到条件为真,停止循环,与 while 相反。
select
select 是一种菜单扩展循环方式,其语法和带列表的for循环非常类似。
select MENU in (list)
do
command
done
[root@localhost shell]# cat select.sh
#!/bin/bash
echo "请选择想要安装的服务:"
select server in Apache Nginx Tomcat
do
break
done
echo "正在安装 $server ......"
[root@localhost shell]# bash select.sh
请选择想要安装的服务:
1) Apache
2) Nginx
3) Tomcat
#? 2
正在安装 Nginx ......
break、continue
- break 跳出循环
- continue 结束本次循环,开始下一次循环