1...100的求和
非递归:
#!/bin/bash
read num
count=1
sum=0
val=""
ret=""
while [ $count -le $num ]
do
if [ $count -eq $num ];then
val=$count
else
val=${count}'+'
fi
let sum+=count
let count++
ret=${ret}${val}
done
echo ${ret}'='$sum
递归:
#!/bin/bash
read num
function add(){
local val=$1
local count=0
local sum=0
local num=$val
if [ $val -eq 1 ];then
echo 1
return
fi
arr=$count1+$val
let val--
let count=$( add $val )
let sum=$count+$num
echo $sum
}
ret=$(add $num)
echo $ret
结果:
[fbl@www test6_19]$ ./add.sh
100
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100=5050
求一个文件中数的最大最小值
#!/bin/bash
read name
max=0
min=0
count=0
while read line
do
if [ $count -eq 1 ];then
min=$line
fi
(( count++ ))
if [ $max -lt $line ];then
max=$line
fi
if [ $min -gt $line ];then
min=$line
fi
done <$name
echo "max: "$max "min: "$min
斐波那契数列求和
非递归
#!/bin/bash
read num
fib_val[0]=1
fib_val[1]=1
count=0
function fib(){
local val=$1
while [ $count -lt $val ]
do
let fib_val[${count}+2]=${fib_val[${count}+1]}+${fib_val[$count]}
let count++
done
}
fib $num
echo ${fib_val[$count]}
递归:
#!/bin/bash
read num
function fib(){
local val=$1
local num1=0
local num2=0
if [ $val -eq 0 -o $val -eq 1 ];then
echo 1
return
fi
let val--
num1=$( fib $val )
let val--
num2=$( fib $val )
let sum=$num1+$num2
echo $sum
}
|
结果:
[fbl@www test6_19]$ ./fib.sh
1
1
[fbl@www test6_19]$ ./fib.sh
2
2
[fbl@www test6_19]$ ./fib.sh
11
144
字符串截取的应用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/bin/bash
read name
str1=""
str2=""
str3=""
str=""
while read line
do
str1=$(echo $line|cut -c 1-3)
str2=$(echo $line|cut -c 4-6 |tr '[a-z]' '[A-Z]')
str3=$(echo $line|cut -c 7-9)
str="${str3}""${str2}""${str1}"
echo $str
done <$name >file_bak
|
结果:
[fbl@www test6_19]$ cat file1
123abc789
456def789
364rgy532
278bsy682
[fbl@www test6_19]$ vim string.sh
[fbl@www test6_19]$ ./string.sh
file1
[fbl@www test6_19]$ cat file_bak
789ABC123
789DEF456
532RGY364
682BSY278
挂载U盘,卸载U盘
#!/bin/bash
quit(){
clear
echo "***************************************"
echo "* Thank you to use *"
echo "***************************************"
exit 0
}
#load usb
mountusb(){
clear
mkdir /mnt/usb
/sbin/fdisk -l | grep dev/sd
echo -e "Please Enter the device name of usb as above:\c"
read PARAMETER
mount /dev/$ PARAMETER /mnt/usb
}
umountusb(){
clear
umount /mnt/usb
}
display(){
clear
ls -l /mnt/usb
}
copydisktousb(){
clear
echo "Please Enter the filename in disk to usb:\c"
read FILE
echo "coping,please wait..."
cp $FILE /mnt/usb
}
copyusbtodisk(){
clear
echo "Please Enter the filename in usb to disk:\c"
read FILE
echo "coping,please wait..."
cp /mnt/usb/$FILE .
}
clear
while true
do
echo "============================================"
echo "*** UNIX USB MANAGE PROGRAM ***"
echo "============================================"
echo " 1-MOUNT USB "
echo " 2-UMOUNT USB "
echo " 3-DISPLAY USB INFOMATION "
echo " 4-COPY FILE IN DISK TO USB "
echo " 5-COPY FILE IN USB TO DISK "
echo " 0-EXIT "
echo -e "Please enter a choice (0--5):\c"
read CHOICE
case $CHOICE in
1) mountusb ;;
2) umountusb ;;
3) display ;;
4) copydisktousb ;;
5) copyusbtodisk ;;
0) quit ;;
*) echo "invalid choice!! correct choice is (0--5)"
sleep 5
clear ;;
esac
done
简易的学生成绩管理:增删查
#/bin/bash
#score.sh
#color function
colour()
{
case $1 in
black_white)
echo -e "\033[40;37m"
;;
black_green)
echo -e "\033[40;32m"
;;
black_cyan)
echo -e "\033[40;36m"
;;
red_yellow)
echo -e "\033[41;33m"
;;
yellow_blue)
echo -e "\033[43;34m"
;;
*)echo "invalid colour"
;;
esac
}
#search function
search()
{
colour black_white
clear
echo -e "please enter name >>>\c"
read NAME
#wujilu
if [ ! -f ./record ];then
echo"you must have some scores before you can seach!"
sleep 10
clear
return
fi
if [ -z $NAME ];then
echo "you did't enter a name!"
echo -e "please enter name >>>\c"
read NAME
fi
grep -i "$NAME" ./record 2> /dev/null
case $? in
1)echo "name not in record!"
;;
2)echo "you did't enter name to search!"
sleep 10
search
esac
}
#add record
add()
{
clear
echo -e "enter name and score of a record:\c"
if [ ! -f ./record ];then
touch record
fi
read NEWNAME
echo "$NEWNAME">./record
sort -o ./record ./record
}
#delete
delete()
{
clear
echo -e "please enter name >>>c"
read NAME
if [ ! -f ./record ];then
echo "This name is not in record"
else
cp record record.bak
rm -f record
grep -v "$NAME" ./record.bak >record
rm -f record.bak
fi
}
#display record
display()
{
colour black_white
more ./record
sleep 10
}
#edit
edit()
{
vi ./record
}
#help pingmu
help()
{
clear
colour black_cyan
echo "This is a student's record program by unix shell language"
}
#exit
quit()
{
clear
colour black_white
exit
}
clear
while true
do
colour red_yellow
echo "##########################################"
echo "# 1:search a record #"
echo "# 2:add a record #"
echo "# 3:delete a record #"
echo "# 4:display all record #"
echo "# 5:edit record with vi #"
echo "# H:help screen #"
echo "# Q:exit pragma #"
echo "##########################################"
colour black_green
echo -e -n "\tplease enter your choice [1 2 3 4 5 H Q]:
\c"
read CHOICE
case $CHOICE in
1)search
;;
2)add;
;;
3)delete;clear
;;
4)display
;;
5)edit;clear
;;
H)help
;;
Q)quit
;;
*)echo "please enter valid mode"
;;
esac
sleep 10
clear
done
#!/bin/bash
#脚本功能:Nginx1.10.2安装,SVN1.8.4安装,Redis3.0.5安装,Memcached1.4.24安装,Tomcat7.0.73安装,jdk-8u101-linux-x64安装,mysql5.6.21安装
######################################源码包下载链接########################################
#========tomcat链接=========
#http://mirrors.cnnic.cn/apache/tomcat/tomcat-7/v7.0.73/bin/apache-tomcat-7.0.73.tar.gz
#=========redis链接=========
#http://download.redis.io/releases/redis-3.0.5.tar.gz
#=======libevent链接========
#http://down1.chinaunix.net/distfiles/libevent-2.0.20-stable.tar.gz
#=======memcahed链接========
#http://www.memcached.org/files/memcached-1.4.24.tar.gz
#=======pcre链接========
#http://exim.mirror.fr/pcre/pcre-8.38.tar.gz
#=======openssl链接========
#http://distfiles.macports.org/openssl/openssl-1.0.2f.tar.gz
#=======zlib链接========
#http://down1.chinaunix.net/distfiles/zlib-1.2.7.tar.gz
#=======nginx链接========
#http://nginx.org/download/nginx-1.10.2.tar.gz
###########################################################
### 定义变量 ###
###########################################################
#-----------安装包名和安装包文件夹名定义--------------
#===jdk8安装包名和包文件名定义====
JDK_PCAKGE='jdk-8u101-linux-x64.tar.gz'
JDK_DIRECTORY='jdk1.8.0_101'
#===Tomcat7安装包名和包文件名定义===
TOMCAT_PCAKGE='apache-tomcat-7.0.73.tar.gz'
TOMCAT_DIRECTORY='apache-tomcat-7.0.73'
#===redis安装包名和包文件名定义===
REDIS_PCAKGE='redis-3.0.5.tar.gz'
REDIS_DIRECTORY='redis-3.0.5'
#===libevent安装包名和包文件名定义===
LIBEVENT_PCAKGE='libevent-2.0.20-stable.tar.gz'
LIBEVENT_DIRECTORY='libevent-2.0.20-stable'
#===makecache安装包名和包文件名定义===
MAKECACHE_PCAKGE='memcached-1.4.24.tar.gz'
MAKECACHE_DIRECTORY='memcached-1.4.24'
#===pcre安装包名和包文件名定义===
PCRE_PCAKGE='pcre-8.38.tar.gz'
PCRE_DIRECTORY='pcre-8.38'
#===openssl安装包名和包文件名定义===
OPENSSL_PCAKGE='openssl-1.0.2f.tar.gz'
OPENSLL_DIRECTORY='openssl-1.0.2f'
#===zlib安装包名和包文件名定义===
ZLIB_PCAKGE='zlib-1.2.8.tar.gz'
ZLIB_DIRECTORY='zlib-1.2.8'
#===nginx安装包名和包文件名定义===
NGINX_PCAKGE='nginx-1.10.2.tar.gz'
NGINX_DIRECTORY='nginx-1.10.2'
#===svn.repo文件名===
SVN_PACKAGE='wandisco-svn.repo'
#===mysql安装包名定义===
MYSQLSERVER_PCAKGE='MySQL-server-5.6.21-1.rhel5.x86_64.rpm'
MYSQLDEVEL_PCAKGE='MySQL-devel-5.6.21-1.rhel5.x86_64.rpm'
MYSQLCLIENT_PCAKGE='MySQL-client-5.6.21-1.rhel5.x86_64.rpm'
#-------------颜色定义-------------------------
BLUEH="\033[36m"
TAILS="\033[0m"
OUTPUT="echo -e"
RED="\033[31m"
GREEN="\033[32m"
GREEN="\033[32m"
FLICKER="\033[05m"
#-------------------路径与文件名定义-----------------
FILE='file'
DIR=`dirname $0`
LOG_NAME='install.log'
PACKAGE_PATH=`cd $DIR && pwd`
FILE_PATH=$PACKAGE_PATH/$FILE
INSTALL_LOG=$FILE_PATH/$LOG_NAME
#---------------------安装包下载地址------------------
INURL='http://192.168.1.1:8001'
OUTURL='http://106.1.1.1:8001/'
URL=$OUTURL
#-----------检测与创建定义的文件、目录----------------
[ -d $FILE_PATH ] || mkdir $FILE_PATH
> $INSTALL_LOG
#------------------服务器IP地址定义------------------
IP=`ifconfig | grep 'inet addr' | head -1 | awk -F':' '{print $2}' | awk '{print $1}'`
#--------------------加载函数库---------------------
. /etc/init.d/functions
###########################输出提示信息############################
echo ''
echo ''
echo ''
echo "###########################################################"
echo "### ###"
echo "### 《迷糊岛》服务端运行环境 一键安装脚本 ###"
echo "### ###"
echo "###########################################################"
echo ''
###########################################################
### 定义函数 ###
###########################################################
#下载Yum
function yumInstall {
#安装编译依赖包
yum -y install gcc gcc-c++ wget elink tar
if [ $? -eq 1 ];then
#检查网络
$OUTPUT --------------------$BLUEH【 正在检查网络,请稍候 】$TAILS--------------------
echo
echo "#Yum安装" >> $INSTALL_LOG
ping www.baidu.com -c 3 &> /dev/null
[ $? -eq 0 ] && echo "网络正常" >> $INSTALL_LOG || {
action "你的网络不通,请修复你的网络,然后再进行安装,脚本退出" /bin/false
echo "@你的网络不通,请修复你的网络,然后再进行安装,脚本在此步退出" >> $INSTALL_LOG
exit 2
}
[ -d /etc/yum.repos.d/other ] || mkdir /etc/yum.repos.d/other
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/other
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
[ $? -eq 0 ] && echo "yum源下载成功" >> $INSTALL_LOG || {
action "yum源下载失败,脚本退出" /bin/false
echo "@yum源下载失败,脚本在此步退出" >> $INSTALL_LOG
exit 2
}
echo >> $INSTALL_LOG
echo >> $INSTALL_LOG
yum clean all
yum makecache
fi
}
#Svn安装
function svnInstall {
echo "############# Svn1.8.4安装 #############" >> $INSTALL_LOG
wget -c -P /etc/yum.repos.d/ $URL$SVN_PACKAGE
[ $? -eq 0 ] && echo "svn包下载成功" >> $INSTALL_LOG || echo "svn包下载失败" >> $INSTALL_LOG
yum clean all
yum -y install subversion subversion-devel
if [ $? -eq 1 ];then
yum -y install subversion subversion-devel
[ $? -eq 0 ] && echo "svn安装成功" >> $INSTALL_LOG || echo "svn安装失败" >> $INSTALL_LOG
else
echo "svn安装成功" >> $INSTALL_LOG
fi
echo >> $INSTALL_LOG
echo >> $INSTALL_LOG
sleep 2
}
#Jdk安装
function jdkInstall {
echo "############# Jdk8u101安装 #############" >> $INSTALL_LOG
wget -c -P $FILE_PATH $URL$JDK_PCAKGE
[ $? -eq 0 ] && echo "jdk包下载成功" >> $INSTALL_LOG || echo "@jdk包下载失败" >> $INSTALL_LOG
cd $FILE_PATH && tar xf $JDK_PCAKGE
[ $? -eq 0 ] && echo "jdk包解压成功" >> $INSTALL_LOG || echo "@jdk包解压失败" >> $INSTALL_LOG
[ -d /usr/local/jdk ] && rm -rf /usr/local/jdk
mv $JDK_DIRECTORY /usr/local/jdk
[ $? -eq 0 ] && echo "jdk目录移动成功" >> $INSTALL_LOG || echo "@jdk目录移动失败" >> $INSTALL_LOG
echo 'export JRE_HOME=/usr/local/jdk' >> /etc/profile
echo 'export JAVA_BIN=/usr/local/jdk/bin' >> /etc/profile
echo 'export PATH=$JRE_HOME/bin:$PATH' >> /etc/profile
echo 'export CLASSPATH=.:$JRE_HOME/lib/dt.jar:$JRE_HOME/lib/tools.jar' >> /etc/profile
echo 'export JRE_HOME JAVA_BIN PATH CLASSPATH' >> /etc/profile
. /etc/profile
echo >> $INSTALL_LOG
echo >> $INSTALL_LOG
}
#Tomcat安装
function tomcatAllInstall {
function tomcat_install {
echo "############# Tomcat7.0.73安装 #############" >> $INSTALL_LOG
wget -c -P $FILE_PATH $URL$TOMCAT_PCAKGE
[ $? -eq 0 ] && echo "tomcat包下载成功" >> $INSTALL_LOG || echo "@tomcat下载失败" >> $INSTALL_LOG
cd $FILE_PATH && tar zxf $TOMCAT_PCAKGE
[ $? -eq 0 ] && echo "tomcat包解压成功" >> $INSTALL_LOG || echo "@tomcat包解压失败" >> $INSTALL_LOG
[ -d /usr/local/tomcat ] && rm -rf /usr/local/tomcat
cp -r $TOMCAT_DIRECTORY /usr/local/tomcat
[ $? -eq 0 ] && echo "tomcat目录拷贝成功" >> $INSTALL_LOG || echo "@tomcat目录拷贝失败" >> $INSTALL_LOG
chmod +x /usr/local/tomcat/bin/*.sh
[ $? -eq 0 ] && echo "tomcat的sh文件添加权限成功" >> $INSTALL_LOG || echo "@tomcat的sh文件添加权限失败" >> $INSTALL_LOG
#/usr/local/tomcat/bin/startup.sh
#[ $? -eq 0 ] && echo "tomcat启动成功" >> $INSTALL_LOG || echo "@tomcat启动失败" >> $INSTALL_LOG
echo >> $INSTALL_LOG
echo >> $INSTALL_LOG
}
tomcat_install
}
#Redis安装
function redisInstall {
echo "############# Redis3.0.5安装 ############# " >> $INSTALL_LOG
wget -c -P $FILE_PATH $URL$REDIS_PCAKGE
[ $? -eq 0 ] && echo "redis包下载成功" >> $INSTALL_LOG || echo "@redis下载失败" >> $INSTALL_LOG
cd $FILE_PATH && tar xf $REDIS_PCAKGE
[ $? -eq 0 ] && echo "redis包解压成功" >> $INSTALL_LOG || echo "@redis包解压失败" >> $INSTALL_LOG
cd $REDIS_DIRECTORY
[ $? -eq 0 ] && echo "redis进入目录成功" >> $INSTALL_LOG || echo "@redis进入目录失败" >> $INSTALL_LOG
make MALLOC=libc
[ $? -eq 0 ] && cd ..;echo "redis包配置成功" >> $INSTALL_LOG || echo "@redis包配置失败" >> $INSTALL_LOG
cd $FILE_PATH/$REDIS_DIRECTORY
[ -d /usr/local/redis ] && rm -rf /usr/local/redis
make PREFIX=/usr/local/redis install
[ $? -eq 0 ] && echo "redis包编译成功" >> $INSTALL_LOG || echo "@redis包编译失败" >> $INSTALL_LOG
echo 'PATH=/usr/local/redis/bin:$PATH' >> /etc/profile
. /etc/profile
[ -d /usr/local/redis/conf ] || mkdir /usr/local/redis/conf
cp redis.conf /usr/local/redis/conf/
[ $? -eq 0 ] && echo "redis.conf配置文件拷贝成功" >> $INSTALL_LOG || echo "@redis.conf配置文件拷贝失败" >> $INSTALL_LOG
#sed -i "46s#/var/run/redis.pid#/usr/local/redis/conf/redis.pid#" /usr/local/redis/conf/redis.conf
#[ $? -eq 0 ] && echo "redis.pid修改路径成功" >> $INSTALL_LOG || echo "@redis.pid修改路径失败" >> $INSTALL_LOG
sed -i "192s#./#/usr/local/redis/conf/#" /usr/local/redis/conf/redis.conf
[ $? -eq 0 ] && echo "redis.conf修改路径成功" >> $INSTALL_LOG || echo "@redis.conf修改路径失败" >> $INSTALL_LOG
echo vm.overcommit_memory = 1 >> /etc/sysctl.conf
#nohup redis-server /usr/local/redis/conf/redis.conf &> /dev/null &
#[ $? -eq 0 ] && echo "redis启动成功" >> $INSTALL_LOG || echo "@redis启动失败" >> $INSTALL_LOG
echo >> $INSTALL_LOG
echo >> $INSTALL_LOG
}
#Memcached安装
function memAllInstall {
#安装libevent包
function libevent_install {
echo "############# Memcached1.4.24安装 #############" >> $INSTALL_LOG
wget -c -P $FILE_PATH $URL$LIBEVENT_PCAKGE
[ $? -eq 0 ] && echo "libevent包下载成功" >> $INSTALL_LOG || echo "@libevent下载失败" >> $INSTALL_LOG
cd $FILE_PATH && tar zxf $LIBEVENT_PCAKGE
[ $? -eq 0 ] && echo "libevent包解压成功" >> $INSTALL_LOG || echo "@bevent包解压失败" >> $INSTALL_LOG
cd $FILE_PATH/$LIBEVENT_DIRECTORY
[ $? -eq 0 ] && echo "libevent进入目录成功" >> $INSTALL_LOG || echo "@libevent进入目录失败" >> $INSTALL_LOG
[ -d /usr/local/libevent ] && rm -rf /usr/local/libevent
./configure #默认装在/usr/local/lib目录里
[ $? -eq 0 ] && echo "libevent包配置成功" >> $INSTALL_LOG || echo "@libevent包配置失败" >> $INSTALL_LOG
make
[ $? -eq 0 ] && echo "libevent包编译成功" >> $INSTALL_LOG || echo "@libevent包编译失败" >> $INSTALL_LOG
make install
[ $? -eq 0 ] && echo "libevent包安装成功" >> $INSTALL_LOG || echo "@libevent包安装失败" >> $INSTALL_LOG
echo "/usr/local/lib" > /etc/ld.so.conf.d/libevent.conf
[ $? -eq 0 ] && echo "/usr/local/lib重定向成功" >> $INSTALL_LOG || echo "@/usr/local/lib重定向失败" >> $INSTALL_LOG
ldconfig
[ $? -eq 0 ] && echo "ldconfig成功" >> $INSTALL_LOG || echo "@ldconfig失败" >> $INSTALL_LOG
echo >> $INSTALL_LOG
}
#安装memcached包
function memcached_install {
wget -c -P $FILE_PATH $URL$MAKECACHE_PCAKGE
[ $? -eq 0 ] && echo "memcached包下载成功" >> $INSTALL_LOG || echo "@memcached下载失败" >> $INSTALL_LOG
cd $FILE_PATH && tar zxf $MAKECACHE_PCAKGE
[ $? -eq 0 ] && echo "memcached包解压成功" >> $INSTALL_LOG || echo "@memcached包解压失败" >> $INSTALL_LOG
cd $FILE_PATH/$MAKECACHE_DIRECTORY
[ $? -eq 0 ] && echo "memcached进入目录成功" >> $INSTALL_LOG || echo "@memcached进入目录失败" >> $INSTALL_LOG
[ -d /usr/local/memcached ] && rm -rf /usr/local/memcached
./configure --prefix=/usr/local/memcached
[ $? -eq 0 ] && echo "memcached包配置成功" >> $INSTALL_LOG || echo "@memcached包配置失败" >> $INSTALL_LOG
make
[ $? -eq 0 ] && echo "memcached包编译成功" >> $INSTALL_LOG || echo "@memcached包编译失败" >> $INSTALL_LOG
make install
[ $? -eq 0 ] && echo "memcached包安装成功" >> $INSTALL_LOG || echo "@memcached包安装失败" >> $INSTALL_LOG
#/usr/local/memcached/bin/memcached -l $IP -u root -c 200 -m 500M -n 10 -f 2 -d
#[ $? -eq 0 ] && echo "memcached启动成功" >> $INSTALL_LOG || echo "@memcached启动失败" >> $INSTALL_LOG
echo >> $INSTALL_LOG
echo >> $INSTALL_LOG
}
libevent_install
memcached_install
}
#Nginx安装
function nginxInstall {
echo "############# Nginx1.10.2安装 #############" >> $INSTALL_LOG
id nginx &> /dev/null || useradd nginx
#pcre包下载解压
wget -c -P $FILE_PATH $URL$PCRE_PCAKGE
[ $? -eq 0 ] && echo "pcre包下载成功" >> $INSTALL_LOG || echo "@pcre下载失败" >> $INSTALL_LOG
cd $FILE_PATH && tar xf $PCRE_PCAKGE -C /usr/local
[ $? -eq 0 ] && echo "pcre包解压成功" >> $INSTALL_LOG || echo "@pcre包解压失败" >> $INSTALL_LOG
#openssl包下载解压
wget -c -P $FILE_PATH $URL$OPENSSL_PCAKGE
[ $? -eq 0 ] && echo "openssl包下载成功" >> $INSTALL_LOG || echo "@openssl下载失败" >> $INSTALL_LOG
tar xf $OPENSSL_PCAKGE -C /usr/local
[ $? -eq 0 ] && echo "openssl包解压成功" >> $INSTALL_LOG || echo "@openssl包解压失败" >> $INSTALL_LOG
#zlib包下载解压
wget -c -P $FILE_PATH $URL$ZLIB_PCAKGE
[ $? -eq 0 ] && echo "zlib包下载成功" >> $INSTALL_LOG || echo "@zlib下载失败" >> $INSTALL_LOG
tar xf $ZLIB_PCAKGE -C /usr/local
[ $? -eq 0 ] && echo "zlib包解压成功" >> $INSTALL_LOG || echo "@zlib包解压失败" >> $INSTALL_LOG
#nginx包下载解压
wget -c -P $FILE_PATH $URL$NGINX_PCAKGE
[ $? -eq 0 ] && echo "nginx包下载成功" >> $INSTALL_LOG || echo "@nginx下载失败" >> $INSTALL_LOG
tar -zxf $NGINX_PCAKGE
[ $? -eq 0 ] && echo "nginx包解压成功" >> $INSTALL_LOG || echo "@nginx包解压失败" >> $INSTALL_LOG
#进入目录
cd $FILE_PATH/$NGINX_DIRECTORY
[ $? -eq 0 ] && echo "nginx进入目录成功" >> $INSTALL_LOG || echo "@nginx进入目录失败" >> $INSTALL_LOG
#删除旧目录
[ -d /usr/local/nginx ] && rm -rf /usr/local/nginx
#配置
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-http_slice_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module \
--with-ipv6 \
--with-pcre=/usr/local/$PCRE_DIRECTORY \
--with-zlib=/usr/local/$ZLIB_DIRECTORY \
--with-openssl=/usr/local/$OPENSLL_DIRECTORY
[ $? -eq 0 ] && echo "Nginx配置成功" >> $INSTALL_LOG || echo "@Nginx配置失败" >> $INSTALL_LOG
#编译
cd $FILE_PATH/nginx-1.10.2
make
[ $? -eq 0 ] && echo "Nginx编译成功" >> $INSTALL_LOG || echo "@Nginx包编译失败" >> $INSTALL_LOG
#安装
cd $FILE_PATH/nginx-1.10.2
make install
[ $? -eq 0 ] && echo "nginx安装成功" >> $INSTALL_LOG || echo "@Nginx安装失败" >> $INSTALL_LOG
#w创建目录
[ -d /var/cache/nginx ] || mkdir /var/cache/nginx -p
#启动服务
#/usr/local/nginx/sbin/nginx
#[ $? -eq 0 ] && echo "Nginx启动成功" >> $INSTALL_LOG || echo "@Nginx启动失败" >> $INSTALL_LOG
echo >> $INSTALL_LOG
echo >> $INSTALL_LOG
}
function mysqlInstall {
echo "############# MySQL5.6.21rpm包安装 ############# " >> $INSTALL_LOG
#删除服务器的旧MySQL数据库
rpm -qa | grep mysql-libs && yum -y remove mysql-libs
rpm -qa | grep mysql-server && yum -y remove mysql-server
rpm -qa | grep mysql-client && yum -y remove mysql-client
-ech
#安装MySQL5.6.21
cd $FILE_PATH
wget -c -P $FILE_PATH $URL{$MYSQLSERVER_PCAKGE,$MYSQLDEVEL_PCAKGE,$MYSQLCLIENT_PCAKGE}
[ $? -eq 0 ] && echo "mysql-server,mysql-devel,mysql-client包下载成功" >> $INSTALL_LOG || echo "mysql-server,mysql-devel,mysql-client包下载失败" >> $INSTALL_LOG
yum -y install $MYSQLSERVER_PCAKGE $MYSQLDEVEL_PCAKGE $MYSQLCLIENT_PCAKGE
[ $? -eq 0 ] && echo "mysql-server,mysql-devel,mysql-client安装成功" >> $INSTALL_LOG || echo "@mysql-server,mysql-devel,mysql-client安装失败" >> $INSTALL_LOG
[ -f /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.old
mv /usr/my.cnf /etc/my.cnf
[ $? -eq 0 ] && echo "my.cnf移动成功" >> $INSTALL_LOG || echo "my.cnf移动失败" >> $INSTALL_LOG
service mysql start
[ $? -eq 0 ] && echo "服务启动成功" >> $INSTALL_LOG || echo "mysql服务启动失败" >> $INSTALL_LOG
####################################################MySQL5.6注意事项################################################################
#cat /root/.mysql_secret #初次安装好MySQL的密码
#mysql -uroot -p输入初次安装好MySQL的密码 #登陆MySQL
#update mysql.user set password=password('123') where host='localhost' and user='root'; #需要先修改好密码才能执行sql命令
}
#检查服务
function checkService {
#Svn检查
echo
echo "###################################################"
svn --version &> /dev/null && $OUTPUT " " $GREEN Svn安装成功 $TAILS " " || $OUTPUT " " $RED$FLICKER Svn安装失败,请检查 $TAILS " "
echo "###################################################"
#Redis检查
echo
echo
echo "###################################################"
[ -f /usr/local/redis/conf/redis.conf ] && $OUTPUT " " $GREEN Redis安装成功 $TAILS " " || $OUTPUT " " $RED$FLICKER Redis安装失败,请检查 $TAILS " "
ss -tlnp | grep 6379 &> /dev/null && $OUTPUT " " $GREEN Redis已经启动 $TAILS " " || $OUTPUT " " $RED$FLICKER Redis没有启动,请启动 $TAILS " "
echo "###################################################"
#Memcached检查
echo
echo
echo "###################################################"
[ -d /usr/local/memcached/ ] && $OUTPUT " " $GREEN Memcached安装成功 $TAILS " " || $OUTPUT " " $RED$FLICKER Memcached安装失败,请检查 $TAILS " "
ss -tlnp | grep 11211 &> /dev/null && $OUTPUT " " $GREEN Memcached已经启动 $TAILS " " || $OUTPUT " " $RED$FLICKER Memcached没有启动,请启动 $TAILS " "
echo "###################################################"
#Tomcat检查
echo
echo
echo "###################################################"
[ -d /usr/local/tomcat/ ]&& $OUTPUT " " $GREEN Tomcat安装成功 $TAILS " " || $OUTPUT " " $RED$FLICKER tomcat安装失败,请检查 $TAILS " "
ss -tlnp | grep 8005 &> /dev/null && $OUTPUT " " $GREEN Tomcat已经启动 $TAILS " " || $OUTPUT " " $RED$FLICKER Tomcat没有启动,请启动 $TAILS " "
echo "###################################################"
#Nginx检查
echo
echo
echo "###################################################"
[ -d /usr/local/nginx/ ] && $OUTPUT " " $GREEN Nginx安装成功 $TAILS " " || $OUTPUT " " $RED$FLICKER Nginx安装失败,请检查 $TAILS " "
ss -tlnp | grep nginx &> /dev/null && $OUTPUT " " $GREEN Nginx已经启动 $TAILS " " || $OUTPUT " " $RED$FLICKER Nginx没有启动,请启动 $TAILS " "
echo "###################################################"
#MySQL检查
echo
echo
echo "###################################################"
[ -f /etc/my.cnf ] && $OUTPUT " " $GREEN MySQL安装成功 $TAILS " " || $OUTPUT " " $RED$FLICKER MySQL安装失败,请检查 $TAILS " "
ss -tlnp | grep mysql &> /dev/null && $OUTPUT " " $GREEN MySQL已经启动 $TAILS " " || $OUTPUT " " $RED$FLICKER MySQL没有启动,请启动 $TAILS " "
echo "###################################################"
}
###########################################################
### 安装函数调用 ###
###########################################################
yumInstall
svnInstall
jdkInstall
tomcatAllInstall
redisInstall
memAllInstall
nginxInstall
mysqlInstall
checkService
!/bin/bash
if [ $# -lt 1 ];then
echo "Usage: $0 play|www|server|fenquapp|globalapp|battle|db_job|report_job|pay_job|bgm_server|router"
fi
case $1 in
play)
ansible -m shell -a "cd /data/site/${1};svn revert -R ./;svn up" dx1sock1
ansible -m shell -a "cd /data/site/${1};svn revert -R ./;svn up" dx1sock2
ansible -m shell -a ". /etc/profile;sh /home/autoconfig/autoconfig.sh /home/dx1/${1}" dx1sock1
ansible -m shell -a ". /etc/profile;sh /home/autoconfig/autoconfig.sh /home/dx1/${1}" dx1sock2
www)
for i in dx1 dx2 dx3 dx4 dx5 lt1 lt2 lt3 360 179 9377
do
ansible -m shell -a "cd /data/site/${1};svn revert -R ./;svn up" ${i}sock1
ansible -m shell -a "cd /data/site/${1};svn revert -R ./;svn up" ${i}sock2
ansible -m shell -a ". /etc/profile;sh /home/autoconfig/autoconfig.sh /home/${i}/${1}" ${i}sock1
ansible -m shell -a ". /etc/profile;sh /home/autoconfig/autoconfig.sh /home/${i}/${1}" ${i}sock2
done
server)
for i in dx1 dx2 dx3 dx4 dx5 lt1 lt2 lt3 360 179 9377
do
ansible -m shell -a "cd /home/${1};svn revert -R ./;svn up" ${i}sock1
ansible -m shell -a "cd /home/${1};svn revert -R ./;svn up" ${i}sock2
ansible -m shell -a ". /etc/profile;sh /home/autoconfig/autoconfig.sh /home/${i}/${1}" ${i}sock1
ansible -m shell -a ". /etc/profile;sh /home/autoconfig/autoconfig.sh /home/${i}/${1}" ${i}sock2
done
fenquapp)
for i in dx1 dx2 dx3 dx4 dx5 lt1 lt2 lt3 360 179 9377
do
ansible -m shell -a "cd /home/appserver;svn revert -R ./;svn up" ${i}appserver
ansible -m shell -a ". /etc/profile;sh /home/autoconfig/autoconfig.sh /home/${i}/appserver" ${i}appserver
done
db_job)
for i in dx1 dx2 dx3 dx4 dx5 lt1 lt2 lt3 360 179 9377
do
ansible -m shell -a "cd /data/${1};svn revert -R ./;svn up" ${i}sock2
ansible -m shell -a ". /etc/profile;sh /home/autoconfig/autoconfig.sh /home/${i}/${1}" ${i}sock2
done
report_job)
for i in dx1 dx2 dx3 dx4 dx5 lt1 lt2 lt3 360 179 9377
do
ansible -m shell -a "cd /data/${1};svn revert -R ./;svn up" ${i}sock2
ansible -m shell -a ". /etc/profile;sh /home/autoconfig/autoconfig.sh /home/${i}/${1}" ${i}sock2
done
pay_job)
for i in dx1 dx2 dx3 dx4 dx5 lt1 lt2 lt3 360 179 9377
do
ansible -m shell -a "cd /data/${1};svn revert -R ./;svn up" ${i}sock2
ansible -m shell -a ". /etc/profile;sh /home/autoconfig/autoconfig.sh /home/${i}/${1}" ${i}sock2
done
router)
for i in dx1 dx2 dx3 dx4 dx5 lt1 lt2 lt3 360 179 9377
do
ansible -m shell -a "cd /home/${1};svn revert -R ./;svn up" ${i}sock2
ansible -m shell -a ". /etc/profile;sh /home/autoconfig/autoconfig.sh /home/${i}/${1}" ${i}sock2
done
bgm_server)
for i in dx1 dx2 dx3 dx4 dx5 lt1 lt2 lt3 360 179 9377
do
ansible -m shell -a "cd /data/site/pjb_bgmv2;svn revert -R ./;svn up" ${i}sock2
ansible -m shell -a ". /etc/profile;sh /home/autoconfig/autoconfig.sh /home/${i}/bgm_server" ${i}sock2
done
global)
ansible -m shell -a "cd /home/autoconfig;svn revert -R ./;svn up" globalappserver
ansible -m shell -a "cd /home/global;svn revert -R ./;svn up" globalappserver
ansible -m shell -a "cd /home/appserver;svn revert -R ./;svn up" globalappserver
ansible -m shell -a ". /etc/profile;sh /home/autoconfig/autoconfig.sh /home/global/appserver" globalappserver
battle)
ansible -m shell -a "cd /home/autoconfig;svn revert -R ./;svn up" phybattleall
ansible -m shell -a "cd /home/battleserver;svn revert -R ./;svn up" phybattleall
for i in {1..10}
do
ansible -m shell -a "cd /home/phybattle${i};svn revert -R ./;svn up" phybattle${i}
ansible -m shell -a ". /etc/profile;sh /home/autoconfig/autoconfig.sh /home/phybattle${i}" phybattle${i}
done
esac