#!/bin/bash anynowtime="date +'%Y-%m-%d %H:%M:%S'" NOW="echo [\`$anynowtime\`][PID:$$]" INIT_LOG="init_vm_3.1415926.log" ##### 可在脚本开始运行时调用,打印当时的时间戳及PID。 function job_start { echo "`eval $NOW` job_start" } ##### 可在脚本执行成功的逻辑分支处调用,打印当时的时间戳及PID。 function job_success { MSG="$*" echo "`eval $NOW` job_success:[$MSG]" exit 0 } ##### 可在脚本执行失败的逻辑分支处调用,打印当时的时间戳及PID。 function job_fail { MSG="$*" echo "`eval $NOW` job_fail:[$MSG]" exit 1 } job_start ###### 作业平台中执行脚本成功和失败的标准只取决于脚本最后一条执行语句的返回值 ###### 如果返回值为0,则认为此脚本执行成功,如果非0,则认为脚本执行失败 ###### 可在此处开始编写您的脚本逻辑代码 function command_exists { command -v "$@" >/dev/null 2>&1 } function apt_install { if ! command_exists $1; then sudo apt-get install -y $1 | tee -a ${INIT_LOG} 2>&1 else echo "$1 already install" | tee -a ${INIT_LOG} 2>&1 fi } function install_bin_1st { sudo apt-get update; apt_install vim apt_install git apt_install curl apt_install expect } function install_dotnet { if [ -x /usr/bin/dotnet ]; then DOTNET_VER=`dotnet --version` if [ ${DOTNET_VER:0:1} -eq 5 ]; then echo "dotnet 5.0 already install" | tee -a ${INIT_LOG} 2>&1 return fi fi wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb | tee -a ${INIT_LOG} 2>&1 rm packages-microsoft-prod.deb apt_install apt-transport-https apt_install dotnet-sdk-5.0 } function create_user { if [ $# != 2 ] ; then echo "invalid call, input: user password" | tee -a ${INIT_LOG} 2>&1 return fi user=$1 password=$2 if id -u ${user} > /dev/null 2>&1 ; then echo "$user already exist" | tee -a ${INIT_LOG} 2>&1 return fi sudo groupadd $user if [ -x "/usr/bin/zsh" ]; then sudo useradd -m -g $user $user -s /usr/bin/zsh else sudo useradd -m -g $user $user fi /usr/bin/expect < /dev/null 2>&1 ; then echo "$user create succ" | tee -a ${INIT_LOG} 2>&1 else echo "$user create fail" | tee -a ${INIT_LOG} 2>&1 fi } # 创建mmog工作目录 function make_mmog_work_path { if [ ! -d "/data/mmogrun" ]; then sudo mkdir -p /data/mmogrun sudo chown mmog:mmog /data/mmogrun sudo ln -s /data/mmogrun /home/mmog/mmogrun fi if [ ! -d "/data/wwwroot" ]; then sudo mkdir -p /data/wwwroot sudo chown mmog:mmog /data/wwwroot fi if [ ! -d "/data/corefile" ]; then sudo mkdir -p /data/corefile sudo chmod 777 /data/corefile sudo sh -c 'echo "/data/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern' fi } # 修改系统限制 function change_mmog_limits_conf { egrep "mmog" /etc/security/limits.conf >& /dev/null if [ $? -ne 0 ] ; then sh -c 'echo "@mmog soft nofile 50000" >> /etc/security/limits.conf' sh -c 'echo "@mmog hard nofile 65536" >> /etc/security/limits.conf' sh -c 'echo "@mmog soft core 1024000" >> /etc/security/limits.conf' fi } # 修改hostname function change_hostname { if [ $# != 1 ] ; then echo "invalid call, need hostname" | tee -a ${INIT_LOG} 2>&1 return fi HN=$1 sudo hostnamectl set-hostname $HN sudo sed -i "1c 127.0.0.1 localhost.localdomian $HN" /etc/hosts } # 生成.zshrc文件 function create_zshrc { if [ -e "/home/ubuntu/.zshrc" ]; then rm /home/ubuntu/.zshrc fi cat > /home/ubuntu/.zshrc <<\EOF # If you come from bash you might have to change your $PATH. # export PATH=$HOME/bin:/usr/local/bin:$PATH # Path to your oh-my-zsh installation. export ZSH="$HOME/.oh-my-zsh" # Set name of the theme to load --- if set to "random", it will # load a random theme each time oh-my-zsh is loaded, in which case, # to know which specific one was loaded, run: echo $RANDOM_THEME # See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes ZSH_THEME="ys" # Set list of themes to pick from when loading at random # Setting this variable when ZSH_THEME=random will cause zsh to load # a theme from this variable instead of looking in ~/.oh-my-zsh/themes/ # If set to an empty array, this variable will have no effect. # ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" ) # Uncomment the following line to use case-sensitive completion. # CASE_SENSITIVE="true" # Uncomment the following line to use hyphen-insensitive completion. # Case-sensitive completion must be off. _ and - will be interchangeable. # HYPHEN_INSENSITIVE="true" # Uncomment the following line to disable bi-weekly auto-update checks. DISABLE_AUTO_UPDATE="true" # Uncomment the following line to automatically update without prompting. # DISABLE_UPDATE_PROMPT="true" # Uncomment the following line to change how often to auto-update (in days). # export UPDATE_ZSH_DAYS=13 # Uncomment the following line if pasting URLs and other text is messed up. # DISABLE_MAGIC_FUNCTIONS=true # Uncomment the following line to disable colors in ls. # DISABLE_LS_COLORS="true" # Uncomment the following line to disable auto-setting terminal title. # DISABLE_AUTO_TITLE="true" # Uncomment the following line to enable command auto-correction. # ENABLE_CORRECTION="true" # Uncomment the following line to display red dots whilst waiting for completion. # COMPLETION_WAITING_DOTS="true" # Uncomment the following line if you want to disable marking untracked files # under VCS as dirty. This makes repository status check for large repositories # much, much faster. # DISABLE_UNTRACKED_FILES_DIRTY="true" # Uncomment the following line if you want to change the command execution time # stamp shown in the history command output. # You can set one of the optional three formats: # "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd" # or set a custom format using the strftime function format specifications, # see 'man strftime' for details. # HIST_STAMPS="mm/dd/yyyy" # Would you like to use another custom folder than $ZSH/custom? # ZSH_CUSTOM=/path/to/new-custom-folder # Which plugins would you like to load? # Standard plugins can be found in ~/.oh-my-zsh/plugins/* # Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/ # Example format: plugins=(rails git textmate ruby lighthouse) # Add wisely, as too many plugins slow down shell startup. plugins=( autojump zsh-autosuggestions zsh-syntax-highlighting sudo command-not-found colored-man-pages ) source $ZSH/oh-my-zsh.sh # User configuration # export MANPATH="/usr/local/man:$MANPATH" # You may need to manually set your language environment # export LANG=en_US.UTF-8 # Preferred editor for local and remote sessions # if [[ -n $SSH_CONNECTION ]]; then # export EDITOR='vim' # else # export EDITOR='mvim' # fi # Compilation flags # export ARCHFLAGS="-arch x86_64" # Set personal aliases, overriding those provided by oh-my-zsh libs, # plugins, and themes. Aliases can be placed here, though oh-my-zsh # users are encouraged to define aliases within the ZSH_CUSTOM folder. # For a full list of active aliases, run `alias`. # # Example aliases # alias zshconfig="mate ~/.zshrc" # alias ohmyzsh="mate ~/.oh-my-zsh" # Enable 256 color to make auto-suggestions look nice export TERM="xterm-256color" alias grep='grep --color -i' # alt+l = ll bindkey -s '\el' 'll\n' bindkey '\ea' beginning-of-line bindkey '\ee' end-of-line bindkey '\e[1~' beginning-of-line bindkey '\e[4~' end-of-line ZSH_DISABLE_COMPFIX=true . /usr/share/autojump/autojump.sh source $ZSH_CUSTOM/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh source $ZSH_CUSTOM/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh EOF } # 生成oh_my_zsh的安装文件 function create_install_omz_sh { if [ -e "./install_omz.sh" ]; then rm install_omz.sh fi cat > install_omz.sh <<\ZEOF #!/bin/sh # # This script should be run via curl: # sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" # or via wget: # sh -c "$(wget -qO- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" # or via fetch: # sh -c "$(fetch -o - https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" # # As an alternative, you can first download the install script and run it afterwards: # wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh # sh install.sh # # You can tweak the install behavior by setting variables when running the script. For # example, to change the path to the Oh My Zsh repository: # ZSH=~/.zsh sh install.sh # # Respects the following environment variables: # ZSH - path to the Oh My Zsh repository folder (default: $HOME/.oh-my-zsh) # REPO - name of the GitHub repo to install from (default: ohmyzsh/ohmyzsh) # REMOTE - full remote URL of the git repo to install (default: GitHub via HTTPS) # BRANCH - branch to check out immediately after install (default: master) # # Other options: # CHSH - 'no' means the installer will not change the default shell (default: yes) # RUNZSH - 'no' means the installer will not run zsh after the install (default: yes) # KEEP_ZSHRC - 'yes' means the installer will not replace an existing .zshrc (default: no) # # You can also pass some arguments to the install script to set some these options: # --skip-chsh: has the same behavior as setting CHSH to 'no' # --unattended: sets both CHSH and RUNZSH to 'no' # --keep-zshrc: sets KEEP_ZSHRC to 'yes' # For example: # sh install.sh --unattended # or: # sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended # set -e # Track if $ZSH was provided custom_zsh=${ZSH:+yes} # Default settings ZSH=${ZSH:-~/.oh-my-zsh} REPO=${REPO:-ohmyzsh/ohmyzsh} REMOTE=${REMOTE:-https://github.com/${REPO}.git} BRANCH=${BRANCH:-master} # Other options CHSH=${CHSH:-yes} RUNZSH=${RUNZSH:-yes} KEEP_ZSHRC=${KEEP_ZSHRC:-no} command_exists() { command -v "$@" >/dev/null 2>&1 } # The [ -t 1 ] check only works when the function is not called from # a subshell (like in `$(...)` or `(...)`, so this hack redefines the # function at the top level to always return false when stdout is not # a tty. if [ -t 1 ]; then is_tty() { true } else is_tty() { false } fi # This function uses the logic from supports-hyperlinks[1][2], which is # made by Kat Marchán (@zkat) and licensed under the Apache License 2.0. # [1] https://github.com/zkat/supports-hyperlinks # [2] https://crates.io/crates/supports-hyperlinks # # Copyright (c) 2021 Kat Marchán # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. supports_hyperlinks() { # $FORCE_HYPERLINK must be set and be non-zero (this acts as a logic bypass) if [ -n "$FORCE_HYPERLINK" ]; then [ "$FORCE_HYPERLINK" != 0 ] return $? fi # If stdout is not a tty, it doesn't support hyperlinks is_tty || return 1 # DomTerm terminal emulator (domterm.org) if [ -n "$DOMTERM" ]; then return 0 fi # VTE-based terminals above v0.50 (Gnome Terminal, Guake, ROXTerm, etc) if [ -n "$VTE_VERSION" ]; then [ $VTE_VERSION -ge 5000 ] return $? fi # If $TERM_PROGRAM is set, these terminals support hyperlinks case "$TERM_PROGRAM" in Hyper|iTerm.app|terminology|WezTerm) return 0 ;; esac # kitty supports hyperlinks if [ "$TERM" = xterm-kitty ]; then return 0 fi # Windows Terminal or Konsole also support hyperlinks if [ -n "$WT_SESSION" ] || [ -n "$KONSOLE_VERSION" ]; then return 0 fi return 1 } fmt_link() { # $1: text, $2: url, $3: fallback mode if supports_hyperlinks; then printf '\033]8;;%s\a%s\033]8;;\a\n' "$2" "$1" return fi case "$3" in --text) printf '%s\n' "$1" ;; --url|*) fmt_underline "$2" ;; esac } fmt_underline() { is_tty && printf '\033[4m%s\033[24m\n' "$*" || printf '%s\n' "$*" } # shellcheck disable=SC2016 # backtick in single-quote fmt_code() { is_tty && printf '`\033[2m%s\033[22m`\n' "$*" || printf '`%s`\n' "$*" } fmt_error() { printf '%sError: %s%s\n' "$BOLD$RED" "$*" "$RESET" >&2 } setup_color() { # Only use colors if connected to a terminal if is_tty; then RAINBOW=" $(printf '\033[38;5;196m') $(printf '\033[38;5;202m') $(printf '\033[38;5;226m') $(printf '\033[38;5;082m') $(printf '\033[38;5;021m') $(printf '\033[38;5;093m') $(printf '\033[38;5;163m') " RED=$(printf '\033[31m') GREEN=$(printf '\033[32m') YELLOW=$(printf '\033[33m') BLUE=$(printf '\033[34m') BOLD=$(printf '\033[1m') RESET=$(printf '\033[m') else RAINBOW="" RED="" GREEN="" YELLOW="" BLUE="" BOLD="" RESET="" fi } setup_ohmyzsh() { # Prevent the cloned repository from having insecure permissions. Failing to do # so causes compinit() calls to fail with "command not found: compdef" errors # for users with insecure umasks (e.g., "002", allowing group writability). Note # that this will be ignored under Cygwin by default, as Windows ACLs take # precedence over umasks except for filesystems mounted with option "noacl". umask g-w,o-w echo "${BLUE}Cloning Oh My Zsh...${RESET}" command_exists git || { fmt_error "git is not installed" exit 1 } ostype=$(uname) if [ -z "${ostype%CYGWIN*}" ] && git --version | grep -q msysgit; then fmt_error "Windows/MSYS Git is not supported on Cygwin" fmt_error "Make sure the Cygwin git package is installed and is first on the \$PATH" exit 1 fi git clone -c core.eol=lf -c core.autocrlf=false \ -c fsck.zeroPaddedFilemode=ignore \ -c fetch.fsck.zeroPaddedFilemode=ignore \ -c receive.fsck.zeroPaddedFilemode=ignore \ -c oh-my-zsh.remote=origin \ -c oh-my-zsh.branch="$BRANCH" \ --depth=1 --branch "$BRANCH" "$REMOTE" "$ZSH" || { fmt_error "git clone of oh-my-zsh repo failed" exit 1 } echo } setup_zshrc() { # Keep most recent old .zshrc at .zshrc.pre-oh-my-zsh, and older ones # with datestamp of installation that moved them aside, so we never actually # destroy a user's original zshrc echo "${BLUE}Looking for an existing zsh config...${RESET}" # Must use this exact name so uninstall.sh can find it OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then # Skip this if the user doesn't want to replace an existing .zshrc if [ "$KEEP_ZSHRC" = yes ]; then echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Keeping...${RESET}" return fi if [ -e "$OLD_ZSHRC" ]; then OLD_OLD_ZSHRC="${OLD_ZSHRC}-$(date +%Y-%m-%d_%H-%M-%S)" if [ -e "$OLD_OLD_ZSHRC" ]; then fmt_error "$OLD_OLD_ZSHRC exists. Can't back up ${OLD_ZSHRC}" fmt_error "re-run the installer again in a couple of seconds" exit 1 fi mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}" echo "${YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \ "${GREEN}Backing up to ${OLD_OLD_ZSHRC}${RESET}" fi echo "${YELLOW}Found ~/.zshrc.${RESET} ${GREEN}Backing up to ${OLD_ZSHRC}${RESET}" mv ~/.zshrc "$OLD_ZSHRC" fi echo "${GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${RESET}" sed "/^export ZSH=/ c\\ export ZSH=\"$ZSH\" " "$ZSH/templates/zshrc.zsh-template" > ~/.zshrc-omztemp mv -f ~/.zshrc-omztemp ~/.zshrc echo } setup_shell() { # Skip setup if the user wants or stdin is closed (not running interactively). if [ "$CHSH" = no ]; then return fi # If this user's login shell is already "zsh", do not attempt to switch. if [ "$(basename -- "$SHELL")" = "zsh" ]; then return fi # If this platform doesn't provide a "chsh" command, bail out. if ! command_exists chsh; then cat < ~/.shell.pre-oh-my-zsh else grep "^$USERNAME:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh fi # Actually change the default shell to zsh if ! chsh -s "$zsh"; then fmt_error "chsh command unsuccessful. Change your default shell manually." else export SHELL="$zsh" echo "${GREEN}Shell successfully changed to '$zsh'.${RESET}" fi echo } # shellcheck disable=SC2183 # printf string has more %s than arguments ($RAINBOW expands to multiple arguments) print_success() { printf '%s %s__ %s %s %s %s %s__ %s\n' $RAINBOW $RESET printf '%s ____ %s/ /_ %s ____ ___ %s__ __ %s ____ %s_____%s/ /_ %s\n' $RAINBOW $RESET printf '%s / __ \%s/ __ \ %s / __ `__ \%s/ / / / %s /_ / %s/ ___/%s __ \ %s\n' $RAINBOW $RESET printf '%s/ /_/ /%s / / / %s / / / / / /%s /_/ / %s / /_%s(__ )%s / / / %s\n' $RAINBOW $RESET printf '%s\____/%s_/ /_/ %s /_/ /_/ /_/%s\__, / %s /___/%s____/%s_/ /_/ %s\n' $RAINBOW $RESET printf '%s %s %s %s /____/ %s %s %s %s....is now installed!%s\n' $RAINBOW $GREEN $RESET printf '\n' printf '\n' printf "%s %s %s\n" "Before you scream ${BOLD}${YELLOW}Oh My Zsh!${RESET} look over the" \ "$(fmt_code "$(fmt_link ".zshrc" "file://$HOME/.zshrc" --text)")" \ "file to select plugins, themes, and options." printf '\n' printf '%s\n' "• Follow us on Twitter: $(fmt_link @ohmyzsh https://twitter.com/ohmyzsh)" printf '%s\n' "• Join our Discord community: $(fmt_link "Discord server" https://discord.gg/ohmyzsh)" printf '%s\n' "• Get stickers, t-shirts, coffee mugs and more: $(fmt_link "Planet Argon Shop" https://shop.planetargon.com/collections/oh-my-zsh)" printf '%s\n' $RESET } main() { # Run as unattended if stdin is not a tty if [ ! -t 0 ]; then RUNZSH=no CHSH=no fi # Parse arguments while [ $# -gt 0 ]; do case $1 in --unattended) RUNZSH=no; CHSH=no ;; --skip-chsh) CHSH=no ;; --keep-zshrc) KEEP_ZSHRC=yes ;; esac shift done setup_color if ! command_exists zsh; then echo "${YELLOW}Zsh is not installed.${RESET} Please install zsh first." exit 1 fi if [ -d "$ZSH" ]; then echo "${YELLOW}The \$ZSH folder already exists ($ZSH).${RESET}" if [ "$custom_zsh" = yes ]; then cat <&1 if [ -z ${ZSH_CUSTOM} ]; then git clone https://github.com/zsh-users/zsh-autosuggestions $HOME/.oh-my-zsh/custom/plugins/zsh-autosuggestions git clone https://github.com/zsh-users/zsh-syntax-highlighting $HOME/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting else git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions git clone https://github.com/zsh-users/zsh-syntax-highlighting $ZSH_CUSTOM/plugins/zsh-syntax-highlighting fi fi create_zshrc } # 设置ubuntu的vimrc, 当前用户肯定是ubuntu, 无需sudo function create_ubuntu_vimrc { if [ -e "/home/ubuntu/.vimrc" ]; then rm /home/ubuntu/.vimrc fi cat > /home/ubuntu/.vimrc <<\EOF set nu set nocp set ignorecase set smartcase set paste set nospell " Font " set guifont=Bitstream_Vera_Sans_Mono:h12:cANSI " Tab related set ts=4 set sw=4 set smarttab set et set ambiwidth=double " Format related set tw=78 set lbr set fo+=mB " Indent relate set cin set ai set cino=:0g0t0(susj1 " Editing related set backspace=indent,eol,start set whichwrap=h,l,b,s,<,>,[,] "set mouse=a set selectmode= set mousemodel=popup set keymodel= set selection=inclusive " File type related filetype plugin indent on " Display related set ru set sm set wrap set hls " color scheme colo ron syntax enable syntax on EOF } function link_user_vimrc { user=$1 if [ -e "/home/${user}/.vimrc" ]; then sudo rm /home/${user}/.vimrc fi sudo ln -s /home/ubuntu/.vimrc /home/${user}/.vimrc if [ -e "/home/${user}/.vimrc" ]; then echo "link /home/${user}/.vimrc succ" | tee -a ${INIT_LOG} 2>&1 fi } ############################### 安装脚本开始 ################################# #set -x set -e if [ -e "${INIT_LOG}" ]; then rm ${INIT_LOG} fi if [ ${USER} != "ubuntu" ]; then echo "script must run as ubuntu" | tee -a ${INIT_LOG} 2>&1 exit 1 fi cd ~ install_bin_1st install_dotnet install_zsh # 创建ubuntu账号的.vimrc create_ubuntu_vimrc create_user mmog arpg@lbera_2019 create_user mmogguest arpg@lbera_2019 # 设置mmog运行环境 make_mmog_work_path change_mmog_limits_conf #change_hostname job_success