-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh.sh
163 lines (142 loc) · 5.05 KB
/
zsh.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
# --------------------- 包管理器检测 ---------------------
PM=""
SUDO="sudo"
[ "$(id -u)" -eq 0 ] && SUDO="" # root用户不需要sudo
# 检测包管理器优先级: apt-get -> dnf -> yum
if command -v apt-get &>/dev/null; then
PM="apt-get"
elif command -v dnf &>/dev/null; then
PM="dnf"
elif command -v yum &>/dev/null; then
PM="yum"
else
echo -e "${RED}错误:未找到支持的包管理器 (apt-get/dnf/yum)${NC}"
exit 1
fi
# --------------------- 依赖检测函数 ---------------------
is_installed() {
local pkg="$1"
case $PM in
apt-get)
dpkg -s "$pkg" &>/dev/null
;;
dnf|yum)
rpm -q "$pkg" &>/dev/null
;;
*)
echo -e "${RED}错误:不支持的包管理器${NC}"
return 1
;;
esac
}
# --------------------- 基础依赖安装 ---------------------
echo -e "${YELLOW}检查系统依赖...${NC}"
for pkg in zsh curl git; do
if is_installed "$pkg"; then
echo -e "${GREEN}✓ ${pkg} 已安装${NC}"
else
echo -e "${YELLOW}正在安装 ${pkg}...${NC}"
$SUDO $PM install -y "$pkg"
fi
done
# --------------------- autojump智能安装 ---------------------
install_autojump() {
local pkg_name="autojump"
# 特殊处理RHEL系
if [[ "$PM" == "dnf" || "$PM" == "yum" ]]; then
if ! is_installed "epel-release"; then
echo -e "${YELLOW}安装EPEL源...${NC}"
$SUDO $PM install -y epel-release
fi
pkg_name="autojump-zsh"
fi
if is_installed "$pkg_name"; then
echo -e "${GREEN}✓ ${pkg_name} 已安装${NC}"
else
echo -e "${YELLOW}正在安装 ${pkg_name}...${NC}"
$SUDO $PM install -y "$pkg_name"
fi
}
install_autojump
# --------------------- oh-my-zsh安装 ---------------------
OHMYZSH_DIR="$HOME/.oh-my-zsh"
if [ -d "$OHMYZSH_DIR" ]; then
echo -e "${GREEN}✓ oh-my-zsh 已安装${NC}"
else
echo -e "${YELLOW}正在安装 oh-my-zsh...${NC}"
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
fi
# --------------------- 插件安装 ---------------------
mkdir -p "$HOME/.oh-my-zsh/custom/plugins"
function clone_plugin() {
local repo="$1"
local name=$(basename "$repo" .git)
if [ ! -d "${HOME}/.oh-my-zsh/custom/plugins/${name}" ]; then
echo -e "${YELLOW}安装插件: ${name}...${NC}"
git clone --depth=1 "$repo" "${HOME}/.oh-my-zsh/custom/plugins/${name}"
else
echo -e "${GREEN}✓ ${name} 已安装${NC}"
fi
}
clone_plugin "https://github.com/zsh-users/zsh-autosuggestions"
clone_plugin "https://github.com/zdharma-continuum/fast-syntax-highlighting"
# --------------------- 配置文件修改 ---------------------
config_zshrc() {
local config_file="$HOME/.zshrc"
# 插件管理
local plugins=(git autojump zsh-autosuggestions fast-syntax-highlighting)
if grep -q "^plugins=" "$config_file"; then
existing_plugins=$(grep "^plugins=" "$config_file" | sed -E 's/plugins=\((.*)\)/\1/')
all_plugins=($(echo "$existing_plugins ${plugins[*]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
sed -i.bak "s/^plugins=.*/plugins=(${all_plugins[*]})/" "$config_file"
else
echo "plugins=(${plugins[*]})" >> "$config_file"
fi
# 智能添加配置
function add_config {
local pattern="$1"
local line="$2"
if ! grep -q "$pattern" "$config_file"; then
echo "$line" >> "$config_file"
echo -e "${YELLOW}添加配置: ${line}${NC}"
else
echo -e "${GREEN}✓ 配置已存在: ${pattern}${NC}"
fi
}
add_config "FAST_HIGHLIGHT" "source \$ZSH_CUSTOM/plugins/fast-syntax-highlighting/fast-syntax-highlighting.plugin.zsh"
add_config "ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" "ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=240'"
}
config_zshrc
# --------------------- 设置zsh为默认shell ---------------------
current_shell=$(getent passwd $USER | cut -d: -f7)
zsh_path=$(which zsh)
if [ "$current_shell" != "$zsh_path" ]; then
echo -e "${YELLOW}设置zsh为默认shell...${NC}"
# 针对root用户的特殊处理
if [ "$(id -u)" -eq 0 ]; then
# 直接修改/etc/passwd文件
sed -i "s|^root:.*$|root:x:0:0:root:/root:$zsh_path|" /etc/passwd
echo -e "${GREEN}✓ root用户的默认shell已更改为zsh${NC}"
else
# 普通用户使用chsh命令
$SUDO chsh -s "$zsh_path" "$USER"
echo -e "${GREEN}✓ zsh已设置为默认shell${NC}"
fi
else
echo -e "${GREEN}✓ zsh已经是默认shell${NC}"
fi
# --------------------- 完成提示 ---------------------
echo -e "\n${GREEN}✅ 安装完成!${NC}"
echo -e "所有配置已设置好,将在下次登录时生效。"
echo -e "如需立即切换到zsh,请运行:\n"
echo -e "${YELLOW}exec zsh${NC}"
# 自动切换到zsh并应用配置
echo -e "${GREEN}切换到zsh并应用新配置...${NC}"
exec zsh -l