Linux安装ohmyzsh脚本

OhMyZsh安装

默认安装

curl -sSL https://my5353.com/ohmyzsh | bash

使用帮助

curl -sSL https://my5353.com/ohmyzsh | bash /dev/stdin -h

配置安装环境

curl -sSL https://my5353.com/ohmyzsh | bash /dev/stdin -c

查看版本信息

curl -sSL https://my5353.com/ohmyzsh | bash /dev/stdin -v

清除已安装环境

curl -sSL https://my5353.com/ohmyzsh | bash /dev/stdin -r

完整脚本

#!/usr/bin/env bash

###
 # @Descripttion : Install Zsh In Linux
 # @version      : v1.0.0
 # @Author       : neet11 neetwy@163.com
 # @Date         : 2022-09-27 03:01:36
 # @LastEditors  : neet11 neetwy@163.com
 # @LastEditTime : 2022-10-10 03:46:42
 # @FilePath     : /shell/config-dev-env/install_ohmyzsh.sh
### 

# set -o xtrace           # Print commands and their arguments

# set -o errexit          # Exit on most errors (see the manual)
# set -o errtrace         # Make sure any error trap is inherited
set -o nounset          # Disallow expansion of unset variables
set -o pipefail         # Use last non-zero exit code in a pipeline


# global constant
TAG="CMD"
# LOG_PATH=$( cd "$(dirname "${BASH_SOURCE[0]}")" && pwd )/logs
LOG_PATH="/tmp/shell/logs"
LOG_FILE="${LOG_PATH}/install_zsh_$(date +"%Y%m%d").log"
HIDE_LOG=true

# global environment variable
user_name="$(whoami)"


# log handler
function log() {
    [ ! -d "${LOG_PATH}" ] && mkdir -p "${LOG_PATH}"
    if [ $HIDE_LOG ]; then
        echo -e "[$(date +"%Y/%m/%d:%H:%M:%S %z")] [$(whoami)] [$TAG]" "${@}" >> "${LOG_FILE}"
    else
        echo "[$(date +"%Y/%m/%d:%H:%M:%S %z")] [$(whoami)] [$TAG]" "${@}" | tee -a "${LOG_FILE}"
    fi
}

# trap err signal
function script_trap_err() {
    local exit_code=1

    # Disable the error trap handler to prevent potential recursion
    trap - ERR

    # Consider any further errors non-fatal to ensure we run to completion
    set +o errexit
    set +o pipefail

    log "[E] ERROR" "${@}" 
    status_closure clear_zsh_env

    exit "$exit_code"
}

# trap exit signal
function script_trap_exit() {
    log "[I] shell exec done."
}

# define:info(32green) warn(31red) process(33yellow)
function print_color () {
  case $1 in
    red) echo -e "\033[31m$2 \033[0m" ;;
    green) echo -e "\033[32m$2 \033[0m" ;;
    yellow) echo -e "\033[33m$2 \033[0m" ;;
    blue) echo -e "\033[34m$2 \033[0m" ;;
    *) echo -e "\033[31m[Color Error]$2 \033[0m" ;;
   esac
}

# check the command execution status
function status_closure () {
  print_color "green" "${1}"
  eval "${*}"
  print_color "green" "${1} executed successfully"
}

# show help info
function help() {
  echo "Usage: ./install_ohmyzsh.sh [-h -v -c -r]"
  echo "    -h          : display this help and exit"
  echo "    -v          : print zsh version and exit"
  echo "    -c          : config zsh env and exit"
  echo "    -r          : remove zsh env and exit"
  exit 0
}

# get user default shell
function get_user_shell() {
    grep "${user_name}" /etc/passwd | awk -F: '{print $NF }'
}

# install oh my zsh
function install_ohmyzsh() {
  # sh -c "$(curl -fsSL https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh)"
  echo y | sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
}

# check zsh is installed
function check_zsh_install() {
    isInstall=$(grep -c zsh /etc/shells)
    if [ "${isInstall}" -eq 0 ]
    then
        print_color "green" "start install zsh"
        sudo apt-get -y install zsh
        # sudo yum -y install zsh
        status_closure set_default_shell "/bin/zsh"
        status_closure install_ohmyzsh
    else
        print_color "green" "zsh has been installed"
        print_color "blue" "version: $(zsh --version)"
    fi
}

# print zsh version
function print_zsh_version() {
    if [ "$(which zsh)" ]
    then 
        zsh --version
    else
        print_color "red" "zsh has not been installed yet"
        print_color "red" "please install zsh first!"
    fi
}

# set default shell
function set_default_shell() {
    print_color "green" "change ${user_name} default shell" 
    if [ "$(get_user_shell)" != "${1}" ];then sudo usermod -s "${1}" vagrant;fi
    print_color "blue" "current shell: $(get_user_shell)"
    print_color "yellow" "shell change succeeded. please login again"
    print_color "green" "${user_name} default shell is ${1}" 
}

# config oh my zsh
function config_oh_my_zsh() {
    local ZSH_CUSTOM
    ZSH_CUSTOM="${HOME}/.oh-my-zsh/custom"

    if [ ! -d "${ZSH_CUSTOM}/themes/spaceship-prompt" ]
    then
      print_color "green" "install zsh spaceship theme"
      git clone https://github.com/spaceship-prompt/spaceship-prompt.git "$ZSH_CUSTOM/themes/spaceship-prompt" --depth=1
      ln -s "$ZSH_CUSTOM/themes/spaceship-prompt/spaceship.zsh-theme" "$ZSH_CUSTOM/themes/spaceship.zsh-theme"
    else
      print_color "green" "zsh spaceship-prompt theme installed"
    fi

    if [ ! -d "${ZSH_CUSTOM}/plugins/zsh-autosuggestions" ]
    then
      print_color "green" "install zsh zsh-autosuggestions plugin"
      git clone https://github.com/zsh-users/zsh-autosuggestions "${ZSH_CUSTOM:-~/.oh-my-zsh/custom}"/plugins/zsh-autosuggestions
    else
      print_color "green" "zsh zsh-autosuggestions plugin installed"
    fi

    if [ ! -d "${ZSH_CUSTOM}/plugins/zsh-syntax-highlighting" ]
    then
      print_color "green" "install zsh zsh-syntax-highlighting plugin"
      git clone https://github.com/zsh-users/zsh-syntax-highlighting.git "${ZSH_CUSTOM:-~/.oh-my-zsh/custom}"/plugins/zsh-syntax-highlighting
    else
      print_color "green" "zsh zsh-syntax-highlighting  plugin installed"
    fi

    print_color "green" "config .zshrc"
    if [  -f "${HOME}/.zshrc" ]; then rm -rf "${HOME}/.zshrc"; fi
    wget https://gitea.com/neet11/config-dev-env/raw/branch/main/.zshrc -P "${HOME}/" && chmod 644 "${HOME}/.zshrc"
    if [  -f "${HOME}/.spacevimrc.zsh" ]; then rm -rf "${HOME}/.spacevimrc.zsh"; fi
    wget https://gitea.com/neet11/config-dev-env/raw/branch/main/.spaceshiprc.zsh -P "${HOME}/" && chmod 644 "${HOME}/.spaceshiprc.zsh"
    print_color "green" "zsh configfile .zshrc has been updated"
    print_color "yellow" "reload .zshrc use source ${HOME}/.zshrc"
}

# clear zsh env
function clear_zsh_env() {
    local uninstall_oh_my_zsh
    uninstall_oh_my_zsh="${HOME}/.oh-my-zsh/tools/uninstall.sh"

    print_color "green" "remove the oh my zsh" 
    if [ -f "${uninstall_oh_my_zsh}" ]
    then
      #sudo rm -rf ~/.zsh/zsh-autosuggestions
      echo y | bash "${uninstall_oh_my_zsh}"
    fi

    if [ -f "${HOME}/.zshrc" ]
    then
      sudo rm -rf "${HOME}"/.zshrc*
    fi

    print_color "green" "remove the installed zsh" 
    sudo apt-get -y --purge autoremove zsh > /dev/null

    print_color "green" "change default shell to bash" 
    if [ "$(get_user_shell)" != "/bin/bash" ] 
    then
      status_closure set_default_shell "/bin/bash"
    else
      print_color "blue" "current shell: $(get_user_shell)"
      print_color "yellow" "clear zsh succeeded. please login again"
    fi
}

# entry function
function run_install_zsh() {
  status_closure check_zsh_install
}

function main() {
    trap script_trap_err INT TERM QUIT HUP ERR
    trap script_trap_exit EXIT
    log "[I] shell start"

    if [ "${#}" -ne 0 ]
    then
      case $1 in
          -h|help)
            help
          ;;
          -v|version)
            print_zsh_version
          ;;
          -r|remove)
            status_closure clear_zsh_env
          ;;
          -c|config)
            status_closure config_oh_my_zsh
          ;;
          *)
            print_color "red" "unknown parameter" && help
          ;;
      esac
    else
      status_closure run_install_zsh
    fi
}

main "${@}"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值