一、for循环
脚本练习1:
check_host.sh
用此脚本检测10台与您当前主机直连主机是否网络通畅
如果网络通畅请显示主机的ip列表
%脚本内容如下所示:
#!/bin/env bash
for ((ip=0;ip<10;ip++))
do
ping -c1 -w1 172.25.254.$ip &> /dev/null && {
echo 172.25.254.$ip
}
done
脚本练习2:
create_user.sh userfile passfile
userfile用的用户会被全部建立,密码对应为passfile文件中的内容
[root@test3 mnt]# cat userfile
user1
user2
user3
[root@test3 mnt]# cat passfile
user1pass
user2pass
user3pass
[root@test3 mnt]# cat createuser.sh
#!/bin/env bash
line_num=`sed -n '$=' userfile`
for line in `seq 1 $line_num`
do
username=`sed -n ${line}p $1`
password=`sed -n ${line}p $2`
id $username &> /dev/null && {
echo $username is exist!!
}||{
useradd $username
echo password | passwd --stdin $username &> /dev/null && echo $username is created!!
}
done
二、while
脚本练习:
check_file.sh
please input filename: file
file is not exist
file is file
file is direcory
此脚本会一直询问直到用户输入exit为止
%脚本内容如下所示:
#!/bin/env bash
while true
do
read -p "Please input filename: " filename
if [ "$filename" = "exit" -o "filename" = "EXIT" ]
then
echo bye
exit
elif [ -z "$filename" ]
then
echo ""
elif [ ! -e "$filename" ]
then
echo "$filename is not exist"
elif [ -f "$filename" ]
then
echo "$filename is file"
elif [ -d "$filename" ]
then
echo "$filename is dir"
else
echo "$filename is another file type"
fi
done
三、case
脚本练习:
system_watch.sh disk memory upload (每秒显示)
disk 监控磁盘使用情况
memory 监控内存使用情况
upload 监控启动负载
%脚本内容如下所示:
#!/bin/env bash
case $1 in
disk)
watch -n 1 df -h
;;
memory)
watch -n 1 free -m
;;
upload)
watch -n 1 uptime
;;
*)
echo error
esac
四、expect
脚本练习:
host_list.sh
检测172.25.254.1-172.25.254.10 网络是否开启
如果网络正常请生成解析列表host_list
格式如下:
ip 主机名称
例如: 172.25.254.1 为开启状态,主机名为westos_student1.westos.org
host_list中:
172.25.254.1 westos_student1.westos.org
%脚本内容如下所示:
#!/bin/env bash
AUTO_SSH()
{
/usr/bin/expect << EOF
spawn ssh root@$1 hostname
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$2\r" }
}
expect eof
EOF
}
for Host_Num in {1..10}
do
ping -c1 -w1 172.25.254.$Host_Num &> /dev/null
if [ "$?" = "0" ] %如果退出值是0,表示这条命令执行是正确的
then
Host_Name=`AUTO_SSH 172.25.254.$Host_Num westos | tail -n 1`
grep $Host_Name /mnt/hosts_list &> /dev/null || {
echo "172.25.254.$Host_Num $Host_Name" >>/mnt/host_list
}
fi
done
sed 's/^M//g' -i /mnt/hosts_list
五、break、continue、exit
contiue %终止当此次前循环提前进入下个循环
break %终止当前所在语句所有动作进行语句外的其他动作
exit %脚本退出
%编写脚本test.sh脚本来测试三者的差别:
[root@localhost mnt]# vim test.sh
#!/bin/env bash
for NUM in {1..10}
do
if [ "$NUM" = "4" ]
then
echo "luck number"
fi
echo $NUM
done
echo end
[root@localhost mnt]# sh test.sh
1
2
3
luck number
4
5
6
7
8
9
10
end
[root@localhost mnt]# vim test.sh %continue
#!/bin/env bash
for NUM in {1..10}
do
if [ "$NUM" = "4" ]
then
echo "luck number"
continue
fi
echo $NUM
done
echo end
[root@localhost mnt]# sh test.sh
1
2
3
luck number
5
6
7
8
9
10
end
[root@localhost mnt]# vim test.sh %break
#!/bin/env bash
for NUM in {1..10}
do
if [ "$NUM" = "4" ]
then
echo "luck number"
break
fi
echo $NUM
done
echo end
[root@localhost mnt]# sh test.sh
1
2
3
luck number
end
[root@localhost mnt]# vim test.sh %exit
#!/bin/env bash
for NUM in {1..10}
do
if [ "$NUM" = "4" ]
then
echo "luck number"
exit
fi
echo $NUM
done
echo end
[root@localhost mnt]# sh test.sh
1
2
3
luck number