静态php下载脚本判断逻辑优化

JustForFun

这个脚本 https://www.workerman.net/install-php-and-composerApple M2/Darwin下的uname -muname -s如下图:

截图

183 2 0
2个回答

walkor 打赏

请直接发出优化后的install-php-and-composer结果吧, 我这没有设备测试

  • 围猎三锅 10天前

    静态php,在老版本的mac上不能用

  • JustForFun 10天前

    在下面,就在 case "$ARCH" 那里多加一个分支 arm64

  • JustForFun 10天前

    还发现了个问题,最后下载证书那一步,打开证书后发现证书内容 https://download.workerman.net/php/ca-certificates.crt 是下载记录。

    # 检查并下载 CA 证书
    if [ ! -f "/etc/ssl/certs/ca-certificates.crt" ]; then
        echo "下载证书..."
        sudo mkdir -p /etc/ssl/certs
        sudo curl -#o /etc/ssl/certs/ca-certificates.crt https://download.workerman.net/php/ca-certificates.crt
        if [ $? -eq 0 ]; then
            echo "CA 证书已下载并保存到 /etc/ssl/certs/ca-certificates.crt"
        else
            echo "下载 CA 证书失败"
            exit 1
        fi
    fi

    根据记录,真正的文件应该是 https://curl.se/ca/cacert-2024-09-24.pem 才对。

  • walkor 10天前

    感谢反馈, 已经修正

JustForFun
#!/bin/bash

# 检查文件是否存在并提示覆盖
check_and_prompt() {
    local file_path=$1
    local file_name=$2

    if [ -f "$file_path" ]; then
        read -p "$file_name 已存在。是否覆盖?(y/n): " choice
        if [ "$choice" != "y" ]; then
            echo "跳过 $file_name 安装。"
            return 1
        fi
    fi
    return 0
}

# 检查系统架构
ARCH=$(uname -m)
OS=$(uname -s)

case "$ARCH" in
    x86_64)
        ARCH_SUFFIX="x86_64"
        ;;
    aarch64)
        ARCH_SUFFIX="aarch64"
        ;;
    arm64)
        ARCH_SUFFIX="aarch64"
        ;;
    *)
        echo "不支持的架构: $ARCH"
        exit 1
        ;;
esac

case "$OS" in
    Linux)
        OS_SUFFIX="linux"
        ;;
    Darwin)
        OS_SUFFIX="mac"
        ;;
    *)
        echo "不支持的操作系统: $OS"
        exit 1
        ;;
esac

# 选择PHP版本
echo "请输入要安装的PHP版本 (8.0-8.3):"
read PHP_VERSION

# 验证版本号
if [[ ! "$PHP_VERSION" =~ ^8\.[0-3]$ ]]; then
    echo "无效的PHP版本: $PHP_VERSION"
    exit 1
fi

# 构造下载链接
PHP_FILENAME="php-$PHP_VERSION-$OS_SUFFIX-$ARCH_SUFFIX.tar.gz"
PHP_URL="https://download.workerman.net/php/$PHP_FILENAME"

# 检查并提示覆盖PHP
if check_and_prompt "/usr/local/bin/php" "PHP"; then
    # 下载并安装PHP
    curl -# $PHP_URL -o $PHP_FILENAME
    if [ $? -ne 0 ]; then
        echo "下载失败: $PHP_URL"
        exit 1
    fi

    tar -xf $PHP_FILENAME
    sudo mv ./php /usr/local/bin
fi

# 检查并提示覆盖Composer
if check_and_prompt "/usr/local/bin/composer" "Composer"; then
    # 下载并安装Composer
    echo "安装Composer..."
    curl -# -O https://download.workerman.net/php/composer.phar
    if [ $? -ne 0 ]; then
        echo "下载Composer失败"
        exit 1
    fi

    sudo mv composer.phar /usr/local/bin/composer
    sudo chmod a+x /usr/local/bin/composer
fi

# 检查并下载 CA 证书
if [ ! -f "/etc/ssl/certs/ca-certificates.crt" ]; then
    echo "下载证书..."
    sudo mkdir -p /etc/ssl/certs
    sudo curl -#o /etc/ssl/certs/ca-certificates.crt https://download.workerman.net/php/ca-certificates.crt
    if [ $? -eq 0 ]; then
        echo "CA 证书已下载并保存到 /etc/ssl/certs/ca-certificates.crt"
    else
        echo "下载 CA 证书失败"
        exit 1
    fi
fi

echo "安装完成。"
×
🔝