案例:
1. for ping测试指网段的主机
网段由用户输入,例如用户输入192.168.2 ,则ping 192.168.2.10 --- 192.168.2.20
UP: /tmp/host_up.txt
Down: /tmp/host_down.txt
2. 使用case实现成绩优良差的判断
1. for ping测试指网段的主机
网段由用户输入,例如用户输入192.168.244 ,则ping 192.168.244.130 --- 192.168.244.140
UP:host_up.txt
Down: host_down.txt
#!/bin/bash
read -p "请输入网段(192.168.244):" network
start=130
end=140
for ((i=start;i<=end;i++))
do
ip="$network.$i"
ping -c1 "$ip" > /dev/null
if [ $? -eq 0 ]; then
echo "$ip" >> "host_up.txt"
else
echo "$ip" >> "host_down.txt"
fi
done
echo "测试成功!"
[root@openEuler ~]# bash ping.sh
请输入网段(192.168.244):192.168.244
测试成功!
[root@openEuler ~]# cat host_up.txt
192.168.244.133
[root@openEuler ~]# cat host_down.txt
192.168.244.130
192.168.244.131
192.168.244.132
192.168.244.134
192.168.244.135
192.168.244.136
192.168.244.137
192.168.244.138
192.168.244.139
192.168.244.140
2. 使用case实现成绩优良差的判断
read -p "请输入成绩[0-100]:" score
case "$score" in
9[0-9]|100)
echo "优"
;;
8[0-9])
echo "良"
;;
6[0-9]|7[0-9])
echo "中"
;;
[0-5][0-9])
echo "差"
;;
*)
echo "input error"
esac
[root@openEuler ~]# bash score.sh
请输入成绩[0-100]:55
差