#!/bin/bash
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
check_curl_installed() {
if command -v curl &> /dev/null; then
return 0
else
return 1
fi
}
check_and_install_homebrew() {
if ! command -v brew &> /dev/null; then
echo -e "${YELLOW}Homebrew not installed, starting Homebrew installation...${NC}"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
if [ -f "/opt/homebrew/bin/brew" ]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [ -f "/usr/local/bin/brew" ]; then
eval "$(/usr/local/bin/brew shellenv)"
fi
fi
}
install_curl() {
echo -e "${YELLOW}Starting curl installation via Homebrew...${NC}"
check_and_install_homebrew
brew install curl
}
verify_curl_after_install() {
if check_curl_installed; then
CURL_VERSION=$(curl --version | head -n1)
echo -e "${GREEN}✅ curl installation successful! Version: ${CURL_VERSION}${NC}"
echo -e "${YELLOW}Testing curl functionality (accessing Baidu homepage)...${NC}"
if curl -s -o /dev/null -w "%{http_code}" https://www.baidu.com | grep -q "200"; then
echo -e "${GREEN}✅ curl functionality test passed!${NC}"
else
echo -e "${YELLOW}⚠️ curl is installed but access test failed (possible network issue)${NC}"
fi
else
echo -e "${RED}❌ curl installation failed, please check manually!${NC}"
exit 1
fi
}
if check_curl_installed; then
CURL_VERSION=$(curl --version | head -n1)
echo -e "${GREEN}curl is already installed, version: ${CURL_VERSION}${NC}"
if ! curl -s -o /dev/null -w "%{http_code}" https://www.baidu.com | grep -q "200" 2>/dev/null; then
echo -e "${YELLOW}⚠️ curl functionality test failed (possible network issue)${NC}"
fi
else
echo -e "${YELLOW}curl is not installed, starting installation...${NC}"
install_curl
verify_curl_after_install
fi
exit 0