#!/usr/bin/env bash # 颜色定义 readonly RED='\033[0;31m' readonly GREEN='\033[0;32m' readonly YELLOW='\033[1;33m' readonly BLUE='\033[0;34m' readonly NC='\033[0m' # No Color # 带颜色的日志函数 log() { if [ $QUIET_MODE -eq 0 ]; then echo -e "${BLUE}[INFO]${NC} $*" fi } log_success() { if [ $QUIET_MODE -eq 0 ]; then echo -e "${GREEN}[SUCCESS]${NC} $*" fi } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $*" >&2 } log_error() { echo -e "${RED}[ERROR]${NC} $*" >&2 } # 执行命令并显示 echoAndRun() { local cmd="$1" hline echo echo -e "${BLUE}[执行命令]${NC} $cmd" echo eval "$cmd" local exit_code=$? echo if [ $exit_code -eq 0 ]; then log_success "命令执行成功" else log_warning "命令执行失败,退出码: $exit_code" fi return $exit_code } # 水平分隔线 hline() { printf '%0.s=' {1..60} echo } # 标准化标题格式 head_normal() { echo hline echo -e "${BLUE}$1${NC}" hline } # 检查命令是否存在 command_exists() { command -v "$1" >/dev/null 2>&1 } # 安全执行命令,遇到错误继续执行 safe_run() { local cmd="$1" local description="${2:-执行命令}" log "$description: $cmd" eval "$cmd" || { log_warning "$description 失败,继续执行..." return 1 } } # 进度指示器 progress() { local message="$1" if [ $QUIET_MODE -eq 0 ]; then echo -n -e "${BLUE}[...]${NC} $message" fi } progress_done() { if [ $QUIET_MODE -eq 0 ]; then echo -e "\r${GREEN}[✓]${NC} $1" fi } progress_failed() { echo -e "\r${RED}[✗]${NC} $1" >&2 } # 验证文件存在 check_file() { local file="$1" if [ ! -f "$file" ]; then log_error "文件不存在: $file" return 1 fi return 0 } # 验证目录存在 check_dir() { local dir="$1" if [ ! -d "$dir" ]; then log_error "目录不存在: $dir" return 1 fi return 0 }