1:使用for循环结构
一.for循环的基本用法
脚本1,通过循环读取账户文件user.txt,批量创建账户: [root@2015idc ~]# vim for01.sh #!/bin/bash for i in $(cat root/user.txt) do useradd $i echo "123456" | passwd --stdin $i done [root@2015idc ~]# chmod +x for01.sh
二.批量检测多个主机的存活状态
[root@2015idc ~]# vim chkhost.sh
#!/bin/bash
for IP in {1..254}
do
ping -c 3 -i 0.2 -w 3 192.168.4.$IP &> /dev/null
if [ $? -eq 0 ] ; then
echo "Host 192.168.4.$IP is up."
else
echo "Host 192.168.4.$IP is down."
fi
done
[root@2015idc ~]# chmod +x chkhost.sh
测试,验证脚本 ... ... [root@2015idc ~]# ./chkhost.sh Host 192.168.4.1 is up. Host 192.168.4.2 is down. Host 192.168.4.3 is down. ... ...