Created
August 29, 2025 06:20
-
-
Save moremorefun/ef4c9603c4dc6564a6cc0d24b77666ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Linux 服务器专用安装脚本 | |
| set -e # 出错时退出 | |
| echo "🚀 Linux 服务器安装 Oh My Zsh + Powerlevel10k" | |
| echo "================================================" | |
| # 检测发行版 | |
| if [ -f /etc/os-release ]; then | |
| . /etc/os-release | |
| OS=$NAME | |
| VERSION=$VERSION_ID | |
| else | |
| echo "⚠️ 无法检测操作系统版本" | |
| OS="Unknown" | |
| fi | |
| echo "📋 检测到系统: $OS" | |
| # 安装 zsh 和必要工具 | |
| install_dependencies() { | |
| echo "📦 安装依赖包..." | |
| if command -v apt-get &> /dev/null; then | |
| # Debian/Ubuntu | |
| if [ "$EUID" -eq 0 ]; then | |
| apt-get update | |
| apt-get install -y zsh git curl wget | |
| else | |
| sudo apt-get update | |
| sudo apt-get install -y zsh git curl wget | |
| fi | |
| elif command -v yum &> /dev/null; then | |
| # CentOS/RHEL 7 | |
| if [ "$EUID" -eq 0 ]; then | |
| yum install -y zsh git curl wget | |
| else | |
| sudo yum install -y zsh git curl wget | |
| fi | |
| elif command -v dnf &> /dev/null; then | |
| # CentOS/RHEL 8+, Fedora | |
| if [ "$EUID" -eq 0 ]; then | |
| dnf install -y zsh git curl wget | |
| else | |
| sudo dnf install -y zsh git curl wget | |
| fi | |
| elif command -v zypper &> /dev/null; then | |
| # openSUSE | |
| if [ "$EUID" -eq 0 ]; then | |
| zypper install -y zsh git curl wget | |
| else | |
| sudo zypper install -y zsh git curl wget | |
| fi | |
| elif command -v pacman &> /dev/null; then | |
| # Arch Linux | |
| if [ "$EUID" -eq 0 ]; then | |
| pacman -S --noconfirm zsh git curl wget | |
| else | |
| sudo pacman -S --noconfirm zsh git curl wget | |
| fi | |
| else | |
| echo "❌ 不支持的包管理器,请手动安装 zsh git curl wget" | |
| exit 1 | |
| fi | |
| } | |
| # 检查并安装依赖 | |
| for cmd in zsh git curl; do | |
| if ! command -v $cmd &> /dev/null; then | |
| install_dependencies | |
| break | |
| fi | |
| done | |
| # 安装 Oh My Zsh | |
| install_ohmyzsh() { | |
| if [ ! -d "$HOME/.oh-my-zsh" ]; then | |
| echo "🔧 安装 Oh My Zsh..." | |
| # 使用备用安装方法(适用于受限网络环境) | |
| if ! sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended 2>/dev/null; then | |
| echo "⚠️ 使用备用安装方法..." | |
| # 手动克隆和安装 | |
| git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh | |
| cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc | |
| fi | |
| else | |
| echo "✅ Oh My Zsh 已安装" | |
| fi | |
| } | |
| # 安装 Powerlevel10k | |
| install_p10k() { | |
| local p10k_dir="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k" | |
| if [ ! -d "$p10k_dir" ]; then | |
| echo "🎨 安装 Powerlevel10k..." | |
| # 尝试克隆,如果失败使用备用源 | |
| if ! git clone --depth=1 https://github.com/romkatv/powerlevel10k.git "$p10k_dir" 2>/dev/null; then | |
| echo "⚠️ 使用备用源..." | |
| git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git "$p10k_dir" || { | |
| echo "❌ 安装失败,请检查网络连接" | |
| exit 1 | |
| } | |
| fi | |
| else | |
| echo "✅ Powerlevel10k 已安装" | |
| fi | |
| } | |
| # 配置主题 | |
| configure_theme() { | |
| echo "⚙️ 配置主题..." | |
| # 备份原配置 | |
| if [ -f ~/.zshrc ]; then | |
| cp ~/.zshrc ~/.zshrc.backup.$(date +%Y%m%d_%H%M%S) | |
| fi | |
| # 设置主题 | |
| sed -i.tmp 's/ZSH_THEME=".*"/ZSH_THEME="powerlevel10k\/powerlevel10k"/' ~/.zshrc | |
| rm -f ~/.zshrc.tmp | |
| } | |
| # 服务器优化配置 | |
| optimize_for_server() { | |
| echo "🖥️ 应用服务器优化配置..." | |
| # 创建服务器专用的 p10k 配置 | |
| cat > ~/.p10k.zsh << 'EOF' | |
| # 服务器专用 Powerlevel10k 配置 | |
| 'builtin' 'local' '-a' 'p10k_config_opts' | |
| [[ -o 'aliases' ]] && p10k_config_opts+=('aliases') | |
| [[ -o 'sh_glob' ]] && p10k_config_opts+=('sh_glob') | |
| [[ -o 'no_brace_expand' ]] && p10k_config_opts+=('no_brace_expand') | |
| 'builtin' 'setopt' 'no_aliases' 'no_sh_glob' 'brace_expand' | |
| # 简化的左侧提示符 - 服务器优化 | |
| typeset -g POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=( | |
| context # user@hostname | |
| dir # 当前目录 | |
| vcs # git status | |
| newline # 换行 | |
| prompt_char # 提示符 | |
| ) | |
| # 右侧提示符 - 只显示必要信息 | |
| typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=( | |
| status # 上一个命令的退出状态 | |
| command_execution_time # 命令执行时间 | |
| background_jobs # 后台任务数 | |
| time # 时间戳 | |
| ) | |
| # 性能优化设置 | |
| typeset -g POWERLEVEL9K_INSTANT_PROMPT=verbose | |
| typeset -g POWERLEVEL9K_DISABLE_GITSTATUS=false | |
| typeset -g POWERLEVEL9K_VCS_MAX_INDEX_SIZE_DIRTY=1024 | |
| # 上下文设置 - 总是显示 user@host (服务器重要) | |
| typeset -g POWERLEVEL9K_CONTEXT_DEFAULT_FOREGROUND=180 | |
| typeset -g POWERLEVEL9K_CONTEXT_TEMPLATE='%n@%m' | |
| # 目录显示 | |
| typeset -g POWERLEVEL9K_SHORTEN_STRATEGY=truncate_to_unique | |
| typeset -g POWERLEVEL9K_SHORTEN_DELIMITER=.. | |
| # 命令执行时间 - 降低阈值到1秒 | |
| typeset -g POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD=1 | |
| # Git 状态优化 | |
| typeset -g POWERLEVEL9K_VCS_CLEAN_FOREGROUND=76 | |
| typeset -g POWERLEVEL9K_VCS_MODIFIED_FOREGROUND=178 | |
| typeset -g POWERLEVEL9K_VCS_UNTRACKED_FOREGROUND=39 | |
| # 禁用不必要的段 | |
| typeset -g POWERLEVEL9K_NODE_VERSION_SHOW_ON_COMMAND='' | |
| typeset -g POWERLEVEL9K_PYTHON_VERSION_SHOW_ON_COMMAND='' | |
| # 时间格式 | |
| typeset -g POWERLEVEL9K_TIME_FORMAT='%D{%H:%M:%S}' | |
| (( ! $#p10k_config_opts )) || setopt ${p10k_config_opts[@]} | |
| 'builtin' 'unset' 'p10k_config_opts' | |
| EOF | |
| # 添加 p10k 配置加载 | |
| if ! grep -q "source ~/.p10k.zsh" ~/.zshrc; then | |
| echo '[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh' >> ~/.zshrc | |
| fi | |
| } | |
| # 安装实用插件 | |
| install_plugins() { | |
| echo "🔌 安装常用插件..." | |
| local custom_dir="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}" | |
| # zsh-autosuggestions | |
| if [ ! -d "$custom_dir/plugins/zsh-autosuggestions" ]; then | |
| git clone https://github.com/zsh-users/zsh-autosuggestions "$custom_dir/plugins/zsh-autosuggestions" 2>/dev/null || \ | |
| git clone https://gitee.com/phpxxo/zsh-autosuggestions.git "$custom_dir/plugins/zsh-autosuggestions" | |
| fi | |
| # zsh-syntax-highlighting | |
| if [ ! -d "$custom_dir/plugins/zsh-syntax-highlighting" ]; then | |
| git clone https://github.com/zsh-users/zsh-syntax-highlighting.git "$custom_dir/plugins/zsh-syntax-highlighting" 2>/dev/null || \ | |
| git clone https://gitee.com/Annihilater/zsh-syntax-highlighting.git "$custom_dir/plugins/zsh-syntax-highlighting" | |
| fi | |
| # 更新插件配置 | |
| sed -i.tmp 's/plugins=(git)/plugins=(git zsh-autosuggestions zsh-syntax-highlighting extract z systemd)/' ~/.zshrc | |
| rm -f ~/.zshrc.tmp | |
| } | |
| # 设置 zsh 为默认 shell (如果可能) | |
| set_default_shell() { | |
| if [ "$SHELL" != "$(which zsh)" ]; then | |
| echo "🐚 设置 zsh 为默认 shell..." | |
| if [ -w /etc/passwd ] || command -v chsh &> /dev/null; then | |
| if command -v chsh &> /dev/null; then | |
| chsh -s $(which zsh) 2>/dev/null || { | |
| echo "⚠️ 无法更改默认 shell,请手动运行: chsh -s $(which zsh)" | |
| } | |
| fi | |
| else | |
| echo "⚠️ 无权限更改默认 shell,请联系管理员或手动运行: chsh -s $(which zsh)" | |
| fi | |
| fi | |
| } | |
| # 主安装流程 | |
| main() { | |
| echo "开始安装..." | |
| install_ohmyzsh | |
| install_p10k | |
| configure_theme | |
| optimize_for_server | |
| install_plugins | |
| set_default_shell | |
| echo "" | |
| echo "🎉 安装完成!" | |
| echo "==============" | |
| echo "📝 配置文件已备份" | |
| echo "🔧 已应用服务器优化配置" | |
| echo "🔌 已安装实用插件" | |
| echo "" | |
| echo "🚀 请运行以下命令之一:" | |
| echo " exec zsh # 在当前会话中启动 zsh" | |
| echo " source ~/.zshrc # 重新加载配置" | |
| echo " exit && ssh user@server # 重新登录" | |
| echo "" | |
| echo "⚙️ 如需重新配置外观,运行: p10k configure" | |
| } | |
| # 运行安装 | |
| main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment