From c78fdd468052368990d2c9c6c1c0d58ae2fc8621 Mon Sep 17 00:00:00 2001 From: Teddysun Date: Wed, 21 Dec 2016 14:30:15 +0900 Subject: [PATCH] update comments --- haproxy.sh | 84 ++++++++++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 44 deletions(-) diff --git a/haproxy.sh b/haproxy.sh index 0927e02..4ebb9eb 100644 --- a/haproxy.sh +++ b/haproxy.sh @@ -1,12 +1,15 @@ -#! /bin/bash -PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin -export PATH -#=================================================================# -# System Required: CentOS, Debian, Ubuntu # -# Description: Install haproxy for Shadowsocks server # -# Author: Teddysun # -# Intro: https://shadowsocks.be/10.html # -#=================================================================# +#!/usr/bin/env bash +# +# System Required: CentOS, Debian, Ubuntu +# +# Description: Install haproxy for Shadowsocks server +# +# Author: Teddysun +# +# Intro: https://shadowsocks.be/10.html +# + +[[ $EUID -ne 0 ]] && echo "Error: This script must be run as root!" && exit 1 clear echo @@ -17,27 +20,20 @@ echo "# Author: Teddysun #" echo "#############################################################" echo -rootness(){ - if [[ $EUID -ne 0 ]]; then - echo "Error:This script must be run as root!" 1>&2 - exit 1 - fi -} - checkos(){ - if [[ -f /etc/redhat-release ]];then + if [[ -f /etc/redhat-release ]]; then OS=CentOS - elif cat /etc/issue | grep -q -E -i "debian";then + elif cat /etc/issue | grep -q -E -i "debian"; then OS=Debian - elif cat /etc/issue | grep -q -E -i "ubuntu";then + elif cat /etc/issue | grep -q -E -i "ubuntu"; then OS=Ubuntu - elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat";then + elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then OS=CentOS - elif cat /proc/version | grep -q -E -i "debian";then + elif cat /proc/version | grep -q -E -i "debian"; then OS=Debian - elif cat /proc/version | grep -q -E -i "ubuntu";then + elif cat /proc/version | grep -q -E -i "ubuntu"; then OS=Ubuntu - elif cat /proc/version | grep -q -E -i "centos|red hat|redhat";then + elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then OS=CentOS else echo "Not supported OS, Please reinstall OS and try again." @@ -73,12 +69,22 @@ get_ip(){ [ ! -z ${IP} ] && echo ${IP} || echo } +get_char(){ + SAVEDSTTY=`stty -g` + stty -echo + stty cbreak + dd if=/dev/tty bs=1 count=1 2> /dev/null + stty -raw + stty echo + stty $SAVEDSTTY +} + # Pre-installation settings -function pre_install(){ +pre_install(){ # Set haproxy config port while : do - echo -e "Please input port for haproxy & shadowsocks [1-65535]" + echo -e "Please enter a port for haproxy and Shadowsocks server [1-65535]" read -p "(Default port: 8989):" haproxyport [ -z "${haproxyport}" ] && haproxyport="8989" expr ${haproxyport} + 0 &>/dev/null @@ -91,17 +97,17 @@ function pre_install(){ echo break else - echo "Input error! Please input correct number." + echo "Enter error! Please enter a correct number." fi else - echo "Input error! Please input correct number." + echo "Enter error! Please enter a correct number." fi done # Set haproxy config IPv4 address while : do - echo -e "Please input your shadowsocks IPv4 address for haproxy" + echo -e "Please enter your Shadowsocks server's IPv4 address for haproxy" read -p "(IPv4 is):" haproxyip valid_ip ${haproxyip} if [ $? -eq 0 ]; then @@ -112,19 +118,10 @@ function pre_install(){ echo break else - echo "Input error! Please input correct IPv4 address." + echo "Enter error! Please enter correct IPv4 address." fi done - get_char(){ - SAVEDSTTY=`stty -g` - stty -echo - stty cbreak - dd if=/dev/tty bs=1 count=1 2> /dev/null - stty -raw - stty echo - stty $SAVEDSTTY - } echo echo "Press any key to start...or Press Ctrl+C to cancel" char=`get_char` @@ -134,13 +131,13 @@ function pre_install(){ # Config haproxy config_haproxy(){ # Config DNS nameserver - if ! grep -q "8.8.8.8" /etc/resolv.conf;then + if ! grep -q "8.8.8.8" /etc/resolv.conf; then cp -p /etc/resolv.conf /etc/resolv.conf.bak echo "nameserver 8.8.8.8" > /etc/resolv.conf echo "nameserver 8.8.4.4" >> /etc/resolv.conf fi - if [ -f /etc/haproxy/haproxy.cfg ];then + if [ -f /etc/haproxy/haproxy.cfg ]; then cp -p /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak fi @@ -172,7 +169,7 @@ EOF install(){ # Install haproxy - if [ "${OS}" == 'CentOS' ];then + if [ "${OS}" == 'CentOS' ]; then yum install -y haproxy else apt-get -y update @@ -217,7 +214,7 @@ install(){ echo -e "Your haproxy Server port: \033[41;37m ${haproxyport} \033[0m" echo -e "Your Input Shadowsocks IP: \033[41;37m ${haproxyip} \033[0m" echo - echo "Welcome to visit:https://shadowsocks.be/10.html" + echo "Welcome to visit: https://shadowsocks.be/10.html" echo "Enjoy it." echo } @@ -226,11 +223,10 @@ install(){ # Install haproxy install_haproxy(){ checkos - rootness disable_selinux pre_install install } # Initialization step -install_haproxy 2>&1 | tee /root/haproxy_for_shadowsocks.log +install_haproxy 2>&1 | tee ~/haproxy_for_shadowsocks.log