Skip to content

Commit 0da2791

Browse files
committed
update zsh & oh-my-zsh install scripts
1 parent 83bf9ef commit 0da2791

2 files changed

Lines changed: 302 additions & 2 deletions

File tree

codes/linux/soft/zsh-install.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@ command -v git > /dev/null 2>&1 || {
4040
yum install -y zsh
4141
chsh -s /bin/zsh
4242
# install oh-my-zsh
43-
curl -o- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash
43+
# 由于国内经常无法使用 sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
44+
# 所以,索性将安装脚本下载下来直接使用
45+
#curl -o- https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash
46+
zsh/oh-my-zsh-install.sh
4447
# choose oh-my-zsh theme
4548
sed -i "s/^ZSH_THEME=.*/ZSH_THEME=\"ys\"/g" ~/.zshrc
4649
# install oh-my-zsh plugins
4750
git clone https://github.com/zsh-users/zsh-autosuggestions.git ~/.oh-my-zsh/plugins/zsh-autosuggestions
4851
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ~/.oh-my-zsh/plugins/zsh-syntax-highlighting
4952
sed -i "s/^plugins=.*/plugins=(git z wd extract zsh-autosuggestions zsh-syntax-highlighting)/g" ~/.zshrc
5053
# reload zsh
51-
54+
# 注册到 /etc/shells
55+
echo "/usr/bin/zsh" >> /etc/shells
56+
# 切换 shell
57+
chsh -s $(which zsh)
5258
printf "${GREEN}<<<<<<<< install zsh finished${RESET}\n"
5359
printf "${GREEN}Please reboot to take effect.${RESET}\n"
Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
# -------------------------------------------------------------------------------------------------------------------
2+
# 由于国内经常无法使用 sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
3+
# 所以,索性将安装脚本下载下来直接使用
4+
# -------------------------------------------------------------------------------------------------------------------
5+
6+
#!/bin/sh
7+
#
8+
# This script should be run via curl:
9+
# sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
10+
# or wget:
11+
# sh -c "$(wget -qO- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
12+
#
13+
# As an alternative, you can first download the install script and run it afterwards:
14+
# wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
15+
# sh install.sh
16+
#
17+
# You can tweak the install behavior by setting variables when running the script. For
18+
# example, to change the path to the Oh My Zsh repository:
19+
# ZSH=~/.zsh sh install.sh
20+
#
21+
# Respects the following environment variables:
22+
# ZSH - path to the Oh My Zsh repository folder (default: $HOME/.oh-my-zsh)
23+
# REPO - name of the GitHub repo to install from (default: ohmyzsh/ohmyzsh)
24+
# REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS)
25+
# BRANCH - branch to check out immediately after install (default: master)
26+
#
27+
# Other options:
28+
# CHSH - 'no' means the installer will not change the default shell (default: yes)
29+
# RUNZSH - 'no' means the installer will not run zsh after the install (default: yes)
30+
# KEEP_ZSHRC - 'yes' means the installer will not replace an existing .zshrc (default: no)
31+
#
32+
# You can also pass some arguments to the install script to set some these options:
33+
# --skip-chsh: has the same behavior as setting CHSH to 'no'
34+
# --unattended: sets both CHSH and RUNZSH to 'no'
35+
# --keep-zshrc: sets KEEP_ZSHRC to 'yes'
36+
# For example:
37+
# sh install.sh --unattended
38+
#
39+
set -e
40+
41+
# Default settings
42+
ZSH=${ZSH:-~/.oh-my-zsh}
43+
REPO=${REPO:-ohmyzsh/ohmyzsh}
44+
REMOTE=${REMOTE:-https://github.com/${REPO}.git}
45+
BRANCH=${BRANCH:-master}
46+
47+
# Other options
48+
CHSH=${CHSH:-yes}
49+
RUNZSH=${RUNZSH:-yes}
50+
KEEP_ZSHRC=${KEEP_ZSHRC:-no}
51+
52+
53+
command_exists() {
54+
command -v "$@" >/dev/null 2>&1
55+
}
56+
57+
error() {
58+
echo ${RED}"Error: $@"${RESET} >&2
59+
}
60+
61+
setup_color() {
62+
# Only use colors if connected to a terminal
63+
if [ -t 1 ]; then
64+
RED=$(printf '\033[31m')
65+
GREEN=$(printf '\033[32m')
66+
YELLOW=$(printf '\033[33m')
67+
BLUE=$(printf '\033[34m')
68+
BOLD=$(printf '\033[1m')
69+
RESET=$(printf '\033[m')
70+
else
71+
RED=""
72+
GREEN=""
73+
YELLOW=""
74+
BLUE=""
75+
BOLD=""
76+
RESET=""
77+
fi
78+
}
79+
80+
setup_ohmyzsh() {
81+
# Prevent the cloned repository from having insecure permissions. Failing to do
82+
# so causes compinit() calls to fail with "command not found: compdef" errors
83+
# for users with insecure umasks (e.g., "002", allowing group writability). Note
84+
# that this will be ignored under Cygwin by default, as Windows ACLs take
85+
# precedence over umasks except for filesystems mounted with option "noacl".
86+
umask g-w,o-w
87+
88+
echo "${BLUE}Cloning Oh My Zsh...${RESET}"
89+
90+
command_exists git || {
91+
error "git is not installed"
92+
exit 1
93+
}
94+
95+
if [ "$OSTYPE" = cygwin ] && git --version | grep -q msysgit; then
96+
error "Windows/MSYS Git is not supported on Cygwin"
97+
error "Make sure the Cygwin git package is installed and is first on the \$PATH"
98+
exit 1
99+
fi
100+
101+
git clone -c core.eol=lf -c core.autocrlf=false \
102+
-c fsck.zeroPaddedFilemode=ignore \
103+
-c fetch.fsck.zeroPaddedFilemode=ignore \
104+
-c receive.fsck.zeroPaddedFilemode=ignore \
105+
--depth=1 --branch "$BRANCH" "$REMOTE" "$ZSH" || {
106+
error "git clone of oh-my-zsh repo failed"
107+
exit 1
108+
}
109+
110+
echo
111+
}
112+
113+
setup_zshrc() {
114+
# Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones
115+
# with datestamp of installation that moved them aside, so we never actually
116+
# destroy a user's original zshrc
117+
echo "${BLUE}Looking for an existing zsh config...${RESET}"
118+
119+
# Must use this exact name so uninstall.sh can find it
120+
OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh
121+
if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
122+
# Skip this if the user doesn't want to replace an existing .zshrc
123+
if [ $KEEP_ZSHRC = yes ]; then
124+
echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Keeping...${RESET}"
125+
return
126+
fi
127+
if [ -e "$OLD_ZSHRC" ]; then
128+
OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)"
129+
if [ -e "$OLD_OLD_ZSHRC" ]; then
130+
error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}"
131+
error "re-run the installer again in a couple of seconds"
132+
exit 1
133+
fi
134+
mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}"
135+
136+
echo "${YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \
137+
"${GREEN}Backing up to ${OLD_OLD_ZSHRC}${RESET}"
138+
fi
139+
echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Backing up to ${OLD_ZSHRC}${RESET}"
140+
mv ~/.zshrc "$OLD_ZSHRC"
141+
fi
142+
143+
echo "${GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}"
144+
145+
sed "/^export ZSH=/ c\\
146+
export ZSH=\"$ZSH\"
147+
" "$ZSH/templates/zshrc.zsh-template" > ~/.zshrc-omztemp
148+
mv -f ~/.zshrc-omztemp ~/.zshrc
149+
150+
echo
151+
}
152+
153+
setup_shell() {
154+
# Skip setup if the user wants or stdin is closed (not running interactively).
155+
if [ $CHSH = no ]; then
156+
return
157+
fi
158+
159+
# If this user's login shell is already "zsh", do not attempt to switch.
160+
if [ "$(basename "$SHELL")" = "zsh" ]; then
161+
return
162+
fi
163+
164+
# If this platform doesn't provide a "chsh" command, bail out.
165+
if ! command_exists chsh; then
166+
cat <<-EOF
167+
I can't change your shell automatically because this system does not have chsh.
168+
${BLUE}Please manually change your default shell to zsh${RESET}
169+
EOF
170+
return
171+
fi
172+
173+
echo "${BLUE}Time to change your default shell to zsh:${RESET}"
174+
175+
# Prompt for user choice on changing the default login shell
176+
printf "${YELLOW}Do you want to change your default shell to zsh? [Y/n]${RESET} "
177+
read opt
178+
case $opt in
179+
y*|Y*|"") echo "Changing the shell..." ;;
180+
n*|N*) echo "Shell change skipped."; return ;;
181+
*) echo "Invalid choice. Shell change skipped."; return ;;
182+
esac
183+
184+
# Check if we're running on Termux
185+
case "$PREFIX" in
186+
*com.termux*) termux=true; zsh=zsh ;;
187+
*) termux=false ;;
188+
esac
189+
190+
if [ "$termux" != true ]; then
191+
# Test for the right location of the "shells" file
192+
if [ -f /etc/shells ]; then
193+
shells_file=/etc/shells
194+
elif [ -f /usr/share/defaults/etc/shells ]; then # Solus OS
195+
shells_file=/usr/share/defaults/etc/shells
196+
else
197+
error "could not find /etc/shells file. Change your default shell manually."
198+
return
199+
fi
200+
201+
# Get the path to the right zsh binary
202+
# 1. Use the most preceding one based on $PATH, then check that it's in the shells file
203+
# 2. If that fails, get a zsh path from the shells file, then check it actually exists
204+
if ! zsh=$(which zsh) || ! grep -qx "$zsh" "$shells_file"; then
205+
if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -1) || [ ! -f "$zsh" ]; then
206+
error "no zsh binary found or not present in '$shells_file'"
207+
error "change your default shell manually."
208+
return
209+
fi
210+
fi
211+
fi
212+
213+
# We're going to change the default shell, so back up the current one
214+
if [ -n "$SHELL" ]; then
215+
echo $SHELL > ~/.shell.pre-oh-my-zsh
216+
else
217+
grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh
218+
fi
219+
220+
# Actually change the default shell to zsh
221+
if ! chsh -s "$zsh"; then
222+
error "chsh command unsuccessful. Change your default shell manually."
223+
else
224+
export SHELL="$zsh"
225+
echo "${GREEN}Shell successfully changed to '$zsh'.${RESET}"
226+
fi
227+
228+
echo
229+
}
230+
231+
main() {
232+
# Run as unattended if stdin is closed
233+
if [ ! -t 0 ]; then
234+
RUNZSH=no
235+
CHSH=no
236+
fi
237+
238+
# Parse arguments
239+
while [ $# -gt 0 ]; do
240+
case $1 in
241+
--unattended) RUNZSH=no; CHSH=no ;;
242+
--skip-chsh) CHSH=no ;;
243+
--keep-zshrc) KEEP_ZSHRC=yes ;;
244+
esac
245+
shift
246+
done
247+
248+
setup_color
249+
250+
if ! command_exists zsh; then
251+
echo "${YELLOW}Zsh is not installed.${RESET} Please install zsh first."
252+
exit 1
253+
fi
254+
255+
if [ -d "$ZSH" ]; then
256+
cat <<-EOF
257+
${YELLOW}You already have Oh My Zsh installed.${RESET}
258+
You'll need to remove '$ZSH' if you want to reinstall.
259+
EOF
260+
exit 1
261+
fi
262+
263+
setup_ohmyzsh
264+
setup_zshrc
265+
setup_shell
266+
267+
printf "$GREEN"
268+
cat <<-'EOF'
269+
__ __
270+
____ / /_ ____ ___ __ __ ____ _____/ /_
271+
/ __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
272+
/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
273+
\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
274+
/____/ ....is now installed!
275+
276+
277+
Please look over the ~/.zshrc file to select plugins, themes, and options.
278+
279+
p.s. Follow us on https://twitter.com/ohmyzsh
280+
281+
p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh
282+
283+
EOF
284+
printf "$RESET"
285+
286+
if [ $RUNZSH = no ]; then
287+
echo "${YELLOW}Run zsh to try it out.${RESET}"
288+
exit
289+
fi
290+
291+
exec zsh -l
292+
}
293+
294+
main "$@"

0 commit comments

Comments
 (0)