1.for循环(遍历)
for 条件
do
动作
done
练习1:检测当前172.25.254网段主机的状态
[root@base1 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
[root@base1 mnt]# sh test.sh
练习2:写一个10秒的倒计时
[root@base1 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
[root@base1 mnt]# sh time.sh
练习3:写一个用户自定义分秒的倒计时
[root@base1 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
[root@base1 mnt]# sh time.sh
please input a time:1 5
2.while循环
while true 条件为真就执行
do
done
[root@base1 mnt]# uptime # 查看系统开启了多长时间
14:31:41 up 5:33, 2 users, load average: 0.00, 0.02, 0.05
[root@base1 mnt]# echo "student" | passwd --stdin student # 非交互修改用户密码
练习1:批量创建用户,并非交互式修改用户密码
[root@base1 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
[root@base1 mnt]# sh while.sh
3.嵌套循环
练习:打印9*9乘法表
[root@base1 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
[root@base1 mnt]# sh multiply.sh
4.if-else 循环
If 条件;then
动作1
elif
动作2
else
动作3
fi
[root@base1 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
[root@base1 mnt]# sh if_else_elif.sh
5.case 语句
case num in
条件1) #可以判断多条
命令1
;;
条件2)
命令2
esac
练习:编写一个脚本,实现如下功能
[root@base1 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: '{ print "系统已经运行了" $1 "小时" $2 "分钟"}'`"
29 ;;
30 Q|q)
31 exit 0
32 ;;
33 *)
34 echo -e "\033[31m \nError \033[0m";;
35 esac
36 done
[root@base1 mnt]# sh case.sh
练习1:编写httpd监控脚本,要求可以输入start|stop|restart|status
[root@base1 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 [ $? -eq 0 ] && echo 您已成功开启httpd
26 ;;
27 status)
28 echo -e "\033[32m `systemctl status httpd | awk ' NR==3 {print $2}'`\033[0m"
29 ;;
30 stop)
31 systemctl stop httpd
32 echo 您已成功关闭httpd
33 ;;
34 restart)
35 systemctl restart httpd
36 [ $? -eq 0 ] && echo 您已重启成功
37 ;;
8 q|Q)
39 exit 0
40 ;;
41 *)
42 echo -e "\033[31m \nError \033[0m"
43 ;;
44 esac
44 done
[root@base1 mnt]# sh httpd.sh
练习2:写一个脚本,建立tom,harry,natasha三个数据库,并备份数据库,再在数据库中建立表,并打印
[root@base1 mnt]# vim db_mysql.sh
10 #!/bin/bash
11 MYUSER=root # 定义用户变量
12 MYPASS=westos # 定义密码变量
13 MYCMD="mysql -u$MYUSER -p$MYPASS" # 定义登录命令
14 MYDUMP="mysqldump -u$MYUSER -p$MYPASS" # 定义备份数据库命令
15 DBPATH=/root/Desktop/ # 定义备份文件
16 # 创建数据库
17 for dbname in tom harry natasha # 创建这三个数据库
18 do
19 $MYCMD -e "create database $dbname" &> /dev/null
20 done
21 if [ $? -eq 0 ];then
22 echo -e "\033[32m the database is creating \033[0m"
23 else
24 echo -e "\033[31m the database is already created \033[0m"
25 fi
26 # 备份数据库
27 [ ! -d "$DBPATH" ] && mkdir -p $DBPATH # 如果存放备份数据的文件不存在,先建立文件
28 for dbname in `mysql -u root -pwestos -e "show databases;" | sed '1,2d' | egrep -v "mysql|schema|ultrax"` # 截取自己建立的数据 库
29 do
30 $MYDUMP $dbname | gzip > $DBPATH/${dbname}_$(date +%F).sql.gz # 把备份的数据库文件压缩
31 done
32 # 创建表
33 for dbname in tom harry natasha
34 do
35 $MYCMD -e "use $dbname;create table test(id int,name varchar(20));insert into test values(1,'westos');" &> /dev/null
36 done
37 if [ $? -eq 0 ];then
38 echo -e "\033[32m the table is creating \033[0m"
39 else
40 echo -e "\033[31m the table is already created \033[0m "
41 fi
42
43 # 显示表内容
44 for dbname in tom harry natasha
45 do
46 echo +++++${dbname}.test+++++
47 $MYCMD -e "use $dbname;select * from ${dbname}.test;"
48 done
[root@base1 mnt]# sh db_mysql.sh # 第一次运行脚本,数据库还没有建立
[root@base1 mnt]# sh db_mysql.sh # 建立好数据库之后,再次运行
练习3:数据库备份,执行script.sh $dbpasswd 备份数据库中所有库到/mnt/mysqldump目录中,备份文件名称为“库名称.sql”,当此文件存在时进入交互模式,询问动作,输入“s”跳过备份,输入“b”,备份“库名称.sql”为“库名称_backup.sql”,输入“O”时,覆盖原文件,e表示退出
[root@base1 mnt]# vim db_mysql_02.sh
#!/bin/bash
mkdir -p /mnt/mysqldump # 先建立存放备份数据库文件的目录
DATABASE=`mysql -uroot -pwestos -e "show databases;" | sed '1,2d' | egrep -v "mysql|schema|ultrax"` # 截取数 据库名称
for dbname in $DATABASE # 遍历数据库
do
if [ -e /mnt/mysqldump/${dbname}.sql ];then # 如果数据库已经备份过了,就执行以下命令
read -p "${dbname} has been dumped!
[S]kip [B]ackup [O]verwrite [E]xit
please input the action:" ACTION
case $ACTION in
s|S)
;;
b|B)
mysqldump -uroot -p$1 $dbname > /mnt/mysqldump/${dbname}_backup.sql # 再次备份数据库
;;
o|O)
mysqldump -uroot -p$1 $dbname > /mnt/mysqldump/${dbname}.sql # 覆盖已经备份好的数据库
;;
e|E)
echo -e "Bye~~"
exit 0
esac
else # 如果数据库还没有备份,就备份数据库
mysqldump -uroot -p$1 $dbname > /mnt/mysqldump/${dbname}.sql # 备份数据库
echo -e "$dbname is backuped!"
fi
done
[root@base1 mnt]# sh db_mysql_02.sh westos # 第一次执行命令,备份数据库
harry is backuped!
natasha is backuped!
tom is backuped!
[root@base1 mnt]# sh db_mysql_02.sh westos # 当数据库备份好之后,再次执行命令,进入交互模式