案例:
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++))
doip="$network.$i"ping -c1 "$ip" > /dev/nullif [ $? -eq 0 ]; thenecho "$ip" >> "host_up.txt"elseecho "$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" in9[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
差