简介
除了上一章学习的if条件语句之外,作为一种脚本编程言,Shell 同样包含循环、分支等其他程序控制结构,从而能够轻松完成更加复杂、强大的功能。
本章将学习for. while. case 语句的具体应用。
使用for循环
1 .创建取值列表
[root@centos01 ~]# vim username.txt
bob
tom
alice
2 .编写for循环脚本
[root@centos01 ~]# vim useradd.sh
#!/bin/bash
user=$(cat ./username.txt)
for username in $user
do
useradd $username
echo "pwd@123" | passwd --stdin $username &> /dev/null
done
3.给useradd.sh权限
[root@centos01 ~]# chmod +x useradd.sh
4.执行
[root@centos01 ~]# ./useradd.sh
5.结果
[root@centos01 ~]# tail -3 /etc/passwd
使用for循环删除指定用户
1.创建deluser.sh
[root@centos01 ~]# vim deluser.sh
#!/bin/bash
user=$(cat ./username.txt)
for username in $user
do
userdel -r $username
done
2.查询结果
[root@centos01 ~]# tail -3 /etc/passwd
使用for循环测试网络连通性
1.创建pinghost.sh
[root@centos01 ~]# vim pinghost.sh
#!/bin/bash
read -p "请输入指定的IP地址或主机名:" host
for ip in $host
do
ping -c 3 -i 0.3 -w 3 $ip &> /dev/null
if [ $? -eq 0 ]
then
echo "hest $ip 主机开机!!"
else
echo "host $ip 主机关机!!"
fi
done
3.给pinghost.sh权限
[root@centos01 ~]# chmod +x pinghost.sh
4.执行结果
[root@centos01 ~]# ./pinghost.sh
使用while循环语句
for循环语句非常适用于列表对象无规律,且列表来源已固定(如某个列表文件)的场合。而对于要求控制循环次数、操作对象按数字顺序编号、按特定条件执行重复操作等情况,则更适合使用.另外一种循环一while 语句。
while循环创建用户名字以stu开头数字随机
1.创建whileadduser.sh
[root@centos01 ~]# vim whileadduser.sh
#!/bin/bash
prefix=stu
i=1
while [ $i -le 20 ]
do
useradd ${prefix}$i
echo "pwd@123" | passwd --stdin ${prefix}$i &> /dev/null
let i++
done
2.给whileadduser.sh权限
[root@centos01 ~]# chmod +x whileadduser.sh
3.执行
[root@centos01 ~]# ./whileadduser.sh
4.结果
[root@centos01 ~]# tail -20 /etc/passwd
while循环删除用户
1.创建whiledeluser.sh
[root@centos01 ~]# vim whiledeluser.sh
#!/bin/bash
prefix=stu
i=1
while [ $i -le 20 ]
do
userdel -r ${prefix}$i
let i++
done
2.执行
[root@centos01 ~]# ./whiledeluser.sh
3.执行
[root@centos01 ~]# tail -20 /etc/passwd
使用case分支语句
上一章学习多分支的if语句时,曾经提到过改用case 语句可以使脚本程序的结构更加清晰.层次分明,本节就来学习case语句的语法结构及应用。
case语法控制vsftpd服务
1.编写控制vsftpd.sh脚本
[root@centos01 ~]# vim vsftpd.sh
#!/bin/bash
#chkconfig:35 21 80
#description:vsftpd server
case "$1" in
start)
echo "正在启动vsftpd服务[确认]"
;;
stop)
echo "正在停止xsftpd服务[确认]"
;;
restart)
echo "正在重新启动xsftpd服务[确认]"
;;
*)
echo "使用语法:$0{start|restart|stop}"
esac
2.给vsftpd.sh权限
[root@centos01 ~]# chmod +x vsftpd.sh
3.执行结果
[root@centos01 ~]# ./vsftpd.sh start
for、 while、 case的区别
1)for
根据列表内容进行循环遍历数据使用
2 )while
根据初始化值随机循环使用
3)case
写服务控制脚本使用
本章实验
1.编辑getarp脚本文件
[root@centos01 ~]# vim getarp.sh
#!/bin/base
et=/etc/ether
et2=${et}.bak
ip1=ip:192.168.100.10
mac1=mac:00:0c:29:7f:97:40
ip2=ip:192.168.100.20
mac2=mac:00:0c:29:a8:fd:96
ip3=ip:192.168.100.30
mac3=mac:00:0c:29:6f:40:9a
i=1
a=([ -e $et ])
while [ $? -ge 0 ] && [ $i -le 2 ]
do
if [ $i -eq 1 ]
then
mv $et $et2 &> /dev/null
elif [ $i -eq 2 ]
then
touch $et
echo "$ip1" >> $et "$mac1" >> $et
echo "$ip2" >> $et "$mac2" >> $et
echo "$ip3" >> $et "$mac3" >> $et
else
echo " 脚本出错请联系作者---> 2107590002qq.com "
fi
let i++
done
2.给getarp.sh权限
[root@centos01 ~]# chmod +x getarp.sh
3.执行结果
[root@centos01 ~]# sh getarp.sh
[root@centos01 ~]# cat /etc/ether
ip:192.168.100.10 mac:00:0c:29:7f:97:40
ip:192.168.100.20 mac:00:0c:29:a8:fd:96
ip:192.168.100.30 mac:00:0c:29:6f:40:9a
4.安装ftp
[root@centos01 ~]# rm -rf /etc/yum.repos.d/CentOS-*
[root@centos01 ~]# mount /dev/cdrom /mnt/
[root@centos01 ~]# yum -y install vsftpd ftp
5.启动服务设置开机自动启动
[root@centos01 ~]# systemctl start vsftpd
[root@centos01 ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
6.编辑scanhost脚本文件
[root@centos01 ~]# vim scanhost.sh
#!/bin/bash
a=$(cat /etc/ether | awk -F: '{print $2}' | awk '{print $1}' )
for ip in $a
do
wget ftp://$ip &> /dev/null
if [ $? -eq 0 ]
then
echo "主机:$ip开启匿名FTP"
else
echo "$ip:请安装FTP,安装完成以后再来查询"
fi
done
7.给scanhost.sh权限
[root@centos01 ~]# chmod +x scanhost.sh
8.执行结果
[root@centos01 ~]# sh scanhost.sh
主机:192.168.100.10开启匿名FTP
主机:192.168.100.20请安装FTP,安装完成以后再来查询
主机:192.168.100.30请安装FTP,安装完成以后再来查询