BASH
Verify Website or API HTTP Status
Quickly check the HTTP status code of any URL using `curl`, useful for monitoring website uptime or API endpoint availability in web development.
#!/bin/bash
# Usage: ./check_status.sh <URL>
if [ -z "$1" ]; then
echo "Usage: $0 <URL>" >&2
exit 1
fi
TARGET_URL="$1"
echo "Checking HTTP status for: $TARGET_URL"
HTTP_STATUS=$(curl -o /dev/null -s -w "%{\http_code}\
" "$TARGET_URL")
echo "HTTP Status Code: $HTTP_STATUS"
if [ "$HTTP_STATUS" -ge 200 ] && [ "$HTTP_STATUS" -lt 300 ]; then
echo "Status: Success (2xx)"
exit 0
elif [ "$HTTP_STATUS" -ge 300 ] && [ "$HTTP_STATUS" -lt 400 ]; then
echo "Status: Redirection (3xx)"
exit 0
elif [ "$HTTP_STATUS" -ge 400 ] && [ "$HTTP_STATUS" -lt 500 ]; then
echo "Status: Client Error (4xx)" >&2
exit 1
elif [ "$HTTP_STATUS" -ge 500 ] && [ "$HTTP_STATUS" -lt 600 ]; then
echo "Status: Server Error (5xx)" >&2
exit 1
else
echo "Status: Unknown or Network Error (Code: $HTTP_STATUS)" >&2
exit 1
fi
How it works: This script takes a URL as an argument and uses `curl` to fetch only the HTTP status code without downloading the actual content. It then interprets this status code (e.g., 2xx for success, 4xx for client errors, 5xx for server errors) and prints a human-readable message, exiting with a non-zero status for errors. This is a simple yet powerful tool for quick health checks of web services and APIs.