diff --git a/init_priv.sh b/init_priv.sh index c182afd..e23be00 100755 --- a/init_priv.sh +++ b/init_priv.sh @@ -27,32 +27,59 @@ configure_tuna() { } configure_upgrade() { - if confirm_action "要运行 apt upgrade 吗?" "N"; then - sudo apt update - sudo apt upgrade -y - sudo apt autoremove -y + if [ "$(uname)" = "Darwin" ]; then + # macOS 系统使用 brew + if confirm_action "要运行 brew upgrade 吗?" "N"; then + brew update + brew upgrade + brew cleanup + fi + else + # 其他系统使用 apt + if confirm_action "要运行 apt upgrade 吗?" "N"; then + sudo apt update + sudo apt upgrade -y + sudo apt autoremove -y + fi fi } configure_install_basic() { - [ "$1" != "noupdate" ] && sudo apt update - sudo apt install tmux git curl htop net-tools tar unzip -y + if [ "$(uname)" = "Darwin" ]; then + [ "$1" != "noupdate" ] && brew update + brew install tmux htop + else + [ "$1" != "noupdate" ] && sudo apt update + sudo apt install tmux git curl htop net-tools tar unzip -y + fi pip3 install trash-cli } configure_install_useful() { - [ "$1" != "noupdate" ] && sudo apt update - sudo apt install hexyl bat neofetch fd-find fzf tcptraceroute tree cloc -y + if [ "$(uname)" = "Darwin" ]; then + [ "$1" != "noupdate" ] && brew update + brew install hexyl bat neofetch tree cloc + else + [ "$1" != "noupdate" ] && sudo apt update + sudo apt install hexyl bat neofetch fd-find fzf tcptraceroute tree cloc -y + fi } configure_install_new() { - [ "$1" != "noupdate" ] && sudo apt update - sudo apt install pigz -y + if [ "$(uname)" = "Darwin" ]; then + [ "$1" != "noupdate" ] && brew update + brew install pigz + else + [ "$1" != "noupdate" ] && sudo apt update + sudo apt install pigz -y + fi pip3 install speedtest-cli } configure_install_eza() { - if [[ $(uname -m) != "x86_64" ]]; then + if [ "$(uname)" = "Darwin" ]; then + brew install eza + elif [[ $(uname -m) != "x86_64" ]]; then echo "eza is not supported on $(uname -m)." elif ! command_exists eza; then unzip $scriptdir/files/eza_x86_64-unknown-linux-musl.zip -d $tempdir/eza @@ -62,20 +89,31 @@ configure_install_eza() { } configure_install() { - if confirm_action "要运行 apt/pip3 install 吗?" "N"; then - sudo apt update - configure_install_basic noupdate - configure_install_useful noupdate - configure_install_new noupdate - configure_install_eza noupdate + if [ "$(uname)" = "Darwin" ]; then + # macOS 系统使用 brew + if confirm_action "要运行 brew/pip3 install 吗?" "N"; then + brew update + else + return + fi + else + if confirm_action "要运行 apt/pip3 install 吗?" "N"; then + sudo apt update + else + return + fi fi + configure_install_basic noupdate + configure_install_useful noupdate + configure_install_new noupdate + configure_install_eza noupdate } configure_nano() { [ -f ~/.nanorc ] && return if confirm_action "要配置 nano 吗?" "Y"; then - sudo apt install nano -y + pkg_install nano cp $scriptdir/files/.nanorc ~/.nanorc fi } @@ -84,16 +122,31 @@ configure_vim() { [ -f ~/.vimrc ] && return if confirm_action "要配置 vim 吗?" "Y"; then - sudo apt install vim -y + pkg_install vim cp $scriptdir/files/.vimrc ~/.vimrc fi } +replace_zsh_source() { + local zshrc="$HOME/.zshrc" + local old_source="\$ZSH/oh-my-zsh.sh" + local new_source="~/.oh-my-zsh/custom/00-init.zsh" + + # 使用 sed 进行替换,注意 macOS 和 Linux 的 sed 语法稍有不同 + if [ "$(uname)" = "Darwin" ]; then + # macOS 使用的是 BSD sed,需要给 -i 选项传递一个空字符串作为扩展名 + sed -i '' "s|${old_source}|${new_source}|g" "$zshrc" + else + # Linux 使用的是 GNU sed + sed -i "s|${old_source}|${new_source}|g" "$zshrc" + fi +} + configure_zsh() { [ -f ~/.zshrc ] && return if confirm_action "要配置 zsh 吗?" "Y"; then - sudo apt install zsh -y + pkg_install zsh rm ~/.oh-my-zsh -r git_clone ohmyzsh/ohmyzsh ~/.oh-my-zsh @@ -102,13 +155,11 @@ configure_zsh() { git_clone zsh-users/zsh-autosuggestions ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions git_clone zsh-users/zsh-syntax-highlighting ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting git_clone paulirish/git-open ~/.oh-my-zsh/custom/plugins/git-open --norecursive - sudo apt install autojump -y + pkg_install autojump if ! grep -Fxq "source ~/.oh-my-zsh/custom/00-init.zsh" ~/.zshrc; then - local line_number=$(grep -n "source \$ZSH/oh-my-zsh.sh" ~/.zshrc | cut -d : -f 1) - sed -i "${line_number}i source ~/.oh-my-zsh/custom/00-init.zsh\n" ~/.zshrc + replace_zsh_source fi - echo -e "\nsource ~/.zsh_user" | sudo tee -a ~/.zshrc >/dev/null cp $scriptdir/files/zsh/.zsh_user ~/.zsh_user cp $scriptdir/files/zsh/00-init.zsh ~/.oh-my-zsh/custom/ @@ -138,6 +189,7 @@ configure_tmux() { git_clone seebi/tmux-colors-solarized ~/.tmux/plugins/tmux-colors-solarized cp $scriptdir/files/.tmux.conf ~/.tmux.conf + [ "$(uname)" = "Darwin" ] && return sudo apt install xsel -y # 能够让 tmux 剪切板与 terminal 同步 fi } @@ -198,6 +250,7 @@ configure_nginx() { } configure_fail2ban() { + [ "$(uname)" = "Darwin" ] && return [ -f /etc/fail2ban/jail.local ] && return if confirm_action "要配置 fail2ban 吗?" "N"; then @@ -228,6 +281,7 @@ configure_hostname() { } configure_timedate() { + [ "$(uname)" = "Darwin" ] && return [[ $(timedatectl | grep "Time zone" | awk '{print $3}') == "Asia/Shanghai" ]] && return if confirm_action "要修改 时区 吗?" "N"; then @@ -236,6 +290,7 @@ configure_timedate() { } configure_bootinfo() { + [ "$(uname)" = "Darwin" ] && return [ -d /etc/update-motd.d/disable ] && return if confirm_action "要修改 启动信息 吗?" "Y"; then @@ -251,6 +306,7 @@ configure_bootinfo() { } configure_oomkiller() { + [ "$(uname)" = "Darwin" ] && return [ -f /etc/default/earlyoom ] && return if confirm_action "要启用 earlyoom 吗?" "Y"; then @@ -261,6 +317,7 @@ configure_oomkiller() { } configure_proxychains() { + [ "$(uname)" = "Darwin" ] && return [ -f /etc/proxychains4.conf ] && return if confirm_action "要配置 proxychains 吗?" "N"; then diff --git a/utils.sh b/utils.sh index b7a1d89..0dcbc55 100755 --- a/utils.sh +++ b/utils.sh @@ -47,6 +47,16 @@ command_exists() { command -v "$@" >/dev/null 2>&1; } +pkg_install() { + # 如果已经安装了,就直接返回(只检查第一个) + command_exists "$1" && return + if [ "$(uname)" = "Darwin" ]; then + brew install "$@" + else + sudo apt install "$@" -y + fi +} + # ============================================================ check_google_access