1.for循环(遍历)
for 条件
do
动作
done
练习1:检测当前172.25.254网段主机的状态
[[email protected] mnt]# vim test.sh
1 #!/bin/bash
2 for ((IP=1;IP<=10;IP++))
3 do {
4 ping -c1 -w1 172.25.254.$IP &> /dev/null && echo "172.25.254.$IP is up" || echo "172.25.254.$IP is down"
5 }
6 done

[[email protected] mnt]# sh test.sh

练习2:写一个10秒的倒计时
[[email protected] mnt]# vim time.sh
10 #!/bin/bash
11 for((i=10;i>=0;i--))
12 do {
13 echo -n "time $i "
14 echo -ne "\r"
15 sleep 1
16 }
17 done

[[email protected] mnt]# sh time.sh

练习3:写一个用户自定义分秒的倒计时
[[email protected] mnt]# vim time.sh
10 #!/bin/bash
11 read -p "please input a time:" a b
12
13 for((i=$a*60+$b;i>=0;i--))
14 do {
15 m=$[$i/60]
16 n=$[$i%60]
17 echo -n "time $m:$n "
18 echo -ne "\r"
19 sleep 1
20 }
21 done

[[email protected] mnt]# sh time.sh
please input a time:1 5

2.while循环
while true 条件为真就执行
do
done
[[email protected] mnt]# uptime # 查看系统开启了多长时间
14:31:41 up 5:33, 2 users, load average: 0.00, 0.02, 0.05
[[email protected] mnt]# echo "student" | passwd --stdin student # 非交互修改用户密码
练习1:批量创建用户,并非交互式修改用户密码
[[email protected] mnt]# vim while.sh
10 #!/bin/bash
11 PREFIX="LALA"
12 i=1
13 while [ $i -le 3]
14 do
15 useradd ${PREFIX}$i
16 echo "123" | passwd --stdin ${PREFIX}$i &> /dev/null
17 ((i++))
18 done

[[email protected] mnt]# sh while.sh

3.嵌套循环
练习:打印9*9乘法表
[[email protected] mnt]# vim multiply.sh
10 #!/bin/bash
11 for((i=1;i<=9;i++))
12 do
13 for((j=1;j<=i;j++))
14 do
15 t=$[i*j]
16 echo -ne $j*$i=$t "\t"
17 done
18 echo -e "\n"
19 done

[[email protected] mnt]# sh multiply.sh

4.if-else 循环
If 条件;then
动作1
elif
动作2
else
动作3
fi
[[email protected] mnt]# vim if_else_elif.sh
10 #!/bin/bash
11 read -p "please input your score:" s
12 if [ "$s" -lt "60" ];then
13 echo you are not pass!!
14 elif [ "$s" -ge "60" -a "$s" -lt "80" ];then
15 echo you are good!!
16 elif [ "$s" -ge "80" ];then
17 echo you are best!!
18 else
19 echo you are not have score!!
20 fi

[[email protected] mnt]# sh if_else_elif.sh

5.case 语句
case num in
条件1) #可以判断多条
命令1
;;
条件2)
命令2
esac
练习:编写一个脚本,实现如下功能
[[email protected] mnt]# vim case.sh
10 #!/bin/bash
11 echo -e "
12 \033[036m A 显示主机IP \033[0m
13 \033[032m B 显示磁盘剩余空间 \033[0m
14 \033[033m C 显示系统运行时间 \033[0m
15 \033[034m Q 退出 \033[0m
16 "
17 for ((i=1;;i++))
18 do
19 read -p "please input a letter:" a
20 case $a in
21 a|A)
22 echo -e "\n `ifconfig eth0 | grep "inet "| cut -d " " -f 10`"
23 ;;
24 B|b)
25 echo -e "\n `df | awk -F " " '{if(NR==2){ print "系统剩余空间为:"$4}}'` "
26 ;;
27 C|c)
28 echo -e "\n `uptime | awk '{print $3}'|awk -F, '{print $1}'|awk -F: '{ p rint "系统已经运行了" $1 "小时" $2 "分钟"}'`"
29 ;;
30 Q|q)
31 exit 0
32 ;;
33 *)
34 echo -e "\033[31m \nError \033[0m";;
35 esac
36 done

[[email protected] mnt]# sh case.sh

练习:编写httpd监控脚本,要求可以输入start|stop|restart|status
[[email protected] mnt]# vim httpd.sh
10 #!/bin/bash
11 echo -e "
12 \033[036m start 开启httpd \033[0m
13 \033[032m status 查看httpd的状态 \033[0m
14 \033[033m stop 关闭httpd \033[0m
15 \033[034m restart 重启httpd \033[0m
16 \033[035m quit 退出 \033[0m
17
18 "
19 for ((i=1;;i++))
20 do
21 read -p "please input a letter:" a
22 case $a in
23 start)
24 systemctl start httpd
25 echo 您已成功开启httpd
26 ;;
27 status)
28 systemctl status httpd
29 ;;
30 stop)
31 systemctl stop httpd
32 echo 您已成功关闭httpd
33 ;;
34 restart)
35 systemctl restart httpd
36 echo 您已重启成功
37 ;;
38 q|Q)
39 exit 0
40 ;;
41 *)
42 echo -e "\033[31m \nError \033[0m";;
43 esac
44 done

[[email protected] mnt]# sh httpd.sh
