linux服务器升级wget脚本

1.脚本流程

1.1判断系统类型

如果是centos提前安装centos需要依赖
yum install gnutls-devel -y
yum install openssl openssl-devel
yum install texinfo
如果是ubuntu提前安装ubuntu需要依赖(麒麟系统Kylin)
sudo apt-get install openssl openssl-devel
sudo apt-get install texinfo

1.2下载最新wget文件

wget http://ftp.gnu.org/gnu/wget/wget-latest.tar.gz
解压文件
tar -zxvf wget-latest.tar.gz
进入解压好的文件目录
检查编译配置文件

1.3编译安装文件

make && make install

1.4检查结果

wget --version

2.脚本内容 wgetupdate4.sh

#!/bin/bash

# 函数:运行shell命令并处理输出和错误
run_command() {
    local command="$1"
    echo "Running: $command"
    if ! eval "$command"; then
        echo "Error executing command: $command"
        exit 1
    fi
}

# 函数:根据系统类型安装必要的依赖项
install_dependencies() {
    if [ -f /etc/os-release ]; then
        . /etc/os-release
        OS=$ID
    elif [ -f /etc/redhat-release ]; then
        if grep -q "CentOS" /etc/redhat-release; then
            OS="centos"
        elif grep -q "Red Hat Enterprise Linux" /etc/redhat-release; then
            OS="rhel"
        fi
    else
        echo "Unsupported Linux distribution"
        exit 1
    fi

    case $OS in
        centos|rhel)
            echo "Detected CentOS/RHEL"
            run_command "yum install -y gnutls-devel openssl openssl-devel texinfo"
            ;;
        ubuntu|debian|kylin)
            echo "Detected Ubuntu/Debian/Kylin"
            run_command "sudo apt-get update"
            run_command "sudo apt-get install -y build-essential libgnutls28-dev libssl-dev texinfo"
            ;;
        *)
            echo "Unsupported Linux distribution"
            exit 1
            ;;
    esac
}

# 函数:下载并解压wget源码
download_and_extract_wget() {
    echo "Downloading latest wget source code"
    run_command "wget http://ftp.gnu.org/gnu/wget/wget-latest.tar.gz"
    echo "Extracting wget source code"
    run_command "tar -zxvf wget-latest.tar.gz"
    # 获取解压后的目录名
    EXTRACTED_DIR=$(find . -maxdepth 1 -type d -name 'wget-*' | head -n 1)
    cd "$EXTRACTED_DIR" || exit 1
}

# 函数:编译并安装wget
compile_and_install_wget() {
    echo "Configuring wget build"
    CONFIGURE_OUTPUT=$(./configure --prefix=/usr --sysconfdir=/etc --with-ssl=openssl 2>&1)
    if echo "$CONFIGURE_OUTPUT" | grep -q "no acceptable C compiler found in \$PATH"; then
        echo "C compiler not found. Please install a C compiler (e.g., gcc)."
        exit 1
    fi
    echo "Compiling and installing wget"
    run_command "make && make install"
}

# 函数:检查wget版本以确认安装成功
check_wget_version() {
    echo "Checking installed wget version"
    WGET_VERSION=$(wget --version | head -n 1)
    echo "$WGET_VERSION"
}

# 主程序
install_dependencies
download_and_extract_wget
compile_and_install_wget
check_wget_version

3.执行

chmod +x wgetupdate4.sh
./wgetupdate4.sh

4.错误处理

-bash: ./wgetupdate4.sh: /bin/bash^M: bad interpreter: No such file or directory
报错原因由于文件格式导致
法1:在linux上创建文件,复制脚本内容,并保存
法2:在linux中打开脚本文件,进入命令行修改格式并保存,如下
vim wgetupdate4.sh
:set fileformat=unix
:wq
posted @ 2024-11-21 17:19  Ben_JM  阅读(161)  评论(0)    收藏  举报