流程控制之条件语句
if 条件判断
单分支结构
shell
if 条件测试;then
条件成立时执行的代码片段
fi
if 条件测试
then
条件成立时执行的代码片段
fi
- 例1.编写脚本choice1.sh,利用单分支结构实现输入2个整数,输出最大值
shell
read -p "请输入两个整数:" num1 num2
if ((num1>num2));then
echo $num1
exit 0
fi
echo $num2
双分支结构
shell
if 条件测试;then
条件测试成立时执行的代码片段
else
条件测试不成立时执行的代码片段
fi
多分支结构
shell
if 条件测试1
then
条件测试1成立时执行的代码片段
elif 条件测试2
then
条件测试2成立时执行的代码片段
else
条件测试不成立时执行的代码片段
fi
-
例2:面试题,编写脚本choice2.sh,判断系统内存剩余容量大小, 若低于100M则打印异常,若正常则打印正常
shell1 free_mem=`free | grep Mem | tr -s " " | cut -d " " -f 4` 2 if ((free_mem<100*1024)) 3 then 4 echo "内存不足!!:${free_mem}kb" 5 else 6 echo "内存正常:${free_mem}kb" 7 fi # free:表示显示内存即虚拟内存的信息 # grep Mem:表示过滤包含Mem所在行 # tr -s " ": 压缩空格为一个 # cut -d " " -f4 :以空格为间隔符,取其第4部分 -
例3:编写脚本choice3.sh,判断当前脚本的执行者,若不是root账户则提示并退出,若是root打印/etc/shadow文件内容
shelluser=`whoami` if [[ $user = root ]] then cat /etc/shadow else echo "you are not root" fi -
例4:编写脚本choice4.sh,实现闰年的判断
shell# 4年一闰 百年不润 ;判断非0结尾的年份是否为闰年 # 400年又润 ; 判断0结尾年份是否是闰年 read -p "请输入要判断的年份:" year if ((year % 400 == 0 || year % 4 == 0 && year % 100 != 0)) then echo "rain" else echo "not rain" fi -
例5:编写脚本choice5.sh,判断sshd是否运行
shell1 sshd_status=$(systemctl status sshd | grep Active | tr -s " " | cut -d " " -f 3) 2 if [[ $sshd_status = active ]] 3 then 4 echo "sshd is active" 5 else 6 echo sshd is inactive 7 fi -
例6:面试题,编写脚本choice6.sh,判断主机是否存活
shellread -p "请输入要判断的主机ip:" ip if ping -c2 ip;then echo "$ip is online." else echo "$ip is offline." fi -
例7:编写脚本choice7.sh,输入百分制成绩,对成绩定级
A级:85-100 B级:75-85 C级: 60-75 D级:60以下
shellread -p "请输入要定级的分数:" score if ((score >= 85));then echo "A" elif ((score >= 75));then echo "B" elif ((score >= 60));then echo "C" else echo "D" fi -
例8:编写脚本,根据用户输入的内容,判断是数字、字母、其它字符
shell1 read -p "请输入一个字符串:" str 2 if [[ $str =~ ^[0-9]+$ ]];then 3 echo "$str是一个纯数字字符串" 4 elif [[ $str =~ ^[a-zA-Z]+$ ]];then 5 echo "$str是一个纯字符字符串" 6 elif [[ $str =~ ^[^0-9a-zA-Z]+$ ]];then 7 echo "$str是一个非数字非英文字符的字符串" 8 else 9 echo "$str是一个混合字符串" 10 fi
case 条件
语法结构
shell
case 变量 in
值1) # 值可以是字面量,也可以是模式匹配(通配符、正则等)
执行成立时的代码片段
;;
值2)
执行成立时的代码片段
;;
值3)
执行成立时的代码片段
;;
*)
执行成立时的代码片段
;;
esac
例:根据输入的分数,通过case语句匹配分数区间,输出对应的等级(A/B/C/D)
shell
1 read -p "请输入要定级的分数:" score
2 case $score in
3 8[5-9]|9[0-9]|100)
4 echo "A"
5 ;;
6 7[5-9]|8[0-4])
7 echo "B"
8 ;;
9 6[0-9]|7[0-4])
10 echo "C"
11 ;;
12 *)
13 echo "D"
14 esac
流程控制之循环结构
for循环
-
使用for循环遍历某一个集合
shellfor 变量 in 集合 do 循环的代码片段 done -
不带参数的for循环
shell# 遍历脚本或者函数传入参数 for 变量 do 循环的代码片段 done # 相当于 for i in $@ 1 for i 2 do 3 echo $i 4 done -
类C的使用方式
shellfor(变量=n;循环条件;变量++;) do 循环的代码片段 done #例 1 for((i=0;i<100;i+=2)) 2 do 3 echo $i 4 done
while循环
shell
while 条件测试 # 条件成立时循环
do
循环代码片段
done
例:猜数字游戏
shell
1 rand=$RANDOM
2 count=0
3 while true
4 do
5 read -p "请输入你的猜测数字:" num
6 let count++
7 if ((num>rand));then
8 echo "大了"
9 elif ((num<rand));then
10 echo "小了"
11 else
12 echo "牛逼!猜对了,你总共猜了$count次"
13 break
14 fi
15 done
until循环
shell
until 条件测试 # 条件不成立时循环
do
循环代码片段
done
shell
# 剪刀石头布
# 1.ai随机出拳
# 2.用户通过输入数字代表猜拳 1.石头 2.剪刀 3.布
# 3.用户猜赢则游戏结束
2 until false
3 do
4 let ai=$RANDOM%4
5 read -p "请输入你的猜拳(1.石头 2.剪刀 3.布):" player
6 if ((player == 1 && ai == 2 || player == 2 && ai ==3
7 ||player == 3 && ai == 1));then
8 echo "牛逼"
9 exit 0
10 elif ((player == ai));then
11 echo "平局"
12 else
13 echo "彩笔"
14 fi
15 done
select循环
专门用于制作菜单的循环
shell
1 select item in 安装nginx 更新ssh 更新openssl 退出
2 do
3 if [[ $item = 安装nginx ]];then
4 dnf install nginx -y
5 elif [[ $item = 更新ssh ]];then
6 dnf update sshd -y
7 elif [[ $item = 更新openssl ]];then
8 dnf update openssl -y
9 elif [[ $item = "退出" ]];then
10 exit 0
11 else
12 echo "请输入正确的选项"
13 fi
14 done
嵌套循环
-
嵌套循环核心是 "外层控大逻辑,内层控细节";
-
外层循环每走一步,内层循环要走完所有步骤;
-
最常见的是
for嵌套for,也可以while嵌套while、for嵌套while。-
例1:打印九九乘法表
shell#!/bin/bash for i in `seq 9` do for j in `seq 9` do ((j<=i)) && echo -ne "$i*$j=`echo $((i*j))`\t" done echo done
-
例2:打印数字三角形
shell#!/bin/bash for ((i=1;i<10;i++)) # 打印9行 do for ((j=1;j<10-i;j++)) # 每一行打印一些空格 do echo -n " " done for ((k=1;k<=i;k++)) # 每一行打印一些数字 do echo -n "$i " done echo # 打印一个\n done
-