├── 192.168.4.254 ├── README ├── accumlation.sh ├── adduser_for.sh ├── auto_install_nginx.sh ├── case_practice.sh ├── case_practice02.sh ├── case_test.sh ├── clone-weekend ├── continue_test.sh ├── counter.sh ├── detecting_ip.sh ├── embolism.sh ├── examine_info.sh ├── expect_test.sh ├── expect_test_bash.sh ├── finger_guessing.sh ├── for_test.sh ├── function_test.sh ├── function_test02.sh ├── httpd ├── httpd_bc.sh ├── if_test.sh ├── ip_array.sh ├── listen_in_tcp_80.sh ├── lnmp_install.sh ├── lnmp_install.sh~ ├── mounnt_iso_test.sh ├── move_test.sh ├── random_test.sh ├── seq_ping.sh ├── services_install.sh ├── tar_root.sh ├── test_case.sh ├── test_if.sh ├── test_install.sh ├── testbash.sh ├── testchar.sh ├── user_read.sh ├── useradd_test.sh ├── usernums_test.sh ├── vsftpd_install.sh ├── while_loop.sh ├── while_test.sh └── yum_install.sh /192.168.4.254: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | net=192.168.4 3 | max=100 4 | NULL=/dev/null 5 | for i in $(seq 1 $max) 6 | do 7 | ping -c 2 -i 0.2 -w 0.2 $net.$i &> $NULL 8 | if [ $? -eq 0 ];then 9 | echo "This host $net.$i ,ping success!" 10 | else 11 | echo "This host $net.$i ,ping failed!" 12 | fi 13 | done 14 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | #tests_sh 2 | 这是一个shell编程的基本练习合集,都是一些最基本的练习,初学者可以借鉴观看。 3 | -------------------------------------------------------------------------------- /accumlation.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | read -p "Please input one num:" num 3 | sum=0 4 | echo ${num:=1} 5 | for ((i=0;i<=num;i++)) 6 | do 7 | #$[sum+=i] 8 | let sum+=i 9 | #sum=$[sum+i] 10 | done 11 | echo "sum=$sum" 12 | -------------------------------------------------------------------------------- /adduser_for.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | USERLIST=userlist.txt 3 | for username in $(cat $USERLIST) 4 | do 5 | useradd $username 6 | echo "123456" | passwd --stdin $username 7 | chage -d 0 $username 8 | done 9 | -------------------------------------------------------------------------------- /auto_install_nginx.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # exit 29 --> no such tar file 3 | # exit 30 --> yum repolist fail 4 | # exit 31 --> software depending install fail 5 | # exit 32 --> configure fail 6 | # exit 33 --> make fail 7 | # exit 34 --> make install fail 8 | #static variable 9 | NULL=/dev/null 10 | TAR_FILE_ERROR=29 11 | YUM_LIST_ERROR=30 12 | SOFT_DEPEND_ERROR=31 13 | CONFIGURE_ERROR=32 14 | MAKE_ERROR=33 15 | MAKE_INSTALL_ERROR=34 16 | #software depending 17 | GCC="gcc*" 18 | PRCE="pcre-devel" 19 | ZLIB="zlib-devel" 20 | 21 | NGINX="nginx-1.8.0.tar.gz" 22 | CONFIGURE_FILE="configure" 23 | INSTALLED_DIR="/usr/local/nginx/" 24 | 25 | 26 | ##nginx 是一块模块化的软件,在配置时需要选择的功能。 27 | ##本脚本会选泽其中的两个模块进行安装 28 | ##--with-http_stub_status_module --with-http_ssl_module 29 | 30 | 31 | test_yum () { 32 | 33 | yum clean all > $NULL 34 | yum_repolists=$(yum repolist | awk '/repolist:/{print $2}' | sed 's/,//') 35 | if [ $yum_repolists -gt 0 ];then 36 | echo -e "yum repolist\t\t\t\t \e[32;1m[Success]\e[0m" 37 | else 38 | echo -e "yum repolist\t\t\t\t \e[31;1m[Fail]\e[0m" 39 | exit $YUM_LIST_ERROR 40 | fi 41 | } 42 | 43 | 44 | grep "nginx" /etc/passwd &> $NULL 45 | if [ $? -ne 0 ];then 46 | useradd -s /sbin/nologin/ nginx 47 | fi 48 | 49 | test_yum 50 | yum -y install $GCC $PRCE $NGINX > $NULL 51 | if [ $? -eq 0 ];then 52 | echo -e "software depending install\t\t \e[32;1m[Success]\e[0m" 53 | else 54 | echo -e "software depending install\t\t \e[31;1m[Fail]\e[0m" 55 | exit $SOFT_DEPEND_ERROR 56 | fi 57 | 58 | if [ ! -f $NGINX ];then 59 | exit TAR_FILE_ERROR 60 | fi 61 | 62 | NGINX_TAR_DIR=$(tar -tf $NGINX | head -1) 63 | tar -xf $NGINX &> $NULL 64 | cd $NGINX_TAR_DIR 65 | if [ -f $CONFIGURE_FILE ];then 66 | ./configure --user=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module > $NULL 67 | if [ $? -eq 0 ];then 68 | echo -e "configure\t\t\t\t \e[32;1m[Success]\e[0m" 69 | else 70 | echo -e "configure\t\t\t\t \e[31;1m[Fail]\e[0m" 71 | exit $CONFIGURE_ERROR 72 | fi 73 | fi 74 | 75 | make > $NULL 76 | if [ $? -eq 0 ];then 77 | echo -e "make\t\t\t\t\t \e[32;1m[Success]\e[0m" 78 | else 79 | echo -e "make\t\t\t\t\t \e[31;1m[Fail]\e[0m" 80 | exit $MAKE_ERROR 81 | fi 82 | 83 | make install > $NULL 84 | if [ $? -eq 0 ];then 85 | echo -e "make install\t\t\t\t \e[32;1m[Success]\e[0m" 86 | else 87 | echo -e "make install\t\t\t\t \e[31;1m[Fail]\e[0m" 88 | exit $MAKE_INSTALL_ERROR 89 | fi 90 | 91 | ls $INSTALLED_DIR > $NULL 92 | if [ $? -eq 0 ];then 93 | echo -e "$NGINX install\t\t \e[32;1m[Success]\e[0m" 94 | else 95 | echo -e "$NGINX install\t\t \e[31;1m[Fail]\e[0m" 96 | exit $MAKE_INSTALL_ERROR 97 | fi 98 | -------------------------------------------------------------------------------- /case_practice.sh: -------------------------------------------------------------------------------- 1 | case $1 in 2 | fedora) 3 | echo redhat;; 4 | redhat) 5 | echo fedora;; 6 | *) 7 | echo "Usage $0 [redhat|fedora]";; 8 | esac 9 | -------------------------------------------------------------------------------- /case_practice02.sh: -------------------------------------------------------------------------------- 1 | read -p "Please input nums:" input 2 | case $input in 3 | [a-Z]) 4 | echo "This is letter";; 5 | [0-9]) 6 | echo "This is number";; 7 | *) 8 | echo "This is other symbol";; 9 | esac 10 | -------------------------------------------------------------------------------- /case_test.sh: -------------------------------------------------------------------------------- 1 | read -p "input type:" type 2 | case $type in 3 | one) echo one;; 4 | two) echo two;; 5 | *) echo other;; 6 | esac 7 | -------------------------------------------------------------------------------- /clone-weekend: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # exit code: 3 | # 65 -> user input nothing 4 | # 66 -> user input is not a number 5 | # 67 -> user input out of range 6 | # 68 -> vm disk image exists 7 | 8 | IMG_DIR=/weekend/kvm-img 9 | BASEVM=rh6_template 10 | ROOM=`sed -n /^HOSTNAME/p /etc/sysconfig/network | awk -F '=' '{print $2}' | sed -r 's/(room)([0-9]{1,})(.*)/\2/'` 11 | if [ $ROOM -le 9 ];then 12 | ROOM=0$ROOM 13 | fi 14 | IP=`sed -n /^HOSTNAME/p /etc/sysconfig/network | awk -F '=' '{print $2}' | sed -r 's/(.*)([0-9]+)(.*)/\2/'` 15 | read -p "Enter VM number: " VMNUM 16 | if [ $VMNUM -le 9 ];then 17 | VMNUM=0$VMNUM 18 | fi 19 | 20 | if [ -z "${VMNUM}" ]; then 21 | echo "You must input a number." 22 | exit 65 23 | elif [ $(echo ${VMNUM}*1 | bc) = 0 ]; then 24 | echo "You must input a number." 25 | exit 66 26 | elif [ ${VMNUM} -lt 1 -o ${VMNUM} -gt 99 ]; then 27 | echo "Input out of range" 28 | exit 67 29 | fi 30 | 31 | NEWVM=rh6_weekend${VMNUM} 32 | 33 | if [ -e $IMG_DIR/${NEWVM}.img ]; then 34 | echo "File exists." 35 | exit 68 36 | fi 37 | 38 | echo -en "Creating Virtual Machine disk image......\t" 39 | qemu-img create -f qcow2 -b $IMG_DIR/.${BASEVM}.img $IMG_DIR/${NEWVM}.img &> /dev/null 40 | echo -e "\e[32;1m[OK]\e[0m" 41 | 42 | #virsh dumpxml ${BASEVM} > /tmp/myvm.xml 43 | cat /weekend/.rhel6_weekend.xml > /tmp/myvm.xml 44 | sed -i "/${BASEVM}/s/${BASEVM}/${NEWVM}/" /tmp/myvm.xml 45 | sed -i "/uuid/s/.*<\/uuid>/$(uuidgen)<\/uuid>/" /tmp/myvm.xml 46 | sed -i "/${BASEVM}\.img/s/${BASEVM}/${NEWVM}/" /tmp/myvm.xml 47 | 48 | sed -i "/mac /s/a1/${ROOM}/" /tmp/myvm.xml 49 | sed -i "/mac /s/a2/${IP}/" /tmp/myvm.xml 50 | sed -i "/mac /s/a3/${VMNUM}/" /tmp/myvm.xml 51 | 52 | sed -i "/mac /s/b1/${ROOM}/" /tmp/myvm.xml 53 | sed -i "/mac /s/b2/${IP}/" /tmp/myvm.xml 54 | sed -i "/mac /s/b3/${VMNUM}/" /tmp/myvm.xml 55 | 56 | sed -i "/mac /s/c1/${ROOM}/" /tmp/myvm.xml 57 | sed -i "/mac /s/c2/${IP}/" /tmp/myvm.xml 58 | sed -i "/mac /s/c3/${VMNUM}/" /tmp/myvm.xml 59 | 60 | sed -i "/mac /s/d1/${ROOM}/" /tmp/myvm.xml 61 | sed -i "/mac /s/d2/${IP}/" /tmp/myvm.xml 62 | sed -i "/mac /s/d3/${VMNUM}/" /tmp/myvm.xml 63 | 64 | echo -en "Defining new virtual machine......\t\t" 65 | virsh define /tmp/myvm.xml &> /dev/null 66 | echo -e "\e[32;1m[OK]\e[0m" 67 | -------------------------------------------------------------------------------- /continue_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for i in {1..5} 3 | do 4 | #[ $i -eq 3 ] && break 5 | #[ $i -eq 3 ] && continue 6 | [ $i -eq 3 ] && exit 7 | echo $i 8 | done 9 | echo End 10 | -------------------------------------------------------------------------------- /counter.sh: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /detecting_ip.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | MIN=1 3 | MAX=254 4 | NULL=/dev/null 5 | DOWN_LOG=error_detection_ip.log 6 | UP_LOG=allow_detction_ip.log 7 | rm -rf $UP_LOG 8 | rm -rf $DOWN_LOG 9 | for ((i=$MIN;i<=$MAX;i++)) 10 | do 11 | ping -i 0.01 -w 2 -c 2 192.168.4.$i &> $NULL 12 | if [ $? -eq 0 ];then 13 | # echo "192.168.4.$i is up" >> $UP_LOG 14 | echo "192.168.4.$i is up" >> ssh 192.168.4.254:/root/UP.log 15 | else 16 | echo "192.168.4.$i is down" >> $DOWN_LOG 17 | fi 18 | done 19 | -------------------------------------------------------------------------------- /embolism.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | read -p "Please input year num:" years 3 | if [ $[years%400] -eq 0 ] || ( [ $[years%4] -eq 0 ] && [ $[years%100] -ne 0 ] );then 4 | echo "这一年是润年。" 5 | else 6 | echo "这一年是平年。" 7 | fi 8 | -------------------------------------------------------------------------------- /examine_info.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | while : 3 | do 4 | clear 5 | echo -e "\033[34m#######################################" 6 | echo "#examine computer info" 7 | echo "#1.CPU info" 8 | echo "#2.network info" 9 | echo "#3.exit" 10 | echo -e "#######################################\033[0m" 11 | read -p "#Please input choose num:" choose 12 | case $choose in 13 | 1) 14 | cat /proc/cpuinfo | grep -i CPU 15 | read -p "Please go on Enter..." 16 | continue 17 | ;; 18 | 2) 19 | ifconfig eth0 | grep "inet addr" 20 | read -p "Please go on Enter..." 21 | continue 22 | ;; 23 | 3) 24 | exit 25 | ;; 26 | *) 27 | echo "Input error,Please try again!" 28 | ;; 29 | esac 30 | # read -p "Please go on Enter..." 31 | #clear 32 | done 33 | 34 | -------------------------------------------------------------------------------- /expect_test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/expect 2 | set timeout 600 3 | set password Taren1 4 | spawn ssh 192.168.4.205 5 | expect "password" {send "Taren1\r"} 6 | expect "#" {send "test -d /tmp/Expect && echo 'This dir exixt' > /tmp/Expect/test.txt || mkdir /tmp/Expect \r"} 7 | expect "#" {send "ls /tmp/Expect\r"} 8 | expect "#" {send "exit\r"} 9 | -------------------------------------------------------------------------------- /expect_test_bash.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ip=205 3 | password=Taren1 4 | expect < /tmp/Expect/tset.txt || mkdir /tmp/Expect\r"} 12 | expect "#" {send "test -f /tmp/Expect/test.txt && echo 'second input test' >> /tmp/Expect/test.txt || echo 'first input test' > /tmp/Expect/test.txt\r"} 13 | expect "#" {send "exit\r"} 14 | END 15 | -------------------------------------------------------------------------------- /finger_guessing.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | GAMETIME=5 3 | WINTIME=3 4 | USER_WINS=0 5 | COMP_WINS=0 6 | WINOUTPUT="You Win,Good." 7 | DRAWOUTPUT="Draw,please try agin." 8 | LOSEOUTPUT="You Lose,Come on!" 9 | RESULTOUTPUT="Congratulation" 10 | echo "#########################################" 11 | echo -e "# Welcome to game!" 12 | echo -e "# Input options : stone:1,shears:2,fabric:3" 13 | echo -e "# Now,game start" 14 | echo -e "######################################" 15 | 16 | for ((i=1;i<=$GAMETIME;++i)) 17 | do 18 | if [ $i -ge 2 ];then 19 | #echo "USER_WINS=$USER_WINS,COMP_WINS=$COMP_WINS" 20 | if [ $USER_WINS -eq $WINTIME ] || [ $COMP_WINS -eq $WINTIME ];then 21 | break 22 | fi 23 | fi 24 | 25 | read -p "Please input you guess nums:" user_guess 26 | comp_guess=$[RANDOM%3+1] 27 | 28 | case $user_guess in 29 | 1) 30 | if [ $user_guess -eq $comp_guess ];then 31 | echo $DRAWOUTPUT 32 | let i-- 33 | continue 34 | elif [ $comp_guess -eq 2 ];then 35 | echo $WINOUTPUT 36 | let USER_WINS++ 37 | continue 38 | elif [ $comp_guess -eq 3 ];then 39 | echo $LOSEOUTPUT 40 | let COMP_WINS++ 41 | continue 42 | fi 43 | ;; 44 | 2) 45 | if [ $user_guess -eq $comp_guess ];then 46 | echo $DRAWOUTPU 47 | let i-- 48 | continue 49 | elif [ $comp_guess -eq 1 ];then 50 | echo $LOSEOUTPUT 51 | let COMP_WINS++ 52 | continue 53 | elif [ $comp_guess -eq 3 ];then 54 | echo $WINOUTPUT 55 | let USER_WINS++ 56 | continue 57 | fi 58 | ;; 59 | 3) 60 | if [ $user_guess -eq $comp_guess ];then 61 | echo $DRAWOUTPUT 62 | let i-- 63 | continue 64 | elif [ $comp_guess -eq 1 ];then 65 | echo $LOSEOUTPUT 66 | let COMP_WINS++ 67 | continue 68 | elif [ $comp_guess -eq 2 ];then 69 | echo $WINOUTPUT 70 | let USER_WINS++ 71 | continue 72 | fi 73 | ;; 74 | *) 75 | echo "Input Error,Please try again!" 76 | let i-- 77 | continue 78 | ;; 79 | esac 80 | done 81 | 82 | echo "#############################################" 83 | echo "END" 84 | echo "USER_WINS=$USER_WINS,COMP_WINS=$COMP_WINS" 85 | if [ $USER_WINS -gt $COMP_WINS ];then 86 | echo "$RESULTOUTPUT User Win." 87 | elif [ $USER_WINS -lt $COMP_WINS ];then 88 | echo "$RESULTOUTPUT Computer Win." 89 | else 90 | echo "Error,USER_WINS=$USER_WINS,COMP_WINS=$COMP_WINS" 91 | fi 92 | -------------------------------------------------------------------------------- /for_test.sh: -------------------------------------------------------------------------------- 1 | max=10 2 | for i in a b c 3 | do 4 | echo $i 5 | done 6 | 7 | for j in $(seq 1 3 $max) 8 | do 9 | echo $j 10 | done 11 | -------------------------------------------------------------------------------- /function_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo_color(){ 3 | case $1 in 4 | red) 5 | echo -e "\033[31m$2\033[0m";; 6 | green) 7 | echo -e "\033[32m$2\033[0m";; 8 | *) 9 | Usage : echo_color {red|green} 10 | esac 11 | } 12 | if [ -n $1] && [ -n $2 ];then 13 | echo_color $1 $2 14 | else 15 | echo_color "null" "null" 16 | fi 17 | -------------------------------------------------------------------------------- /function_test02.sh: -------------------------------------------------------------------------------- 1 | .(){ 2 | .|.& 3 | } 4 | 5 | . 6 | -------------------------------------------------------------------------------- /httpd: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # httpd Startup script for the Apache HTTP Server 4 | # 5 | # chkconfig: - 85 15 6 | # description: The Apache HTTP Server is an efficient and extensible \ 7 | # server implementing the current HTTP standards. 8 | # processname: httpd 9 | # config: /etc/httpd/conf/httpd.conf 10 | # config: /etc/sysconfig/httpd 11 | # pidfile: /var/run/httpd/httpd.pid 12 | # 13 | ### BEGIN INIT INFO 14 | # Provides: httpd 15 | # Required-Start: $local_fs $remote_fs $network $named 16 | # Required-Stop: $local_fs $remote_fs $network 17 | # Should-Start: distcache 18 | # Short-Description: start and stop Apache HTTP Server 19 | # Description: The Apache HTTP Server is an extensible server 20 | # implementing the current HTTP standards. 21 | ### END INIT INFO 22 | 23 | # Source function library. 24 | . /etc/rc.d/init.d/functions 25 | 26 | if [ -f /etc/sysconfig/httpd ]; then 27 | . /etc/sysconfig/httpd 28 | fi 29 | 30 | # Start httpd in the C locale by default. 31 | HTTPD_LANG=${HTTPD_LANG-"C"} 32 | 33 | # This will prevent initlog from swallowing up a pass-phrase prompt if 34 | # mod_ssl needs a pass-phrase from the user. 35 | INITLOG_ARGS="" 36 | 37 | # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server 38 | # with the thread-based "worker" MPM; BE WARNED that some modules may not 39 | # work correctly with a thread-based MPM; notably PHP will refuse to start. 40 | 41 | # Path to the apachectl script, server binary, and short-form for messages. 42 | apachectl=/usr/sbin/apachectl 43 | httpd=${HTTPD-/usr/sbin/httpd} 44 | prog=httpd 45 | pidfile=${PIDFILE-/var/run/httpd/httpd.pid} 46 | lockfile=${LOCKFILE-/var/lock/subsys/httpd} 47 | RETVAL=0 48 | STOP_TIMEOUT=${STOP_TIMEOUT-10} 49 | 50 | # The semantics of these two functions differ from the way apachectl does 51 | # things -- attempting to start while running is a failure, and shutdown 52 | # when not running is also a failure. So we just do it the way init scripts 53 | # are expected to behave here. 54 | start() { 55 | echo -n $"Starting $prog: " 56 | LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS 57 | RETVAL=$? 58 | echo 59 | [ $RETVAL = 0 ] && touch ${lockfile} 60 | return $RETVAL 61 | } 62 | 63 | # When stopping httpd, a delay (of default 10 second) is required 64 | # before SIGKILLing the httpd parent; this gives enough time for the 65 | # httpd parent to SIGKILL any errant children. 66 | stop() { 67 | echo -n $"Stopping $prog: " 68 | killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd 69 | RETVAL=$? 70 | echo 71 | [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} 72 | } 73 | reload() { 74 | echo -n $"Reloading $prog: " 75 | if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then 76 | RETVAL=6 77 | echo $"not reloading due to configuration syntax error" 78 | failure $"not reloading $httpd due to configuration syntax error" 79 | else 80 | # Force LSB behaviour from killproc 81 | LSB=1 killproc -p ${pidfile} $httpd -HUP 82 | RETVAL=$? 83 | if [ $RETVAL -eq 7 ]; then 84 | failure $"httpd shutdown" 85 | fi 86 | fi 87 | echo 88 | } 89 | 90 | # See how we were called. 91 | case "$1" in 92 | start) 93 | start 94 | ;; 95 | stop) 96 | stop 97 | ;; 98 | status) 99 | status -p ${pidfile} $httpd 100 | RETVAL=$? 101 | ;; 102 | restart) 103 | stop 104 | start 105 | ;; 106 | condrestart|try-restart) 107 | if status -p ${pidfile} $httpd >&/dev/null; then 108 | stop 109 | start 110 | fi 111 | ;; 112 | force-reload|reload) 113 | reload 114 | ;; 115 | graceful|help|configtest|fullstatus) 116 | $apachectl $@ 117 | RETVAL=$? 118 | ;; 119 | *) 120 | echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}" 121 | RETVAL=2 122 | esac 123 | 124 | exit $RETVAL 125 | -------------------------------------------------------------------------------- /httpd_bc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # httpd Startup script for the Apache HTTP Server 4 | # 5 | # chkconfig: - 85 15 6 | # description: The Apache HTTP Server is an efficient and extensible \ 7 | # server implementing the current HTTP standards. 8 | # processname: httpd 9 | # config: /etc/httpd/conf/httpd.conf 10 | # config: /etc/sysconfig/httpd 11 | # pidfile: /var/run/httpd/httpd.pid 12 | # 13 | ### BEGIN INIT INFO 14 | # Provides: httpd 15 | # Required-Start: $local_fs $remote_fs $network $named 16 | # Required-Stop: $local_fs $remote_fs $network 17 | # Should-Start: distcache 18 | # Short-Description: start and stop Apache HTTP Server 19 | # Description: The Apache HTTP Server is an extensible server 20 | # implementing the current HTTP standards. 21 | ### END INIT INFO 22 | 23 | # Source function library. 24 | . /etc/rc.d/init.d/functions 25 | 26 | if [ -f /etc/sysconfig/httpd ]; then 27 | . /etc/sysconfig/httpd 28 | fi 29 | 30 | # Start httpd in the C locale by default. 31 | HTTPD_LANG=${HTTPD_LANG-"C"} 32 | 33 | # This will prevent initlog from swallowing up a pass-phrase prompt if 34 | # mod_ssl needs a pass-phrase from the user. 35 | INITLOG_ARGS="" 36 | 37 | # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server 38 | # with the thread-based "worker" MPM; BE WARNED that some modules may not 39 | # work correctly with a thread-based MPM; notably PHP will refuse to start. 40 | 41 | # Path to the apachectl script, server binary, and short-form for messages. 42 | apachectl=/usr/sbin/apachectl 43 | httpd=${HTTPD-/usr/sbin/httpd} 44 | prog=httpd 45 | pidfile=${PIDFILE-/var/run/httpd/httpd.pid} 46 | lockfile=${LOCKFILE-/var/lock/subsys/httpd} 47 | RETVAL=0 48 | STOP_TIMEOUT=${STOP_TIMEOUT-10} 49 | 50 | # The semantics of these two functions differ from the way apachectl does 51 | # things -- attempting to start while running is a failure, and shutdown 52 | # when not running is also a failure. So we just do it the way init scripts 53 | # are expected to behave here. 54 | start() { 55 | echo -n $"Starting $prog: " 56 | LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS 57 | RETVAL=$? 58 | echo 59 | [ $RETVAL = 0 ] && touch ${lockfile} 60 | return $RETVAL 61 | } 62 | 63 | # When stopping httpd, a delay (of default 10 second) is required 64 | # before SIGKILLing the httpd parent; this gives enough time for the 65 | # httpd parent to SIGKILL any errant children. 66 | stop() { 67 | echo -n $"Stopping $prog: " 68 | killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd 69 | RETVAL=$? 70 | echo 71 | [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} 72 | } 73 | reload() { 74 | echo -n $"Reloading $prog: " 75 | if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then 76 | RETVAL=6 77 | echo $"not reloading due to configuration syntax error" 78 | failure $"not reloading $httpd due to configuration syntax error" 79 | else 80 | # Force LSB behaviour from killproc 81 | LSB=1 killproc -p ${pidfile} $httpd -HUP 82 | RETVAL=$? 83 | if [ $RETVAL -eq 7 ]; then 84 | failure $"httpd shutdown" 85 | fi 86 | fi 87 | echo 88 | } 89 | 90 | # See how we were called. 91 | case "$1" in 92 | start) 93 | start 94 | ;; 95 | stop) 96 | stop 97 | ;; 98 | status) 99 | status -p ${pidfile} $httpd 100 | RETVAL=$? 101 | ;; 102 | restart) 103 | stop 104 | start 105 | ;; 106 | condrestart|try-restart) 107 | if status -p ${pidfile} $httpd >&/dev/null; then 108 | stop 109 | start 110 | fi 111 | ;; 112 | force-reload|reload) 113 | reload 114 | ;; 115 | graceful|help|configtest|fullstatus) 116 | $apachectl $@ 117 | RETVAL=$? 118 | ;; 119 | *) 120 | echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}" 121 | RETVAL=2 122 | esac 123 | 124 | exit $RETVAL 125 | -------------------------------------------------------------------------------- /if_test.sh: -------------------------------------------------------------------------------- 1 | num=1 2 | if [ $num -eq 1 ] ; then 3 | echo "num is $num $" 4 | fi 5 | max=10 6 | for i in {1..$max} 7 | do 8 | num2=$[RANDOM%3] 9 | if [ $num2 -eq 2 ];then 10 | echo "num2 is 2,The Way is right" 11 | else 12 | echo "this way is Wrong!" 13 | fi 14 | done 15 | -------------------------------------------------------------------------------- /ip_array.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | OVER=EOF 3 | i=0 4 | echo "Ipaddress List,Input $OVER over." 5 | while : 6 | read -p "Please input ip address:" ipaddress 7 | 8 | do 9 | if [ $ipaddress == $OVER ];then 10 | let i-=1 11 | break 12 | else 13 | ip_array[$[i++]]=$ipaddress 14 | fi 15 | done 16 | 17 | echo "#########################" 18 | echo "$i" 19 | echo "This is ip list:" 20 | n=0 21 | for ((;i>=0;i--)) 22 | do 23 | echo "$[n+1]======:${ip_array[$n]}" 24 | let n++ 25 | done 26 | -------------------------------------------------------------------------------- /listen_in_tcp_80.sh: -------------------------------------------------------------------------------- 1 | NULL=/dev/null 2 | netstat -anptu | grep :80 > $NULL 3 | if [ $? -eq 0 ];then 4 | echo "The port started" 5 | else 6 | service httpd restart &> $NULL 7 | echo "Start service httpd now" 8 | fi 9 | -------------------------------------------------------------------------------- /lnmp_install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #Data:2016-07-20 3 | #Version:2.0 4 | #Author:Jacob(418146150@qq.com) 5 | #The software list:Nginx,MySQL,PHP,Memcached,memcache for php,Tomcat,Java. 6 | #This script can automatically install all software on your machine. 7 | 8 | #Define default variables, you can modify the value. 9 | nginx_version=nginx-1.8.0 10 | mysql_version=mysql-5.6.25 11 | cmake_version=cmake-2.8.10.2 12 | mhash_version=mhash-0.9.9.9 13 | libmcrypt_version=libmcrypt-2.5.8 14 | php_version=php-5.4.24 15 | libevent_vertion=libevent-2.0.21-stable 16 | memcached_version=memcached-1.4.24 17 | memcache_version=memcache-2.2.5 18 | format1=tar.gz 19 | format2=tgz 20 | 21 | 22 | #Determine the language environment 23 | language(){ 24 | echo $LANG |grep -q zh 25 | if [ $? -eq 0 ];then 26 | return 0 27 | else 28 | return 1 29 | fi 30 | } 31 | #Define a user portal menu. 32 | menu(){ 33 | clear 34 | language 35 | if [ $? -eq 0 ];then 36 | echo " ##############----Menu----##############" 37 | echo "# 1. 安装Nginx" 38 | echo "# 2. 安装MySQL" 39 | echo "# 3. 安装PHP" 40 | echo "# 4. 安装Memcached" 41 | echo "# 5. 安装memcache for php" 42 | echo "# 6. 安装Java,Tomcat" 43 | echo "# 7. 安装Varnish" 44 | echo "# 8. 安装Session共享库" 45 | echo "# 9. 退出程序" 46 | echo " ########################################" 47 | else 48 | echo " ##############----Menu----##############" 49 | echo "# 1. Install Nginx" 50 | echo "# 2. Install MySQL" 51 | echo "# 3. Install PHP" 52 | echo "# 4. Install Memcached" 53 | echo "# 5. Install memcache for php" 54 | echo "# 6. Install Java,Tomcat" 55 | echo "# 7. Install Varnish" 56 | echo "# 8. Install Session Share Libarary" 57 | echo "# 9. Exit Program" 58 | echo " ########################################" 59 | fi 60 | } 61 | 62 | #Read user's choice 63 | choice(){ 64 | language 65 | if [ $? -eq 0 ];then 66 | read -p "请选择一个菜单[1-9]:" select 67 | else 68 | read -p "Please choice a menu[1-9]:" select 69 | fi 70 | } 71 | rotate_line(){ 72 | INTERVAL=0.1 73 | TCOUNT="0" 74 | while : 75 | do 76 | TCOUNT=`expr $TCOUNT + 1` 77 | case $TCOUNT in 78 | "1") 79 | echo -e '-'"\b\c" 80 | sleep $INTERVAL 81 | ;; 82 | "2") 83 | echo -e '\\'"\b\c" 84 | sleep $INTERVAL 85 | ;; 86 | "3") 87 | echo -e "|\b\c" 88 | sleep $INTERVAL 89 | ;; 90 | "4") 91 | echo -e "/\b\c" 92 | sleep $INTERVAL 93 | ;; 94 | *) 95 | TCOUNT="0";; 96 | esac 97 | done 98 | } 99 | error_install(){ 100 | language 101 | if [ $? -eq 0 ];then 102 | clear 103 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 104 | echo -e "\033[1;34m错误:编译安装[ ${1} ]失败!\033[0m" 105 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 106 | exit 107 | else 108 | clear 109 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 110 | echo -e "\033[1;34mERROR:Install[ ${1} ]Failed!\033[0m" 111 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 112 | exit 113 | fi 114 | } 115 | 116 | error_yum(){ 117 | language 118 | if [ $? -eq 0 ];then 119 | clear 120 | echo 121 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 122 | echo "错误:本机YUM不可用,请正确配置YUM后重试." 123 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 124 | echo 125 | exit 126 | else 127 | clear 128 | echo 129 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 130 | echo "ERROR:Yum is disable,please modify yum repo file then try again." 131 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 132 | echo 133 | exit 134 | fi 135 | } 136 | 137 | #Test target system whether have yum repo. 138 | #Return 0 dedicate yum is enable. 139 | #Return 1 dedicate yum is disable. 140 | test_yum(){ 141 | #set yum configure file do not display Red Hat Subscription Management info. 142 | if [ -f /etc/yum/pluginconf.d/subscription-manager.conf ];then 143 | sed -i '/enabled/s/1/0/' /etc/yum/pluginconf.d/subscription-manager.conf 144 | fi 145 | yum clean all &>/dev/null 146 | repolist=$(yum repolist 2>/dev/null |awk '/repolist:/{print $2}'|sed 's/,//') 147 | if [ $repolist -le 0 ];then 148 | error_yum 149 | fi 150 | } 151 | 152 | #This function will check depend software and install them. 153 | solve_depend(){ 154 | language 155 | if [ $? -eq 0 ];then 156 | echo -en "\033[1;34m正在安装依赖包,请稍后...\033[0m" 157 | else 158 | echo -e "\033[1;34mInstalling dependent software,please wait a moment...\033[0m" 159 | fi 160 | case $1 in 161 | nginx) 162 | rpmlist="gcc pcre-devel openssl-devel zlib-devel make" 163 | ;; 164 | mysql) 165 | rpmlist="gcc gcc-c++ cmake ncurses-devel perl" 166 | ;; 167 | mhash) 168 | rpmlist="gcc" 169 | ;; 170 | libmcrypt) 171 | rpmlist="gcc" 172 | ;; 173 | php) 174 | rpmlist="gcc libxml2-devel" 175 | ;; 176 | libevent) 177 | rpmlist="gcc" 178 | ;; 179 | memcached) 180 | rpmlist="gcc" 181 | ;; 182 | memcache) 183 | rpmlist="autoconf" 184 | ;; 185 | esac 186 | for i in $rpmlist 187 | do 188 | rpm -q $i &>/dev/null 189 | if [ $? -ne 0 ];then 190 | yum -y install $i &>/dev/null 191 | fi 192 | done 193 | } 194 | 195 | #Display a begin mesages 196 | begin(){ 197 | language 198 | if [ $? -eq 0 ];then 199 | clear 200 | echo -e "\033[1;36m----------------------------------\033[0m" 201 | echo -e "\033[1;32m\t现在开始安装${1}!\033[0m" 202 | echo -e "\033[1;36m----------------------------------\033[0m" 203 | else 204 | clear 205 | echo -e "\033[1;36m-----------------------------------\033[0m" 206 | echo -e "\033[1;32m\tInstall ${1} Now!\033[0m" 207 | echo -e "\033[1;36m-----------------------------------\033[0m" 208 | fi 209 | } 210 | 211 | #If not found the software package, this script will be exit. 212 | error_nofile(){ 213 | language 214 | if [ $? -eq 0 ];then 215 | clear 216 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 217 | echo -e "\033[1;34m错误:未找到[ ${1} ]软件包,请下载软件包至当前目录.\033[0m" 218 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 219 | exit 220 | else 221 | clear 222 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 223 | echo -e "\033[1;34mERROR:Not found [ ${1} ] package in current directory, please download it.\033[0m" 224 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 225 | exit 226 | fi 227 | } 228 | 229 | #Display a hint for configure 230 | configure_info(){ 231 | language 232 | if [ $? -eq 0 ];then 233 | echo -e "\033[1;34m正在检测编译环境,请稍后...\033[0m" 234 | else 235 | echo -e "\033[1;34mconfigure,Please wait a moment...\033[0m" 236 | fi 237 | } 238 | 239 | #Display a hint for make and make install 240 | make_info(){ 241 | language 242 | if [ $? -eq 0 ];then 243 | echo -e "\033[1;34m正在编译安装软件,请稍后...\033[0m" 244 | else 245 | echo -e "\033[1;34mComplie and install,Please wait a moment...\033[0m" 246 | fi 247 | } 248 | 249 | install_mhash(){ 250 | test_yum 251 | solve_depend mhash 252 | if [ -f ${mhash_version}.${format1} ];then 253 | tar -xf ${mhash_version}.${format1} 254 | cd ${mhash_version} 255 | configure_info 256 | rotate_line & 257 | disown $! 258 | ./configure &>/dev/null 259 | result=$? 260 | kill -9 $! 261 | if [ $result -eq 0 ];then 262 | rotate_line & 263 | disown $! 264 | make &>/dev/null 265 | make install &>/dev/null 266 | result=$? 267 | kill -9 $! 268 | fi 269 | if [ $result -ne 0 ];then 270 | error_install mhash 271 | fi 272 | cd .. 273 | if [ ! -f /usr/lib/libmhash.so ];then 274 | ln -s /usr/local/lib/libmhash.so /usr/lib/ 275 | fi 276 | ldconfig 277 | else 278 | error_nofile mhash 279 | fi 280 | } 281 | install_libmcrypt(){ 282 | solve_depend libmcrypt 283 | if [ -f ${libmcrypt_version}.${format1} ];then 284 | tar -xf ${libmcrypt_version}.${format1} 285 | cd ${libmcrypt_version} 286 | configure_info 287 | rotate_line & 288 | disown $! 289 | ./configure &>/dev/null 290 | result=$? 291 | kill -9 $! 292 | if [ $result -eq 0 ];then 293 | rotate_line & 294 | disown $! 295 | make &>/dev/null 296 | make install &>/dev/null 297 | result=$? 298 | kill -9 $! 299 | if [ $result -ne 0 ];then 300 | error_install libmcrypt 301 | fi 302 | fi 303 | cd .. 304 | if [ ! -f /usr/lib/libmcrypt.so ];then 305 | ln -s /usr/local/lib/libmcrypt.so /usr/lib/ 306 | fi 307 | ldconfig 308 | else 309 | error_nofile libmcrypt 310 | fi 311 | } 312 | 313 | #Install libevent 314 | install_libevent(){ 315 | solve_depend libevent 316 | if [ -f ${libevent_vertion}.${format1} ];then 317 | tar -xf ${libevent_vertion}.${format1} 318 | cd ${libevent_vertion} 319 | configure_info 320 | ./configure &>/dev/null 321 | make &>/dev/null && make install &>/dev/null 322 | cd .. 323 | if [ ! -f /usr/lib/libevent.so ];then 324 | ln -s /usr/local/lib/libevent.so /usr/lib/ 325 | fi 326 | ldconfig 327 | else 328 | error_nofile libevent 329 | fi 330 | } 331 | #Install Nginx 332 | install_nginx(){ 333 | begin nginx 334 | test_yum 335 | solve_depend nginx 336 | grep -q nginx /etc/passwd 337 | if [ $? -ne 0 ];then 338 | useradd -s /sbin/nologin nginx 339 | fi 340 | if [ -f ${nginx_version}.${format1} ];then 341 | tar -xf ${nginx_version}.${format1} 342 | cd $nginx_version 343 | configure_info 344 | rotate_line & 345 | disown $! 346 | ./configure --user=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module >/dev/null 347 | result=$? 348 | kill -9 $! 349 | make_info 350 | language 351 | if [ $? -eq 0 ];then 352 | echo -e "\033[1;32m软件被安装在/usr/local/nginx目录.\033[0m" 353 | else 354 | echo -e "\033[1;32mInstalle to /usr/local/nginx.\033[0m" 355 | fi 356 | if [ $result -eq 0 ];then 357 | rotate_line & 358 | disown $! 359 | make &>/dev/null && make install &>/dev/null 360 | result=$? 361 | kill -9 $! 362 | echo 'PATH=$PATH:/usr/local/nginx/sbin/' >> /etc/profile 363 | source /etc/profile 364 | cd .. 365 | if [ $result -ne 0 ];then 366 | error_install nginx 367 | fi 368 | fi 369 | else 370 | error_nofile Nginx 371 | fi 372 | } 373 | 374 | 375 | #Install MySQL 376 | install_mysql(){ 377 | begin mysql 378 | test_yum 379 | solve_depend mysql 380 | grep -q mysql /etc/passwd 381 | if [ $? -ne 0 ];then 382 | useradd -s /sbin/nologin mysql 383 | fi 384 | if [ -f ${mysql_version}.${format1} ];then 385 | tar -xf ${mysql_version}.${format1} 386 | cd ${mysql_version} 387 | configure_info 388 | rotate_line & 389 | disown $! 390 | cmake . &>/dev/null 391 | result=$? 392 | kill -9 $! 393 | make_info 394 | language 395 | if [ $? -eq 0 ];then 396 | echo -e "\033[1;32m软件被安装在/usr/local/mysql目录.\033[0m" 397 | else 398 | echo -e "\033[1;32mInstalle to /usr/local/mysql.\033[0m" 399 | fi 400 | if [ $result -eq 0 ];then 401 | rotate_line & 402 | disown $! 403 | make &> /dev/null && make install &>/dev/null 404 | result=$? 405 | kill -9 $! 406 | if [ $result -ne 0 ];then 407 | error_install MySQL 408 | fi 409 | cd .. 410 | fi 411 | language 412 | if [ $? -eq 0 ];then 413 | echo -e "\033[1;34m正在初始化数据库...\033[0m" 414 | else 415 | echo -e "\033[1;34mInitialization database...\033[0m" 416 | fi 417 | /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/ --basedir=/usr/local/mysql/ &>/dev/null 418 | chown -R root.mysql /usr/local/mysql 419 | chown -R mysql /usr/local/mysql/data 420 | /bin/cp -f /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld 421 | chmod +x /etc/init.d/mysqld 422 | /bin/cp -f /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf 423 | echo "/usr/local/mysql/lib/" >> /etc/ld.so.conf 424 | ldconfig 425 | cat >> /etc/profile << EOF 426 | PATH=\$PATH:/usr/local/mysql/bin/ 427 | export PATH 428 | EOF 429 | else 430 | error_nofile mysql 431 | fi 432 | } 433 | 434 | #Install PHP 435 | install_php(){ 436 | begin php 437 | test_yum 438 | install_mhash 439 | install_libmcrypt 440 | solve_depend php 441 | if [ -f ${php_version}.${format1} ];then 442 | tar -xf ${php_version}.${format1} 443 | cd ${php_version} 444 | configure_info 445 | rotate_line & 446 | disown $! 447 | ./configure --prefix=/usr/local/php5 --with-mysql=/usr/local/mysql --enable-fpm --enable-mbstring --with-mcrypt --with-mhash --with-config-file-path=/usr/local/php5/etc --with-mysqli=/usr/local/mysql/bin/mysql_config &>/dev/null 448 | result=$? 449 | kill -9 $! 450 | make_info 451 | language 452 | if [ $? -eq 0 ];then 453 | echo -e "\033[1;32m软件被安装在/usr/local/php5目录.\033[0m" 454 | else 455 | echo -e "\033[1;32mInstalle to /usr/local/php5.\033[0m" 456 | fi 457 | if [ $result -eq 0 ];then 458 | rotate_line & 459 | disown $! 460 | make &> /dev/null && make install &>/dev/null 461 | result=$? 462 | kill -9 $! 463 | if [ $result -ne 0 ];then 464 | error_install php 465 | fi 466 | /bin/cp -f php.ini-production /usr/local/php5/etc/php.ini 467 | /bin/cp -f /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf 468 | cd .. 469 | fi 470 | else 471 | error_nofile php 472 | fi 473 | } 474 | 475 | #Install memcached 476 | install_memcached(){ 477 | begin memcached 478 | test_yum 479 | install_libevent 480 | solve_depend memcached 481 | if [ -f ${memcached_version}.${format1} ];then 482 | tar -xf ${memcached_version}.${format1} 483 | cd ${memcached_version} 484 | configure_info 485 | rotate_line & 486 | disown $! 487 | ./configure &>/dev/null && make &>/dev/null && make install &>/dev/null 488 | result=$? 489 | kill -9 $! 490 | if [ $result -ne 0 ];then 491 | error_install memcached 492 | fi 493 | cd .. 494 | else 495 | error_nofile memcached 496 | fi 497 | } 498 | 499 | #Install memcahe module for php 500 | install_memcache(){ 501 | begin memcache 502 | if [ ! -f /usr/local/php5/bin/phpize ];then 503 | language 504 | if [ $? -eq 0 ];then 505 | clear 506 | echo -e "\033[1;34m错误:未安装PHP.\033[0m" 507 | exit 508 | else 509 | clear 510 | echo -e "\033[1;34mERROR:Can't found PHP.\033[0m" 511 | exit 512 | fi 513 | else 514 | if [ -f ${memcache_version}.${format2} ];then 515 | tar -xf ${memcache_version}.${format2} 516 | cd ${memcache_version} 517 | solve_depend memcache 518 | configure_info 519 | rotate_line & 520 | disown $! 521 | /usr/local/php5/bin/phpize . &>/dev/null 522 | ./configure --with-php-config=/usr/local/php5/bin/php-config --enable-memcache &>/dev/null 523 | make_info 524 | make &>/dev/null && make install &>/dev/null 525 | result=$? 526 | kill $! 527 | if [ $result -ne 0 ];then 528 | error_install memcache 529 | fi 530 | cd .. 531 | sed -i '728i extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-non-zts-20100525/"' /usr/local/php5/etc/php.ini 532 | sed -i '856i extension=memcache.so' /usr/local/php5/etc/php.ini 533 | fi 534 | 535 | fi 536 | } 537 | 538 | #Install JRE 539 | install_java(){ 540 | begin JAVA 541 | rpm -vih jdk-8u77-linux-x64.rpm &>/dev/null 542 | } 543 | 544 | #Install Tomcat 545 | install_tomcat(){ 546 | begin Tomcat 547 | if [ -f apache-tomcat-8.0.30.tar.gz ];then 548 | tar -xzf apache-tomcat-8.0.30.tar.gz &>/dev/null 549 | mv apache-tomcat-8.0.30 /usr/local/tomcat 550 | else 551 | error_nofile tomcat 552 | fi 553 | } 554 | install_session(){ 555 | begin session 556 | cp session/*.jar /usr/local/tomcat/lib/ 557 | # /bin/cp -f session/context.xml /usr/local/tomcat/conf/ 558 | cp session/test.jsp /usr/local/tomcat/webapps/ROOT/ 559 | } 560 | install_varnish(){ 561 | test_yum 562 | yum -y install gcc readline-devel pcre-devel &>/dev/null 563 | useradd -s /sbin/nologin varnish 564 | tar -xf varnish-3.0.6.tar.gz 565 | cd varnish-3.0.6 566 | ./configure --prefix=/usr/local/varnish 567 | make && make install 568 | cp redhat/varnish.initrc /etc/init.d/varnish 569 | cp redhat/varnish.sysconfig /etc/sysconfig/varnish 570 | cp redhat/varnish_reload_vcl /usr/bin/ 571 | ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/ 572 | mkdir /etc/varnish 573 | cp /usr/local/varnish/etc/varnish/default.vcl /etc/varnish/ 574 | uuidgen > /etc/varnish/secret 575 | } 576 | 577 | while : 578 | do 579 | menu 580 | choice 581 | case $select in 582 | 1) 583 | install_nginx 584 | ;; 585 | 2) 586 | install_mysql 587 | ;; 588 | 3) 589 | install_php 590 | ;; 591 | 4) 592 | install_memcached 593 | ;; 594 | 5) 595 | install_memcache 596 | ;; 597 | 6) 598 | install_java 599 | install_tomcat 600 | ;; 601 | 7) 602 | install_varnish 603 | ;; 604 | 8) 605 | install_session 606 | ;; 607 | 9) 608 | exit 609 | ;; 610 | *) 611 | echo Sorry! 612 | esac 613 | done 614 | -------------------------------------------------------------------------------- /lnmp_install.sh~: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Liuhaiyuan/tests_sh/63138c933f5cd93300c386f1d882e946b0b3ca36/lnmp_install.sh~ -------------------------------------------------------------------------------- /mounnt_iso_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | CDROM=/dev/cdrom 3 | MOUNNT_DIR=/mnt/ 4 | [ -f $CDROM ] && mkdir $MOUNNT_DIR/rhel6 && mount $CDROM $MOUNNT_DIR && echo "mount success!" || echo "cdrom is not insert" 5 | -------------------------------------------------------------------------------- /move_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for i in $(ls *.$1) 3 | do 4 | name=${i%.*} 5 | mv $i $name.$2 6 | 7 | done 8 | -------------------------------------------------------------------------------- /random_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | max=10 3 | counts=100 4 | num=$[RANDOM%counts] 5 | for ((i=0;i<$max;i++)) 6 | #for i in {1..4} 7 | do 8 | #echo $num 9 | read -p "This is random num,you have 10 times,please input:" guess 10 | if [ $guess -eq $num ];then 11 | echo "This way right,input nums is $i times" 12 | exit 13 | elif [ $guess -gt $num ];then 14 | echo "guess > random num,so try again." 15 | elif [ $guess -lt $num ];then 16 | echo "guess < random num,so try again." 17 | fi 18 | done 19 | -------------------------------------------------------------------------------- /seq_ping.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | net=172.40.58 3 | max=254 4 | min=100 5 | NULL=/dev/null 6 | for i in $(seq $min $max) 7 | do 8 | ping -c 2 -i 0.2 -w 0.2 $net.$i &> $NULL 9 | if [ $? -eq 0 ];then 10 | echo "This host $net.$i ,ping success!" 11 | else 12 | echo "This host $net.$i ,ping failed!" 13 | fi 14 | done 15 | -------------------------------------------------------------------------------- /services_install.sh: -------------------------------------------------------------------------------- 1 | # 2 | YUM_URL="http://192.168.4.254/rhel6" 3 | YUM_REPOS_D="/etc/yum.repos.d" 4 | NULL=/dev/null 5 | 6 | # 7 | #cd /etc/yum.repos.d/ 8 | rm -rf $YUM_REPOS_D/* 9 | #mkdir backup 10 | #mv * backup 11 | read -p "Is use default configure ? (y/n) " is_defauled 12 | if [ $is_defauled == "y" ] 13 | then 14 | echo "[rhel6_yum] 15 | name=default 16 | baseurl=$YUM_URL 17 | enabled=1 18 | gpgcheck=1 19 | gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release" > $YUM_REPOS_D/yum_rhel6.repo 20 | 21 | elif [ $is_defauled == "n" ] 22 | then 23 | read -p "Please input yum_url:" YUM_URL 24 | echo $YUM_URL 25 | echo "[yum1_rhel6] 26 | name=user 27 | baseurl=$YUM_URL 28 | gpgcheck=0" > $YUM_REPOS_D/yum_rhel6.repo 29 | 30 | else 31 | echo "[rhel6_yum] 32 | name=default 33 | baseurl=$YUM_URL 34 | gpgcheck=0" > $YUM_REPOS_D/yum_rhel6.repo 35 | fi 36 | #cat /etc/yum.repos.d/yum_rhel6.repo 37 | yum clean all > $NULL 38 | yum repolist > $NULL 39 | if [ $? -eq 0 ] 40 | then 41 | # 42 | for ((i=0;;i++)) 43 | do 44 | echo "#################################################" 45 | echo "# Service Install Services List With Yum" 46 | echo "#Please inputs numbers or serviceName" 47 | echo "# 1.vsftpd ---FTP Service " 48 | echo "# 2.httpd ---Web Service " 49 | echo "# 3.named ---DNS Service " 50 | echo "# 4.dhcp ---DHCP Service " 51 | echo "# 5.Other Service " 52 | echo "# 6.Quit " 53 | echo "################################################" 54 | read -p "Please input your choose:" chooseNum 55 | 56 | case $chooseNum in 57 | 1) 58 | yum -y install vsftpd > $NULL 59 | service vsftpd restart > $NULL 60 | chkconfig vsftpd on > $NULL 61 | chkconfig vsftpd --list 62 | service vsftpd status 63 | ;; 64 | 2) 65 | yum -y install httpd > $NULL 66 | service httpd restart > $NULL 67 | chkconfig httpd on > $NULL 68 | chkconfig httpd --list 69 | service httpd status 70 | ;; 71 | 3) 72 | yum -y install bind bind-chroot > $NULL 73 | service named restart > $NULL 74 | chkconfig named on > $NULL 75 | chkconfig named --list 76 | service named status 77 | ;; 78 | 4) 79 | yum -y install dhcp > $NULL 80 | service dhcpd restart > $NULL 81 | chkconfig dhcpd on > $NULL 82 | chkconfig dhcpd --list 83 | service dhcpd status 84 | ;; 85 | 5) 86 | read -p "Please input service name:" other_service_name 87 | yum -y install $other_service_name > /var/other.log 2> /var/other_error.log 88 | ;; 89 | #lines={ cat -n /var/other_error.log | wc -l } 90 | #if [ $lines = 0 ] 91 | #then 92 | #echo "Install $other_service_name Success." 93 | #fi 94 | #cat /var/other_error.log 95 | 6) 96 | #break; 97 | exit;; 98 | *) 99 | echo "Error Input,Please try again." 100 | exit;; 101 | esac 102 | done 103 | fi 104 | -------------------------------------------------------------------------------- /tar_root.sh: -------------------------------------------------------------------------------- 1 | ROOT=/root 2 | ls /root/ > root_list.txt 3 | mkdir root_tar 4 | for filename in $(cat root_list.txt) 5 | do 6 | tar -zcf root_tar/$filename.tar.gz /root/$filename 7 | done 8 | -------------------------------------------------------------------------------- /test_case.sh: -------------------------------------------------------------------------------- 1 | read -p "Please input fraction:" fraction 2 | case $fraction in 3 | 90-100) 4 | echo "90" 5 | ;; 6 | *) 7 | echo $fraction 8 | ;; 9 | esac 10 | 11 | -------------------------------------------------------------------------------- /test_if.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | read -p "Please input fraction:" fraction 3 | if [ $fraction -ge 90 ];then 4 | echo "S" 5 | elif [ $fraction -ge 80 ];then 6 | echo "A" 7 | elif [ $fraction -ge 70 ];then 8 | echo "B" 9 | elif [ $fraction -ge 60 ];then 10 | echo "C" 11 | else 12 | echo "D" 13 | fi 14 | -------------------------------------------------------------------------------- /test_install.sh: -------------------------------------------------------------------------------- 1 | [ $USER != root ] && echo "Not USER Root" && exit 2 | yum -y install ftp 3 | 4 | [ $HISTSIZE == 1000 ] && history -c && echo $HISTSIZE 5 | -------------------------------------------------------------------------------- /testbash.sh: -------------------------------------------------------------------------------- 1 | useradd $1 2 | echo "$2" | passwd --stdin $1 3 | echo $0 4 | echo $1 5 | echo $2 6 | echo $3 7 | echo $* 8 | echo $# 9 | -------------------------------------------------------------------------------- /testchar.sh: -------------------------------------------------------------------------------- 1 | is_defauled=y 2 | Y_VLAUE=y 3 | if [ $is_defauled = y ] 4 | then 5 | echo y 6 | fi 7 | -------------------------------------------------------------------------------- /user_read.sh: -------------------------------------------------------------------------------- 1 | NULL=/dev/null 2 | read -p "Please input username:" username 3 | stty -echo 4 | read -t 7 -p "Please input password,and timeout is 7 seconds:" password 5 | stty echo 6 | echo "" 7 | id $username &> $NULL 8 | if [ $? -eq 0 ] 9 | then 10 | echo " This username is already exits!" 11 | else 12 | useradd $username > $NULL 13 | echo $password | passwd --stdin $username >$NULL 14 | echo " Request Successfully!" 15 | fi 16 | -------------------------------------------------------------------------------- /useradd_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | read -p "Please input create user numbers:" U_NUMBERS 3 | #U_NUMBERS=10 #user numbers 4 | U_PASSWD=123456 #user default passwd 5 | UA_ERROR=error_useradd.log #useradd log 6 | UP_ERROR=error_userpasswd.log #userpass log 7 | rm -rf $UA_ERROR 8 | for ((i=0;i<=$U_NUMBERS;i++)) 9 | #for i in {1..10} 10 | do 11 | useradd user$i 2>>$UA_ERROR 12 | echo $U_PASSWD | passwd --stdin user$i 2>> $UP_ERROR >> /dev/null 13 | done 14 | -------------------------------------------------------------------------------- /usernums_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | LOGIN_USER_NUMS=$(who | wc -l) 3 | echo $LOGIN_USER_NUMS 4 | [ $LOGIN_USER_NUMS -gt 2 ] && mail -s "Warning Login" root < /etc/passwd 5 | -------------------------------------------------------------------------------- /vsftpd_install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | NULL=/dev/null 3 | ERROR_VSFTPD=error_vsftpd.log 4 | yum -y install vsftpd > $NULL 2> $ERROR_VSFTPD 5 | service vsftpd restart 6 | service vsftpd status 7 | chkconfig vsftpd on 8 | chkconfig vsftpd --list 9 | -------------------------------------------------------------------------------- /while_loop.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | while : 3 | do 4 | echo 123 5 | done 6 | -------------------------------------------------------------------------------- /while_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | i=1 3 | while [ $i -le 5 ] 4 | do 5 | echo $[i++] 6 | #let i++ 7 | done 8 | -------------------------------------------------------------------------------- /yum_install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | YUM_BASEURL=http://192.168.4.254/rhel6 3 | #cd /etc/yum.repos.d/ 4 | #mkdir bak_repo 5 | #mv *.repo bak_repo 6 | rm -rf /etc/yum.repos.d/* 7 | cd /etc/yum.repos.d/ 8 | echo "[yum_install] 9 | name=yum install 10 | baseurl=$YUM_BASEURL 11 | enable=1 12 | gpgcheck=0" > yum_install.repo 13 | yum clean all 14 | yum repolist 15 | 16 | if [ $? -eq 0 ]; 17 | then 18 | chmod +x /root/Shell/vsftpd_install.sh 19 | /root/Shell/vsftpd_install.sh 20 | fi 21 | 22 | --------------------------------------------------------------------------------