├── .gitignore ├── README.md ├── sh ├── README.md ├── centos-7.x-install-deno.sh ├── sshpass.sh ├── centos-7.x-update-kernel.sh └── typecho │ └── install.sh ├── install-curl.sh ├── zsh └── ubuntu.sh └── helper.sh /.gitignore: -------------------------------------------------------------------------------- 1 | .history 2 | *.log 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # shs 2 | more shell scripts for my self 3 | -------------------------------------------------------------------------------- /sh/README.md: -------------------------------------------------------------------------------- 1 | ## 内容介绍 2 | 3 | 所有使用方式介绍均使用`curl`命令,如果你的系统不支持,请先安装,或者使用`wget`方式下载到本地后执行 4 | 5 | 1. `typecho` 6 | 部署一个typecho博客,使用root用户执行以下命令 7 | ```shell 8 | bash <(curl -sL http://git.io/typecho.sh) 9 | ``` 10 | 11 | -------------------------------------------------------------------------------- /install-curl.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | set -e 3 | # 4 | # Usage 5 | # wget -O - https://github.com/aliuq/shs/raw/main/install-curl.sh | sh 6 | # wget -O - https://github.com/aliuq/shs/raw/main/install-curl.sh | sh -s 8.5.0 7 | # 8 | 9 | CURL_VERSION=${1:-8.6.0} 10 | 11 | echo "Installing curl version $CURL_VERSION" 12 | 13 | apk add --update --no-cache openssl-dev nghttp2-dev ca-certificates libpsl-dev 14 | apk add --update --no-cache --virtual curldeps g++ make perl 15 | 16 | wget https://curl.haxx.se/download/curl-$CURL_VERSION.tar.bz2 -O /tmp/curl-$CURL_VERSION.tar.bz2 && \ 17 | cd /tmp && \ 18 | tar xjvf curl-$CURL_VERSION.tar.bz2 && \ 19 | rm curl-$CURL_VERSION.tar.bz2 && \ 20 | cd curl-$CURL_VERSION && \ 21 | ./configure \ 22 | --with-nghttp2=/usr \ 23 | --prefix=/usr \ 24 | --with-ssl \ 25 | --enable-ipv6 \ 26 | --enable-unix-sockets \ 27 | --without-libidn \ 28 | --disable-static \ 29 | --disable-ldap \ 30 | --with-pic && \ 31 | make && \ 32 | make install && \ 33 | cd / && \ 34 | rm -rf /tmp/curl-$CURL_VERSION && \ 35 | apk del curldeps 36 | -------------------------------------------------------------------------------- /zsh/ubuntu.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Usage 4 | # 5 | # > curl -fsSL https://raw.githubusercontent.com/aliuq/shs/main/zsh/ubuntu.sh | sh 6 | # > curl -fsSL https://raw.githubusercontent.com/aliuq/shs/main/zsh/ubuntu.sh | sh -s 5.9 7 | # > ZSH_ORIGIN=https://udomain.dl.sourceforge.net curl -fsSL https://raw.githubusercontent.com/aliuq/shs/main/zsh/ubuntu.sh | sh -s 5.9 8 | # 9 | ZSH_VERSION=${1:-5.9} 10 | ZSH_ORIGIN=${ZSH_ORIGIN:-"https://zenlayer.dl.sourceforge.net"} 11 | 12 | echo """ 13 | Installing zsh version $ZSH_VERSION on $ZSH_ORIGIN 14 | 15 | OS: Ubuntu 16 | Requireed: curl make gcc libncurses5-dev libncursesw5-dev 17 | 18 | Run the following commands to install required deps: 19 | 20 | apt install -y curl make gcc libncurses5-dev libncursesw5-dev 21 | """ 22 | 23 | command_exists() { 24 | command -v "$@" >/dev/null 2>&1 25 | } 26 | 27 | check_commands() { 28 | for cmd in "$@"; do 29 | if ! command_exists "$cmd"; then 30 | echo "Error: $cmd is not installed or not in PATH" >&2 31 | exit 1 32 | fi 33 | done 34 | } 35 | 36 | install_zsh() { 37 | curl -fsS -o /tmp/zsh.tar.xz $ZSH_ORIGIN/project/zsh/zsh/$ZSH_VERSION/zsh-$ZSH_VERSION.tar.xz && \ 38 | tar -xf /tmp/zsh.tar.xz -C /tmp && \ 39 | cd /tmp/zsh-$ZSH_VERSION && \ 40 | ./Util/preconfig && \ 41 | ./configure --without-tcsetpgrp --prefix=/usr --bindir=/bin && \ 42 | make -j 20 install.bin install.modules install.fns && \ 43 | cd / && rm -rf /tmp/zsh.tar.xz && rm -rf /tmp/zsh-$ZSH_VERSION && \ 44 | zsh --version && \ 45 | echo "/bin/zsh" | tee -a /etc/shells && \ 46 | echo "/usr/bin/zsh" | tee -a /etc/shells 47 | } 48 | 49 | check_commands curl tar make gcc 50 | install_zsh 51 | -------------------------------------------------------------------------------- /sh/centos-7.x-install-deno.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | set -e 3 | # 4 | # Install deno (https://deno.land) 5 | # 6 | # Used mirrors: 7 | # deno: https://x.deno.js.cn/install.sh 8 | # glibc-2.18: https://ftp.gnu.org/gnu/glibc/glibc-2.18.tar.gz 9 | # 10 | # Usage: 11 | # $ source <(curl -sL https://raw.githubusercontent.com/aliuq/shs/main/sh/centos-7.x-install-deno.sh) 12 | # 13 | 14 | DEFAULT_DENO_URL="https://deno.land/x/install/install.sh" 15 | DEFAULT_GLIBC_2_18_URL="https://ftp.gnu.org/gnu/glibc/glibc-2.18.tar.gz" 16 | if [ -z "$DENO_URL" ]; then 17 | DENO_URL=$DEFAULT_DENO_URL 18 | fi 19 | if [ -z "$GLIBC_2_18_URL" ]; then 20 | GLIBC_2_18_URL=$DEFAULT_GLIBC_2_18_URL 21 | fi 22 | 23 | mirror='' 24 | while [ $# -gt 0 ]; do 25 | case "$1" in 26 | --mirror) 27 | mirror="$2" 28 | shift 29 | ;; 30 | --*) 31 | echo "Illegal option $1" 32 | ;; 33 | esac 34 | shift $(($# > 0 ? 1 : 0)) 35 | done 36 | 37 | case "$mirror" in 38 | Aliyun) 39 | DENO_URL="https://x.deno.js.cn/install.sh" 40 | GLIBC_2_18_URL="https://aliuq.oss-cn-beijing.aliyuncs.com/deno/glibc-2.18.tar.gz" 41 | ;; 42 | esac 43 | 44 | ldd=$(ldd --version | grep 'ldd (GNU libc) ' | head -n 1) 45 | lddver=${ldd:15} 46 | 47 | function version_lt() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"; } 48 | 49 | if version_lt $lddver '2.18'; then 50 | mkdir /temp_down -p && cd /temp_down 51 | wget "$GLIBC_2_18_URL" 52 | tar -zxvf glibc-2.18.tar.gz 53 | 54 | cd glibc-2.18 && mkdir build 55 | cd build 56 | ../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin 57 | make && make install 58 | 59 | cd ~ 60 | rm -rf /temp_down 61 | fi 62 | 63 | curl -fsSL "$DENO_URL" | sh 64 | 65 | export DENO_INSTALL="/root/.deno" 66 | export PATH="$DENO_INSTALL/bin:$PATH" 67 | -------------------------------------------------------------------------------- /sh/sshpass.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | set -e 3 | # 4 | # Copy public key to remote host, and update ssh configuration `PubkeyAuthentication` to `yes` 5 | # 6 | # Usage: 7 | # echo " X.X.X.X " | sh <(curl -fsSL https://raw.githubusercontent.com/aliuq/shs/main/sh/sshpass.sh) --name test_rsa 8 | # 9 | # View ssh config: 10 | # cat /etc/ssh/sshd_config | grep -P "(AuthorizedKeysFile )|(PubkeyAuthentication )|(PasswordAuthentication )" 11 | # 12 | # Update ssh config: 13 | # sed -i "s/^#\?PasswordAuthentication no$/PasswordAuthentication yes/g" /etc/ssh/sshd_config && systemctl restart sshd 14 | # 15 | 16 | red='\033[0;31m' 17 | green='\033[0;32m' 18 | yellow='\033[0;33m' 19 | plain='\033[0m' 20 | 21 | name="id_rsa" 22 | while [ $# -gt 0 ]; do 23 | case "$1" in 24 | --name) 25 | name="$2" 26 | shift 27 | ;; 28 | --*) 29 | echo "Illegal option $1" 30 | ;; 31 | esac 32 | shift $(($# > 0 ? 1 : 0)) 33 | done 34 | 35 | command_exists() { 36 | command -v "$@" > /dev/null 2>&1 37 | } 38 | 39 | do_run() { 40 | if ! command_exists sshpass; then 41 | yum install sshpass -y 42 | fi 43 | 44 | secrect="$HOME/.ssh/$name" 45 | if [ -a "$secrect" ]; then 46 | echo -e "${red}ERROR: ssh key $name is already exists at $secrect${plain}" 47 | echo 48 | else 49 | ssh-keygen -t rsa -f $secrect -N "" -q 50 | fi 51 | 52 | while read user ip passwd 53 | do 54 | sleep 1 55 | sshpass -p $passwd ssh-copy-id -i $secrect.pub -o StrictHostKeyChecking=no $user@$ip 56 | sleep 1 57 | sshpass -p $passwd ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $user@$ip \ 58 | 'sed -i "s/^#\?PubkeyAuthentication \(yes\|no\)$/PubkeyAuthentication yes/g" /etc/ssh/sshd_config && systemctl restart sshd' 59 | echo 60 | echo -e " ${green}ssh -i $secrect $user@$ip${plain}" 61 | echo 62 | done 63 | } 64 | do_run 65 | -------------------------------------------------------------------------------- /sh/centos-7.x-update-kernel.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | #=================================================================# 3 | # System Required: CentOS 7 v0.0.1 # 4 | # Description: Update kernel # 5 | # Description: CentOS系统内核 # 6 | #=================================================================# 7 | 8 | clear 9 | 10 | # Colors 11 | red='\033[0;31m' 12 | green='\033[0;32m' 13 | yellow='\033[0;33m' 14 | plain='\033[0m' 15 | 16 | kernel=$(uname -sr) 17 | 18 | echo "" 19 | echo -e "Current kernel version: ${yellow}$kernel${plain}" 20 | echo "" 21 | read -n2 -p "Are you sure to update kernel [Y/N]?" answer 22 | 23 | case $answer in 24 | (Y | y) 25 | echo "" 26 | echo -e "${green}- Update kernel${plain}" 27 | echo "" 28 | rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org 29 | rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm 30 | yum --disablerepo="*" --enablerepo="elrepo-kernel" list available 31 | 32 | echo "" 33 | echo -e "${green}- Install kernel${plain}" 34 | echo "" 35 | yum --enablerepo=elrepo-kernel install kernel-lt -y 36 | 37 | echo "" 38 | echo -e "${green}- Set default${plain}" 39 | echo "" 40 | sed -i 's/GRUB_DEFAULT=saved/GRUB_DEFAULT=0/g' /etc/default/grub 41 | 42 | echo "" 43 | echo -e "${green}- Regenerate the grub configuration file${plain}" 44 | echo "" 45 | grub2-mkconfig -o /boot/grub2/grub.cfg 46 | 47 | echo "" 48 | echo -e "${green}- Remove old tools${plain}" 49 | yum remove -y kernel-tools-libs.x86_64 kernel-tools.x86_64 50 | 51 | echo "" 52 | echo -e "${green}- Install new tools${plain}" 53 | yum --disablerepo=\* --enablerepo=elrepo-kernel install -y kernel-lt-tools.x86_64 54 | 55 | echo "" 56 | echo -e "${green}- Reboot wait 5s${plain}" 57 | echo "" 58 | sleep 5s 59 | 60 | reboot 61 | ;; 62 | esac 63 | -------------------------------------------------------------------------------- /sh/typecho/install.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | #=================================================================# 3 | # System Required: CentOS 7 v0.0.1 # 4 | # Description: Deploy Typecho Blog Shell Script # 5 | # Author: Linka # 6 | #=================================================================# 7 | 8 | clear 9 | echo 10 | echo "###################################################################" 11 | echo "# #" 12 | echo "# Deploy Typecho Blog In Centos7 v0.0.1 #" 13 | echo "# Content: Apache、MySQL5.7、Php7、Typecho #" 14 | echo "# Author: Linka #" 15 | echo "# #" 16 | echo "###################################################################" 17 | echo 18 | 19 | # Commands 20 | commands=( 21 | "一键安装" 22 | "安装Apache" 23 | "卸载Apache" 24 | "安装MySQL5.7" 25 | "卸载MySQL5.7" 26 | "安装Php7" 27 | "卸载Php7" 28 | "安装Typecho" 29 | "修改数据库密码" 30 | "服务检测" 31 | "退出" 32 | ) 33 | # Colors 34 | red='\033[0;31m' 35 | green='\033[0;32m' 36 | yellow='\033[0;33m' 37 | plain='\033[0m' 38 | 39 | echo_commands() { 40 | for ((i=1;i<${#commands[@]}+1;i++)) 41 | do 42 | echo -e "${green}${i}${plain}) ${commands[$i-1]}" 43 | done 44 | echo 45 | read -p "请选择一个命令: " command 46 | excute_command $command 47 | } 48 | check_package_is_installed(){ 49 | which $1 &> /dev/null 50 | if [ $? -eq 0 ]; then 51 | echo -e "${green}$2 已安装${plain}" 52 | return 0 53 | else 54 | return 1 55 | fi 56 | } 57 | install_apache(){ 58 | echo 59 | echo "======================= 🧡 安装Apache =======================" 60 | echo 61 | if [ $1 ]; then 62 | uninstall_package httpd 63 | fi 64 | check_package_is_installed httpd Apache 65 | if [[ $? != 0 || $1 ]]; then 66 | yum install -y httpd 67 | systemctl start httpd 68 | systemctl enable httpd 69 | fi 70 | } 71 | install_mysql() { 72 | echo 73 | echo "======================= 🧡 安装MySQL5.7 =====================" 74 | echo 75 | if [ $1 ]; then 76 | uninstall_package mysql5.7 77 | fi 78 | check_package_is_installed mysql mysql5.7 79 | if [[ $? != 0 || $1 ]]; then 80 | wget -i http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm 81 | yum -y install mysql57-community-release-el7-10.noarch.rpm 82 | yum -y install mysql-community-server 83 | systemctl start mysqld.service 84 | systemctl status mysqld.service 85 | fi 86 | } 87 | install_php() { 88 | echo 89 | echo "======================= 🧡 安装Php7 =========================" 90 | echo 91 | if [ $1 ]; then 92 | uninstall_package Php7 93 | fi 94 | check_package_is_installed php Php7 95 | if [[ $? != 0 || $1 ]]; then 96 | rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm 97 | rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm 98 | yum install -y php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysql.x86_64 php70w-pdo.x86_64 php70w-fpm 99 | yum -y install php-mysql php-gd php-imap php-ldap php-odbc php-mbstring php-devel php-soap php-cli php-pdo 100 | yum -y install php-mcrypt php-tidy php-xml php-xmlrpc php-pear 101 | yum -y install php-pecl-memcache php-eaccelerator 102 | fi 103 | } 104 | install_typecho(){ 105 | echo 106 | echo "======================= 🧡 安装typecho ======================" 107 | echo 108 | link=$(curl -s http://typecho.org/download | grep downloads | sed -r 's/.*?(http:.*?\.tar\.gz).*?/\1/g') 109 | filename="typecho.tar.gz" 110 | if [ $link ]; then 111 | wget $link -O $filename 112 | tar xzvf $filename -C /var/www/html 113 | mv /var/www/html/build/* /var/www/html -bf 114 | rm /var/www/html/build -rf 115 | fi 116 | } 117 | uninstall_package(){ 118 | echo -e "\n${red}开始卸载: $1${plain}\n" 119 | list=$(rpm -qa | grep $1 | sed -r 's/\\n//g') 120 | for name in $list 121 | do 122 | echo -e "[${green}Info${plain}] 正在卸载$name" 123 | rpm -e --nodeps $name 124 | done 125 | echo 126 | } 127 | write_root_mysql_conf(){ 128 | password=$1 129 | if [[ -z ${password} ]]; then 130 | grepStr=$(grep "password" /var/log/mysqld.log) 131 | sedStr=$(echo $grepStr | sed -r 's/.*?root@localhost: (.*?)/\1/g') 132 | password=$(echo $sedStr | awk '{print substr($1, 0, 12)}') 133 | fi 134 | echo -e "[client]\nuser=root\nport=3306\nhost=127.0.0.1\npassword=$password" > /root/.my.cnf 135 | } 136 | update_mysql_password(){ 137 | cat /root/.my.cnf &> /dev/null 138 | if [ $? == 0 ]; then 139 | echo 140 | echo -n "请输入数据库密码(包含字母、数字、特殊符号): " 141 | read -s oldpassword 142 | echo 143 | write_root_mysql_conf $oldpassword 144 | else 145 | write_root_mysql_conf 146 | fi 147 | echo 148 | echo -n "请输入新的数据库密码(包含字母、数字、特殊符号): " 149 | read -s password 150 | echo 151 | mysql --connect-expired-password <" > /var/www/html/phpinfo.php 162 | code=$(curl -m 20 -I -s -o /dev/null -w %{http_code}"\n" http://localhost/phpinfo.php) 163 | if [[ $code == "200" ]]; then 164 | echo -e "${green}Php: 访问正常${plain}" 165 | 166 | else 167 | echo -e "${red}Php: 访问失败${plain}" 168 | fi 169 | rm /var/www/html/phpinfo.php -rf 170 | # Typecho 171 | code=$(curl -m 20 -I -s -o /dev/null -w %{http_code}"\n" http://localhost/install.php) 172 | if [ $code == "200" ]; 173 | then 174 | echo -e "${green}Typecho: 部署成功${plain}" 175 | else 176 | echo -e "${red}Typecho: 部署失败${plain}" 177 | fi 178 | } 179 | excute_command(){ 180 | case $1 in 181 | 1) 182 | install_apache 183 | install_mysql 184 | install_php 185 | update_mysql_password 186 | install_typecho 187 | ;; 188 | 2) install_apache 189 | ;; 190 | 3) uninstall_package httpd 191 | ;; 192 | 4) install_mysql 193 | ;; 194 | 5) 195 | uninstall_package mariadb 196 | uninstall_package mysql 197 | ;; 198 | 6) install_php 199 | ;; 200 | 7) uninstall_package php7 201 | ;; 202 | 8) install_typecho 203 | ;; 204 | 9) update_mysql_password 205 | ;; 206 | 10) check_service_status 207 | ;; 208 | 11) exit 209 | ;; 210 | *) echo -e "\n[${yellow}$1${plain}] 命令不存在" 211 | ;; 212 | esac 213 | echo 214 | echo 215 | echo_commands 1 216 | } 217 | 218 | echo -e "${red}* 请勿在安装过程中按下[Enter]键${plain}\n" 219 | echo_commands 220 | -------------------------------------------------------------------------------- /helper.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Usage: 4 | # 5 | # For shell: 6 | # 7 | # . /dev/stdin </dev/null 2>&1 159 | } 160 | 161 | command_valid() { 162 | if ! command_exists "$1"; then 163 | if [ -z "$2" ]; then 164 | red "Error: $1 is not installed or not in PATH" 165 | else 166 | red "$2" 167 | fi 168 | exit 1 169 | fi 170 | } 171 | 172 | commands_valid() { 173 | for cmd in "$@"; do 174 | command_valid "$cmd" 175 | done 176 | } 177 | 178 | run() { 179 | if $dry_run; then 180 | echo "+ $sh_c '$1'" 181 | return 182 | fi 183 | if $verbose; then 184 | echo "+ $sh_c '$1'" 185 | fi 186 | $sh_c "$1" 187 | } 188 | 189 | set_var() { 190 | user="$(id -un 2>/dev/null || true)" 191 | sh_c="sh -c" 192 | if [ "$user" != "root" ]; then 193 | if command_exists sudo; then 194 | sh_c="sudo -E sh -c" 195 | elif command_exists su; then 196 | sh_c="su -c" 197 | else 198 | printf >&2 "Error: this installer needs the ability to run commands as root.\n" 199 | printf >&2 "We are unable to find either \"sudo\" or \"su\" available to make this happen.\n" 200 | exit 1 201 | fi 202 | fi 203 | } 204 | 205 | # 发送 Webhook 消息 206 | send_webhook() { 207 | # 如果不存在 MY_WEBHOOK_URL 环境变量,则不发送消息 208 | if [ -z "$MY_WEBHOOK_URL" ]; then 209 | yellow "MY_WEBHOOK_URL 环境变量不存在" 210 | return 211 | fi 212 | 213 | # 如果不存在消息内容,则不发送消息 214 | if [ -z "$1" ]; then 215 | return 216 | fi 217 | 218 | local content="$1" 219 | body="{\"content\":\"$content\"}" 220 | run "curl -X POST -H 'Content-Type: application/json' -d '$body' \"$MY_WEBHOOK_URL\"" 221 | } 222 | 223 | read_confirm() { 224 | echo 225 | read -p "$(green "$1")" confrim 226 | # 如果 $2 为 true,则取消 echo,否则打印 227 | [ "$2" = false ] || echo 228 | 229 | case $confrim in 230 | [yY] | [yY][eE][sS]) 231 | return 0 232 | ;; 233 | [nN] | [nN][oO]) 234 | return 1 235 | ;; 236 | *) 237 | return 0 238 | ;; 239 | esac 240 | } 241 | 242 | read_input() { 243 | read -p "$(green "$1")" input 244 | case $input in 245 | "") 246 | input="$2" 247 | ;; 248 | esac 249 | echo $input 250 | } 251 | 252 | read_confirm_and_input() { 253 | read -p "$(green "$1")" confrim 254 | case $confrim in 255 | "" | [yY] | [yY][eE][sS]) 256 | confrim="$2" 257 | ;; 258 | [nN] | [nN][oO]) 259 | confrim="" 260 | ;; 261 | esac 262 | echo $confrim 263 | } 264 | 265 | is_ubuntu() { 266 | [ -f /etc/lsb-release ] && grep -q "DISTRIB_ID=Ubuntu" /etc/lsb-release 267 | } 268 | is_centos() { 269 | [ -f /etc/redhat-release ] && grep -q "CentOS" /etc/redhat-release 270 | } 271 | is_debian() { 272 | [ -f /etc/os-release ] && grep -q "ID=debian" /etc/os-release 273 | } 274 | 275 | print_arg_warn 276 | set_var 277 | --------------------------------------------------------------------------------