高效工作-自动化脚本-bug记录

120 阅读1分钟

前文

写了一篇自动化脚本相关的问题 juejin.cn/post/726492…

这篇文章会把遇到的问题和解决方案记录下来!

问题记录

brew脚本问题

源代码

# homebrew安装脚本
install_homebrew() {
    # 下载 Homebrew 安装脚本
    # curl -v https://raw.githubusercontent.com/Homebrew/install/master/install.sh > install-brew.sh
    curl -v https://logan-download.oss-cn-hangzhou.aliyuncs.com/homebrew_install.sh > install-brew.sh
    

    # 修改 BREW_REPO 为国内镜像站
    sed -i '' 's|https://github.com/Homebrew/brew|https://mirrors.ustc.edu.cn/brew.git|g' install-brew.sh

    # 设置 HOMEBREW_CORE_GIT_REMOTE 环境变量为国内镜像站
    export HOMEBREW_CORE_GIT_REMOTE=https://mirrors.ustc.edu.cn/homebrew-core.git

    # 运行安装脚本
    bash install-brew.sh

    # 删除安装脚本
    rm install-brew.sh
}

# 检查homebrew是否已经安装,如果未安装,则执行安装脚本
if ! command -v brew &> /dev/null; then
    echo "Homebrew not found. Installing..."
    install_homebrew
    echo "Homebrew is installed."
else
    echo "Homebrew is already installed."
fi

# 替换brew 源为中科大的数据源
# 替换 brew.git
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

# 替换 homebrew-core.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git

# 使用brew安装基础的终端能力
check_and_install_dependencies() {
    dependency=$1
    if ! command -v $dependency &> /dev/null; then
        echo "$dependency is not installed, installing..."
        brew install $dependency
    else
        echo "$dependency is already installed"
    fi
}

check_and_install_dependencies wget
check_and_install_dependencies dpkg

执行报错信息

Homebrew is installed.

test.sh: line 30: brew: command not found

fatal: not a git repository (or any of the parent directories): .git

test.sh: line 34: brew: command not found

test.sh: line 34: cd: /Library/Taps/homebrew/homebrew-core: No such file or directory

fatal: not a git repository (or any of the parent directories): .git

wget is not installed, installing...

test.sh: line 42: brew: command not found

dpkg is not installed, installing...

test.sh: line 42: brew: command not found

问题判断

todo

修改代码


# homebrew安装脚本
install_homebrew() {
    # 下载 Homebrew 安装脚本
    curl -v https://logan-download.oss-cn-hangzhou.aliyuncs.com/homebrew_install.sh > install-brew.sh

    # 修改 BREW_REPO 为国内镜像站
    sed -i '' 's|https://github.com/Homebrew/brew|https://mirrors.ustc.edu.cn/brew.git|g' install-brew.sh

    # 设置 HOMEBREW_CORE_GIT_REMOTE 环境变量为国内镜像站
    export HOMEBREW_CORE_GIT_REMOTE=https://mirrors.ustc.edu.cn/homebrew-core.git

    # 运行安装脚本
    bash install-brew.sh

    # 删除安装脚本
    rm install-brew.sh
}

# 检查homebrew是否已经安装,如果未安装,则执行安装脚本
if ! command -v brew &> /dev/null; then
    echo "Homebrew not found. Installing..."
    install_homebrew
    echo "Homebrew is installed."
else
    echo "Homebrew is already installed."
fi

# 安装环境变量
echo "准备安装Homebrew对应的环境变量"
echo 'eval $(/opt/homebrew/bin/brew shellenv)' >> ~/.zshrc
eval $(/opt/homebrew/bin/brew shellenv)
source ~/.zshrc
echo "环境变量安装成功!"

# 替换brew 源为中科大的数据源
# 替换 brew.git
echo "开始替换中科大数据源..."
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git


cd "$(brew --repo)/Library/"
mkdir Taps && cd Taps
mkdir homebrew && cd homebrew
git clone git://mirrors.ustc.edu.cn/homebrew-core.git

cd "$(brew --repo)/Library/Taps/" 
cd homebrew
git clone https://mirrors.ustc.edu.cn/homebrew-cask.git


# 替换 homebrew-core.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
echo "替换成功...."

# 使用brew安装基础的终端能力
check_and_install_dependencies() {
    dependency=$1
    if ! command -v $dependency &> /dev/null; then
        echo "$dependency is not installed, installing..."
        brew install $dependency
    else
        echo "$dependency is already installed"
    fi
}

echo "开始安装wget..."
check_and_install_dependencies wget
echo "安装成功..."
echo "开始安装dpkg..."
check_and_install_dependencies dpkg
echo "安装成功..."

todo