使用场景:检查网络连通性、服务运行状态等。
#!/bin/bash
# 检查远程服务器的连通性
remote_host="example.com"
if ping -c 1 "$remote_host" > /dev/null; then
echo "远程服务器可达。"
else
echo "远程服务器不可达。"
fi
# 检查HTTP服务的状态
http_status=$(curl -s -o /dev/null -w "%{http_code}" http://example.com)
if [ "$http_status" -eq 200 ]; then
echo "HTTP服务正常。"
else
echo "HTTP服务异常,状态码:$http_status。"
fi