├── README.md ├── install.sh ├── uninstall.sh ├── upgrade.sh └── zh-cn.md /README.md: -------------------------------------------------------------------------------- 1 | # oh-my-zsh on openwrt 2 | 3 | [查看中文说明](./zh-cn.md) 4 | 5 | Install oh-my-zsh on a openwrt router without git and git-http installed. It's base on the official scripts. 6 | 7 | Package git and git-http are so large for a cheap router with a little storage. 8 | 9 | ## dependence packages 10 | 11 | * zsh 12 | 13 | ```bash 14 | opkg update 15 | opkg install zsh 16 | ``` 17 | 18 | ## Install 19 | 20 | ```bash 21 | sh -c "$(curl -fsSL https://raw.githubusercontent.com/felix-fly/openwrt-ohmyzsh/master/install.sh)" 22 | ``` 23 | 24 | ## Set zsh default 25 | 26 | ```bash 27 | which zsh && sed -i -- 's:/bin/ash:'`which zsh`':g' /etc/passwd 28 | ``` 29 | 30 | Relogin your router, you'll see oh-my-zsh here. 31 | 32 | ## Uninstall 33 | 34 | ```bash 35 | sh -c "$(curl -fsSL https://raw.githubusercontent.com/felix-fly/openwrt-ohmyzsh/master/uninstall.sh)" 36 | ``` 37 | 38 | ## Issues 39 | 40 | If you meet this error: 41 | 42 | ```bash 43 | regexp-replace:28: failed to load module: zsh/regex 44 | regexp-replace:28: -regex-match not available for regex 45 | ``` 46 | 47 | You can remove the following file to fix it. 48 | 49 | ```bash 50 | mv ~/.oh-my-zsh/lib/vcs_info.zsh ~/.oh-my-zsh/lib/vcs_info.zsh.bak 51 | ``` 52 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | 5 | # Default settings 6 | ZSH=${ZSH:-~/.oh-my-zsh} 7 | REMOTE=${REMOTE:- https://codeload.github.com/ohmyzsh/ohmyzsh/tar.gz/refs/heads/master} 8 | 9 | # Other options 10 | CHSH=${CHSH:-yes} 11 | RUNZSH=${RUNZSH:-yes} 12 | KEEP_ZSHRC=${KEEP_ZSHRC:-no} 13 | 14 | command_exists() { 15 | command -v "$@" >/dev/null 2>&1 16 | } 17 | 18 | error() { 19 | echo ${RED}"Error: $@"${RESET} >&2 20 | } 21 | 22 | setup_color() { 23 | # Only use colors if connected to a terminal 24 | if [ -t 1 ]; then 25 | RED=$(printf '\033[31m') 26 | GREEN=$(printf '\033[32m') 27 | YELLOW=$(printf '\033[33m') 28 | BLUE=$(printf '\033[34m') 29 | BOLD=$(printf '\033[1m') 30 | RESET=$(printf '\033[m') 31 | else 32 | RED="" 33 | GREEN="" 34 | YELLOW="" 35 | BLUE="" 36 | BOLD="" 37 | RESET="" 38 | fi 39 | } 40 | 41 | setup_ohmyzsh() { 42 | umask g-w,o-w 43 | 44 | echo "${BLUE}Download Oh My Zsh...${RESET}" 45 | 46 | curl $REMOTE | tar xz -C /tmp > /dev/null || { 47 | error "Download oh-my-zsh failed" 48 | exit 1 49 | } 50 | mv /tmp/ohmyzsh-master $ZSH 51 | 52 | # replace upgrade.sh 53 | curl https://raw.githubusercontent.com/felix-fly/openwrt-ohmyzsh/master/upgrade.sh > ${ZSH}/tools/upgrade.sh 54 | # patch to check_for_upgrade.sh 55 | sed -i '/command git.*/d' "${ZSH}/tools/check_for_upgrade.sh" 56 | 57 | echo 58 | } 59 | 60 | setup_zshrc() { 61 | # Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones 62 | # with datestamp of installation that moved them aside, so we never actually 63 | # destroy a user's original zshrc 64 | echo "${BLUE}Looking for an existing zsh config...${RESET}" 65 | 66 | # Must use this exact name so uninstall.sh can find it 67 | OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh 68 | if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then 69 | # Skip this if the user doesn't want to replace an existing .zshrc 70 | if [ $KEEP_ZSHRC = yes ]; then 71 | echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Keeping...${RESET}" 72 | return 73 | fi 74 | if [ -e "$OLD_ZSHRC" ]; then 75 | OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)" 76 | if [ -e "$OLD_OLD_ZSHRC" ]; then 77 | error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}" 78 | error "re-run the installer again in a couple of seconds" 79 | exit 1 80 | fi 81 | mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}" 82 | 83 | echo "${YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \ 84 | "${GREEN}Backing up to ${OLD_OLD_ZSHRC}${RESET}" 85 | fi 86 | echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Backing up to ${OLD_ZSHRC}${RESET}" 87 | mv ~/.zshrc "$OLD_ZSHRC" 88 | fi 89 | 90 | echo "${GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}" 91 | 92 | sed "/^export ZSH=/ c\\ 93 | export ZSH=\"$ZSH\" 94 | " "$ZSH/templates/zshrc.zsh-template" > ~/.zshrc-omztemp 95 | mv -f ~/.zshrc-omztemp ~/.zshrc 96 | 97 | echo 98 | } 99 | 100 | setup_shell() { 101 | # Skip setup if the user wants or stdin is closed (not running interactively). 102 | if [ $CHSH = no ]; then 103 | return 104 | fi 105 | 106 | # If this user's login shell is already "zsh", do not attempt to switch. 107 | if [ "$(basename "$SHELL")" = "zsh" ]; then 108 | return 109 | fi 110 | 111 | # If this platform doesn't provide a "chsh" command, bail out. 112 | if ! command_exists chsh; then 113 | cat <<-EOF 114 | I can't change your shell automatically because this system does not have chsh. 115 | ${BLUE}Please manually change your default shell to zsh${RESET} 116 | EOF 117 | return 118 | fi 119 | 120 | echo "${BLUE}Time to change your default shell to zsh:${RESET}" 121 | 122 | # Prompt for user choice on changing the default login shell 123 | printf "${YELLOW}Do you want to change your default shell to zsh? [Y/n]${RESET} " 124 | read opt 125 | case $opt in 126 | y*|Y*|"") echo "Changing the shell..." ;; 127 | n*|N*) echo "Shell change skipped."; return ;; 128 | *) echo "Invalid choice. Shell change skipped."; return ;; 129 | esac 130 | 131 | # Check if we're running on Termux 132 | case "$PREFIX" in 133 | *com.termux*) termux=true; zsh=zsh ;; 134 | *) termux=false ;; 135 | esac 136 | 137 | if [ "$termux" != true ]; then 138 | # Test for the right location of the "shells" file 139 | if [ -f /etc/shells ]; then 140 | shells_file=/etc/shells 141 | elif [ -f /usr/share/defaults/etc/shells ]; then # Solus OS 142 | shells_file=/usr/share/defaults/etc/shells 143 | else 144 | error "could not find /etc/shells file. Change your default shell manually." 145 | return 146 | fi 147 | 148 | # Get the path to the right zsh binary 149 | # 1. Use the most preceding one based on $PATH, then check that it's in the shells file 150 | # 2. If that fails, get a zsh path from the shells file, then check it actually exists 151 | if ! zsh=$(which zsh) || ! grep -qx "$zsh" "$shells_file"; then 152 | if ! zsh=$(grep '^/.*/zsh$' "$shells_file" | tail -1) || [ ! -f "$zsh" ]; then 153 | error "no zsh binary found or not present in '$shells_file'" 154 | error "change your default shell manually." 155 | return 156 | fi 157 | fi 158 | fi 159 | 160 | # We're going to change the default shell, so back up the current one 161 | if [ -n "$SHELL" ]; then 162 | echo $SHELL > ~/.shell.pre-oh-my-zsh 163 | else 164 | grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh 165 | fi 166 | 167 | # Actually change the default shell to zsh 168 | if ! chsh -s "$zsh"; then 169 | error "chsh command unsuccessful. Change your default shell manually." 170 | else 171 | export SHELL="$zsh" 172 | echo "${GREEN}Shell successfully changed to '$zsh'.${RESET}" 173 | fi 174 | 175 | echo 176 | } 177 | 178 | main() { 179 | # Run as unattended if stdin is closed 180 | if [ ! -t 0 ]; then 181 | RUNZSH=no 182 | CHSH=no 183 | fi 184 | 185 | # Parse arguments 186 | while [ $# -gt 0 ]; do 187 | case $1 in 188 | --unattended) RUNZSH=no; CHSH=no ;; 189 | --skip-chsh) CHSH=no ;; 190 | --keep-zshrc) KEEP_ZSHRC=yes ;; 191 | esac 192 | shift 193 | done 194 | 195 | setup_color 196 | 197 | if ! command_exists zsh; then 198 | echo "${YELLOW}Zsh is not installed.${RESET} Please install zsh first." 199 | exit 1 200 | fi 201 | 202 | if [ -d "$ZSH" ]; then 203 | cat <<-EOF 204 | ${YELLOW}You already have Oh My Zsh installed.${RESET} 205 | You'll need to remove '$ZSH' if you want to reinstall. 206 | EOF 207 | exit 1 208 | fi 209 | 210 | setup_ohmyzsh 211 | setup_zshrc 212 | setup_shell 213 | 214 | # Keep or remove plugins 215 | read -r -p "Keep plugins(about 5M)? [y/N] " input 216 | case $input in 217 | [yY][eE][sS]|[yY]) 218 | echo "Yes" 219 | ;; 220 | *) 221 | echo "No" 222 | rm -rf "${ZSH}/plugins" 223 | sed -i '/^plugins=.*/d' ~/.zshrc 224 | ;; 225 | esac 226 | 227 | printf "$GREEN" 228 | cat <<-'EOF' 229 | __ __ 230 | ____ / /_ ____ ___ __ __ ____ _____/ /_ 231 | / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \ 232 | / /_/ / / / / / / / / / / /_/ / / /_(__ ) / / / 233 | \____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/ 234 | /____/ ....is now installed! 235 | 236 | 237 | Please look over the ~/.zshrc file to select plugins, themes, and options. 238 | 239 | p.s. Follow us on https://twitter.com/ohmyzsh 240 | 241 | p.p.s. Get stickers, shirts, and coffee mugs at https://shop.planetargon.com/collections/oh-my-zsh 242 | 243 | EOF 244 | printf "$RESET" 245 | 246 | if [ $RUNZSH = no ]; then 247 | echo "${YELLOW}Run zsh to try it out.${RESET}" 248 | exit 249 | fi 250 | 251 | exec zsh -l 252 | } 253 | 254 | main "$@" 255 | -------------------------------------------------------------------------------- /uninstall.sh: -------------------------------------------------------------------------------- 1 | read -r -p "Are you sure you want to remove Oh My Zsh? [y/N] " confirmation 2 | if [ "$confirmation" != y ] && [ "$confirmation" != Y ]; then 3 | echo "Uninstall cancelled" 4 | exit 5 | fi 6 | 7 | echo "Removing ~/.oh-my-zsh" 8 | if [ -d ~/.oh-my-zsh ]; then 9 | rm -rf ~/.oh-my-zsh 10 | fi 11 | 12 | echo "Looking for original zsh config..." 13 | ZSHRC_ORIG=~/.zshrc.pre-oh-my-zsh 14 | if [ -e "$ZSHRC_ORIG" ]; then 15 | echo "Found $ZSHRC_ORIG -- Restoring to ~/.zshrc" 16 | 17 | if [ -e ~/.zshrc ]; then 18 | ZSHRC_SAVE=~/.zshrc.omz-uninstalled-$(date +%Y-%m-%d_%H-%M-%S) 19 | echo "Found ~/.zshrc -- Renaming to ${ZSHRC_SAVE}" 20 | mv ~/.zshrc "${ZSHRC_SAVE}" 21 | fi 22 | 23 | mv "$ZSHRC_ORIG" ~/.zshrc 24 | 25 | echo "Your original zsh config was restored." 26 | fi 27 | 28 | if hash chsh >/dev/null 2>&1 && [ -f ~/.shell.pre-oh-my-zsh ]; then 29 | old_shell=$(cat ~/.shell.pre-oh-my-zsh) 30 | echo "Switching your shell back to '$old_shell':" 31 | if chsh -s "$old_shell"; then 32 | rm -f ~/.shell.pre-oh-my-zsh 33 | else 34 | echo "Could not change default shell. Change it manually by running chsh" 35 | echo "or editing the /etc/passwd file." 36 | fi 37 | fi 38 | 39 | echo "Thanks for trying out Oh My Zsh. It's been uninstalled." 40 | echo "Don't forget to restart your terminal!" -------------------------------------------------------------------------------- /upgrade.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | ZSH=${ZSH:-~/.oh-my-zsh} 4 | REMOTE=${REMOTE:-https://codeload.github.com/ohmyzsh/ohmyzsh/tar.gz/refs/heads/master} 5 | 6 | curl $REMOTE | tar xz -C /tmp > /dev/null || { 7 | printf "Download oh-my-zsh failed" 8 | exit 1 9 | } 10 | cp -rf /tmp/ohmyzsh-master/* $ZSH 11 | rm -rf /tmp/ohmyzsh-master 12 | 13 | # check plugins status 14 | if [ ! -d "${ZSH}/plugins" ]; then 15 | plugins=false 16 | else 17 | plugins=true 18 | fi 19 | 20 | # replace upgrade.sh 21 | curl https://raw.githubusercontent.com/felix-fly/openwrt-ohmyzsh/master/upgrade.sh -O "${ZSH}/tools/upgrade.sh" 22 | # patch to check_for_upgrade.sh 23 | sed -i '/^whence git.*/d' "${ZSH}/tools/check_for_upgrade.sh" 24 | # fix for remove lock file 25 | mkdir "$ZSH/log/update.lock" 2>/dev/null 26 | 27 | # remove plugins 28 | if [[ $plugins != true ]]; then 29 | rm -rf "${ZSH}/plugins" 30 | fi 31 | 32 | printf "Update oh-my-zsh successful." 33 | -------------------------------------------------------------------------------- /zh-cn.md: -------------------------------------------------------------------------------- 1 | # 在 openwrt 中安装 oh-my-zsh 2 | 3 | oh-my-zsh 官方的安装脚本需要预先安装 git 和 git-http,但是这两个包对于小内存的路由器来说实在是太大了。此处的脚本是基于官方脚本改造的,不使用git而是通过curl获取源码的tar.gz包的方式来安装。 4 | 5 | ## 依赖的包 6 | 7 | * zsh 8 | 9 | ```bash 10 | opkg update 11 | opkg install zsh 12 | ``` 13 | 14 | ## 安装 15 | 16 | ```bash 17 | sh -c "$(curl -fsSL https://raw.githubusercontent.com/felix-fly/openwrt-ohmyzsh/master/install.sh)" 18 | ``` 19 | 20 | ## 设置 zsh 为默认 shell 21 | 22 | ```bash 23 | which zsh && sed -i -- 's:/bin/ash:'`which zsh`':g' /etc/passwd 24 | ``` 25 | 26 | 重新登录到路由器,就是 oh-my-zsh 了。 27 | 28 | ## 卸载 29 | 30 | ```bash 31 | sh -c "$(curl -fsSL https://raw.githubusercontent.com/felix-fly/openwrt-ohmyzsh/master/uninstall.sh)" 32 | ``` 33 | 34 | ## 问题解决 35 | 36 | 如果遇到这样的错误提示: 37 | 38 | ```bash 39 | regexp-replace:28: failed to load module: zsh/regex 40 | regexp-replace:28: -regex-match not available for regex 41 | ``` 42 | 43 | 可以删除下面的文件来解决。 44 | 45 | ```bash 46 | mv ~/.oh-my-zsh/lib/vcs_info.zsh ~/.oh-my-zsh/lib/vcs_info.zsh.bak 47 | ``` 48 | --------------------------------------------------------------------------------