shell基本案例

一、自定義rmjava

linux系統的rm命令太危險,一不當心就會刪除掉系統文件。 寫一個shell腳原本替換系統的rm命令,要求當刪除一個文件或者目錄時,都要作一個備份,而後再刪除。下面分兩種狀況,作練習:mysql

1. 簡單linux

假設有一個大的分區/data/,每次刪除文件或者目錄以前,都要先在/data/下面建立一個隱藏目錄,以日期/時間命名,好比/data/.201703271012/,而後把全部刪除的文件同步到該目錄下面,可使用rsync -R 把文件路徑一同同步c++

2. 複雜sql

不知道哪一個分區有剩餘空間,在刪除以前先計算要刪除的文件或者目錄大小,而後對比系統的磁盤空間,若是夠則按照上面的規則建立隱藏目錄,並備份,若是沒有足夠空間,要提醒用戶沒有足夠的空間備份並提示是否放棄備份,若是用戶選擇y,則直接刪除文件或者目錄,若是選擇n,則提示未刪除,而後退出腳本。docker

 

參考答案:shell

1.  簡單數據庫

#!/bin/bashcentos

filename=$1tomcat

d=`date +%Y%m%d%H%M`

read -p 「Are U sure delete the file or directory $1? y|n: 」 c

if [ $c == 「y」 ] || [ $c == 「Y」 ]

then

    mkdir /data/.$d

    rsync -aR $1/ /data/.$d/$1/

    /bin/rm -rf $1

elif [ $c == 「n」 ] || [ $c == 「N」 ]

then

    exit 0

else

    echo 「Please input ‘y’ or ‘n’.」

fi

2.  複雜

#!/bin/bash

filename=$1

d=`date +%Y%m%d%H%M`

f_size=`du -sk $1|awk ‘{print $1}’`

disk_size=`LANG=en; df -k |grep -vi filesystem|awk ‘{print $4}’ |sort -n |tail -n1`

big_filesystem=`LANG=en; df -k |grep -vi filesystem |sort -n -k4 |tail -n1 |awk ‘{print $NF}’`

if [ $f_size -lt $disk_size ]

then

    read -p 「Are U sure delete the file or directory: $1? y|n: 」 c

    if [ $c == 「y」 ] || [ $c == 「Y」 ]

    then

        mkdir -p $big_filesystem/.$d && rsync -aR $1 $big_filesystem/.$d/ && /bin/rm -rf $1

    elif [ $c == 「n」 ] || [ $c == 「N」 ]

    then

        exit 0

    else

        echo 「Please input ‘y’ or ‘n’.」

    fi

else

    echo 「The disk size is not enough to backup the files $1.」

    read -p 「Do you want to delete 「$1″? y|n: 」 c

    if [ $c == 「y」 ] || [ $c == 「Y」 ]

    then

        echo 「It will delete 「$1″ after 5 seconds whitout backup.」

        for i in `seq 1 5`; do echo -ne 「.」; sleep 1;done

    echo

        /bin/rm -rf $1

    elif [ $c == 「n」 ] || [ $c == 「N」 ]

    then

        echo 「It will not delete $1.」

        exit 0

    else

        echo 「Please input ‘y’ or ‘n’.」

    fi

fi

 

二、

請把下面的字符串:

zhangsan

y97JbzPru

lisi

5JhvCls6q

xiaowang

Nnr8qt2Ma

laoma

iqMtvC02y

zhaosi

9fxrb4sJD

改成以下:

zhangsan:y97JbzPru

lisi:5JhvCls6q

xiaowang:Nnr8qt2Ma

laoma:iqMtvC02y

zhaosi:9fxrb4sJD

參考答案:

sed 'N;s/\n/:/g'  filename

 

截取字符串

三、利用你學過的知識點,想辦法根據要求截取出字符。

字符串var=http://www.aaa.com/root/123.htm

1.取出www.aaa.com/root/123.htm

2.取出123.htm

3.取出http://www.aaa.com/root

4.取出http:

5.取出http://

6.取出www.aaa.com/root/123.htm

7.取出123

8.取出123.htm

 

參考答案:

#!/bin/bash

 

var="http://www.aaa.com/root/123.htm"

 

echo "##1 取出www.aaa.com/root/123.htm"

echo $var | awk -F"//" '{print $2}'       #截取//後面的內容

echo $var | grep -o "www.*"

 

echo "##2 取出123.htm"

echo $var | awk -F"/" '{print $5}'

echo $var | grep -o "[0-9]*\.htm"

 

echo "##3 取出http://www.aaa.com/root"

echo $var | sed 's|\(.*//.*/.*\)\(/.*\)|\1|'

echo $var | grep -o http.*root

 

echo "##4 取出http:"

echo $var | awk -F '//' '{print $1}'

echo $var | sed 's/\/\/www.*//'

 

echo "##5 取出http://"

echo $var | awk -F "w" '{print $1}'

echo $var | sed 's/www.*//'

 

echo "##6 取出www.aaa.com/root/123.htm"

echo $var | awk -F '//' '{print $2}'

echo $var | sed 's|http://||'

 

echo "##7 取出123"

echo $var | tr -c -d '0-9\n'

echo $var | grep -o '[0-9]\{3\}'

 

echo "##8 取出123.htm"

echo $var | awk -F '/' '{print $5}'

 

四、打印三角形

打印一個三角形(正三角形,元素用*表示)。

參考答案:

#!/bin/bash

read -p "please input the lenth:" n

for i in `seq 1 $n`

do

    for ((j=$n;j>i;j--))

    do

        echo -n " "

    done

    for m in `seq 1 $i`

    do

        echo -n "* "

    done

    echo

done

 

五、shell多線程

需求背景:

領導要求小明備份數據庫服務器裏面的100個庫(數據量在幾十到幾百G),須要以最快的時間完成(5小時內),而且不能影響服務器性能。

需求分析:

因爲數據量比較大,單個庫備份時間少則10幾分鐘,多則幾個小時,咱們算平均每一個庫30分鐘,若一個庫一個庫的去備份,則須要3000分鐘,至關於50個小時。很明顯不可取。但所有丟到後臺去備份,100個併發,數據庫服務器也沒法承受。因此,須要寫一個腳本,可以控制併發數就能夠實現了。

 

參考答案:

#!/bin/sh

##假設100個庫的名字存到了一個文件裏,文件名字爲/tmp/databases.list

##其中備份數據庫用了mysqldump,這裏你能夠換成xtrabackup,更快

function bak_data {

    dbname=$1

    d=`date +%y%d`

    mysqldump -uroot -pxxxxx $dbname > /backup/$1.$d

}

mkfifo $tmp_fifofile

exec 1000<>$tmp_fifofile

rm -f $tmp_fifofile

thread=10

for ((i=0;i<$thread;i++));

do

    echo >&1000

done

for d in `cat /tmp/databases.list`

do

    read -u1000

    {

        bak_data $d 

        echo >&1000

    } & 

done

wait

exec 1000>&-

 

六、自動掛載磁盤

咱們使用的雲主機,購買一塊雲盤後,默認並非掛載狀態的,用shell寫一個腳本,只要把盤符和掛載點以參數的形式提供給腳本,該腳本就能夠自動格式化、掛載。

要求:

1 不用分區,直接格式化

2 格式化爲ext4文件系統類型

 

參考答案:

#!/bin/bash

 

echo "Useage $0 盤符 掛載點, 如: $0 /dev/xvdb /data"

 

if [ $# -ne 2 ]

then

    exit

fi

 

if [ ! -b $1 ]

then 

    echo "你提供的盤符不正確,請檢查後再操做"

    exit 1

fi

mkfs -t ext4 $1

 

if [ ! -d $2 ] ;then

        mkdir -p $2

fi

 

n=`egrep " $2 " /etc/fstab|wc -l`

if [ $n -eq 0 ]

then

    echo "$1              $2                      ext4   defaults  0  0" >> /etc/fstab

    mount -a

else

    mount $1 $2

    echo "配置文件/etc/fstab中已經存在掛載點$2,請檢查一下."

fi

 

七、自動封/解封ip

需求背景:

discuz論壇,天天有不少註冊機註冊的用戶,而後發垃圾廣告帖子。雖然使用了一些插件但沒有效果。分析訪問日誌,發現有幾個ip訪問量特別大,因此想到能夠寫個shell腳本,經過分析訪問日誌,把訪問量大的ip直接封掉。

可是這個腳本頗有可能誤傷,因此還須要考慮到自動解封這些ip

思路:

1 能夠每分鐘分析1次訪問日誌,設定一個閾值,把訪問量大的ipiptables封掉80端口

2 20分鐘檢測一次已經被封ip的請求數據包數量,設定閾值,把沒有請求的或者請求量很小的解封

 

參考答案:

#! /bin/bash

## To block the ip of bad requesting.

## Writen by aming 2017-11-18.

 

log="/data/logs/www.xxx.com.log"

tmpdir="/tmp/badip"

#白名單ip,不該該被封

goodip="27.133.28.101"

 

[ -d $tmpdir ] || mkdir -p $tmpdir

 

t=`date -d "-1 min"  +%Y:%H:%M`

 

#截取一分鐘之前的日誌

grep "$t:" $log > $tmpdir/last_min.log

 

#把一分鐘內日誌條數大於120的標記爲不正常的請求

awk '{print $1}' $tmpdir/last_min.log |sort -n |uniq -c |sort -n |tail |awk '$1>120 {print $2}'|grep -v "$good_ip"> $tmpdir/bad.ip

 

d3=`date +%M`

 

#每隔20分鐘解封一次ip

if [ $d3 -eq "20" ] || [ $d3 -eq "40" ] || [ $d3 -eq "00" ]

then

        /sbin/iptables -nvL INPUT|grep 'DROP' |awk '$1<10 {print $8}'>$tmpdir/good.ip

        if [ -s $tmpdir/good.ip ]

        then

        for ip in `cat $tmpdir/good.ip`

        do

                /sbin/iptables -D INPUT -p tcp --dport 80 -s $ip -j DROP

                d4=`date +%Y%m%d-%H:%M`

                echo "$d4 $ip unblock" >>$tmpdir/unblock.ip

        done

        fi

 

        #解封后,再把iptables的計數器清零

        /sbin/iptables -Z INPUT

fi

 

if [ -s $tmpdir/bad.ip ]

then

    for ip in `cat $tmpdir/bad.ip`

    do

        /sbin/iptables -A INPUT -p tcp --dport 80 -s $ip -j DROP

        d4=`date +%Y%m%d-%H:%M`

        echo "$d4 $ip block" >>$tmpdir/block.ip

    done

fi

 

8、安裝samba

寫個shell腳本,可以實現一鍵安裝並配置samba服務,執行該腳本時須要帶一個參數,爲共享的目錄,目錄能夠不存在,若不存在,須要腳本自動建立。

 

參考答案:

#!/bin/bash

is_samba_installed=`rpm -qa|grep samba|wc -l`

if [ $is_samba_installed != 0 ]

then

    echo "You had already installed Samba."

    exit 0

fi

 

echo "It will install Samba."

sleep 1

cnfdir="/etc/samba/smb.conf"

 

chkok(){

    if [ $? != 0 ]

    then

        echo "Error, Please try again."

        exit 1

    fi

}

 

yum install -y samba

chkok

 

sed -i 's/MYGROUP/WORKGROUP/' $cnfdir

sed -i 's/user/share/' $cnfdir

sed -i '$a\[fish]' $cnfdir

if [ -d $1 ]

then

    cd $1

    echo "test" > test.txt

    sed -i '$a\[fish]\n\tcomment = Share All\n\tpath = "'$1'"\n\tbrowseable = yes\n\tpublic = yes\n\twritable = no' $cnfdir

else

    mkdir $1

    cd $1

    echo "test" > test.txt

    sed -i '$a\[fish]\n\tcomment = Share All\n\tpath = "'$1'"\n\tbrowseable = yes\n\tpublic = yes\n\twritable = no' $cnfdir

fi

/etc/init.d/smb start

chkok

echo "Please input [\\sambaIP\sharename] to access the share dir."

 

九、管理容器

shell寫一個腳本,實現一鍵管理docker容器,好比啓動/關閉/刪除容器等操做。

要求:

1 腳本支持啓動所有容器、關閉所有容器、刪除所有容器

2 須要提示用戶如何使用該腳本,需給出範例

參考答案:

說明,本腳本爲端亞同窗提供。

#! /bin/bash

##start,restart,delete the docker containers

##written by zhdya_20171114

list=`docker ps -a |awk '{print $2}'| grep -v 'ID'`

echo "======================================="

echo -e "pls check the follow list of container: \n$list"

read -p "pls choose an action which you want!<1.start 2.stop 3.rm > " act

echo "======================================"

echo -e "stop\nstart\nrm\nrmi" > /tmp/docker.txt 

##judge if input the words or not!

 

if [ -z $act ]

then

        echo "you type was wrong,pls just input "start"."stop"."rm"."rmi"."

        exit

fi

 

##judge if input a wrong words!!

if grep -wq $act /tmp/docker.txt

then

        case $act in

        start)

                docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)

                echo "already start all of container,pls checking it.."

        ;;

 

        stop)

                docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)

                echo "already restart all of container,pls checking it.."

        ;;

 

        rm)

                docker rm $(docker ps -a | awk '{ print $1}' | tail -n +2)

                echo "already rm all of container,pls checking it.."

        ;;

 

        *)

                docker rmi $(docker images | awk '{print $3}' |tail -n +2)

                echo "already rm all of container,pls checking it.."

        esac

else

        echo "you type was wrong,pls just input "start"."stop"."rm"."rmi"."

fi

 

十、部署mysql主從

shell腳本實現,部署mysql主從架構。

思路是這樣的:

1master.sh腳本用來安裝mastermysql

2)而後經過expect腳本+rsync工具把slave.sh腳本、/etc/my.cnf /etc/init.d/mysqld 還有mysqldump下來的all.sql,以及在master下載下來的mysql二進制安裝包傳到slave

3)經過expect腳原本運行slave.sh的腳原本安裝,而且配置好主從,期間,用slave.tmp來記錄master機子的binlog的狀態,以便於傳到slave後用命令添加進去。

 

參考答案:

cp_slave.expect

#!/usr/bin/expect

set user [lindex $argv 0]

set host [lindex $argv 1]

set passwd [lindex $argv 2]

set file [lindex $argv 3]

spawn rsync -avzP $file $user@$host:/tmp

set timeout 600

expect {

「yes/no」 { send 「yes\r」}

「password:」 { send 「$passwd\r」 }

}

expect eof

ins_rsync.expect

#!/usr/bin/expect

set user [lindex $argv 0]

set host [lindex $argv 1]

set passwd [lindex $argv 2]

spawn ssh $user@$host

expect {

「yes/no」 { send 「yes\r」;exp_continue}

「password:」 { send 「$passwd\r」 }

}

expect 「]*」

send 「yum install -y rsync\rexit\r」

interact

slave.expect

#!/usr/bin/expect

set host [lindex $argv 0]

set passwd [lindex $argv 1]

set cm [lindex $argv 2]

spawn ssh root@$host

expect {

「yes/no」 { send 「yes\r」}

「password:」 { send 「$passwd\r」 }

}

expect 「]*」

send 「$cm\rexit\r」

interact

slave.sh

#!/bin/bash

####this is for building slave script

##by lv.

####master ip address

mas_ip=192.168.47.24

###mysql password conf

my_passwd=hd8832508

####replication user and password

rp_user=hd

rp_passwd=hd8832508

###check ok

check(){

if [ $? != 0 ]

then

echo 「error,please check log.」

exit 1

fi

}

##close seliux

sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/’ /etc/selinux/config

selinux_s=`getenforce`

if [ $selinux_s == 「Enforcing」  -o $selinux_s == 「enforcing」 ]

then

setenforce 0

fi

##close iptables

iptables-save > /etc/sysconfig/iptables_`date +%s`

iptables -F

service iptables save

##install the mirror.aliyun.com

cd /etc/yum.repos.d/

if rpm -qa |grep epel-release >/dev/null

then

rpm -e epel-release

fi

if [ -f epel.repo ]

then

/bin/mv epel.repo epel.repo.bak

fi

yum install -y wget

if [ -f CentOS-Base.repo ]

then

/bin/mv CentOS-Base.repo CentOS-Base.repo.bak

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

wget http://mirrors.aliyun.com/repo/epel-6.repo -O /etc/yum.repos.d/epel.repo

fi

yum clean all

yum makecache

#first to update datetime

[ `rpm -qa |grep ntpdate|wc -l` -eq 1 ] || yum install -y ntpdate

ntpdate 0.openwrt.pool.ntp.org 2>&1 >/dev/null;clock -w

###install lib software

syum(){

if ! rpm -qa|grep -q $1

then

yum install -y $1

check

else

echo 「$1 is already installed」

fi

}

## install some packges for the first on setup.

for p in gcc perl perl-devel libaio libaio-devel pcre-devel zlib-devel cmake glibc pcre compat-libstdc++-33

do

syum $p

done

###check file is already in tmp

if [ ! -f /tmp/my.cnf ] && [ ! -f /tmp/mysqld ] && [ ! -f /tmp/mysql-* ] && [ ! -f /tmp/slave.tmp ]

then

echo 「error,please try to sync again」

exit 1

fi

mysql=`ls /tmp |grep tar.gz`

version=`echo /tmp/$mysql|awk -F – ‘{print $2}’|cut -d. -f2`

######install mysql

cd /tmp

tar -zxf $mysql

mv `echo $mysql|sed ‘s/.tar.gz//g’` /usr/local/mysql

cd /usr/local/mysql

if ! grep 「^mysql:」 /etc/passwd

then

useradd -s /sbin/nologin -M mysql

check

fi

[ -d /data/mysql ] && /bin/mv /data/mysql /data/mysql_`date +%s`

mkdir -p /data/mysql

chown -R mysql:mysql /data/mysql

###initialize

case $version in

1)

/usr/local/mysql/scripts/mysql_install_db –user=mysql –datadir=/data/mysql

check

sed -i ‘/^server-id/’d /tmp/my.cnf

check

sed -i ‘/\[mysqld\]/a\server-id=2’ /tmp/my.cnf

check

;;

6)

/usr/local/mysql/scripts/mysql_install_db –user=mysql –datadir=/data/mysql

check

sed -i ‘/^server_id/’d /tmp/my.cnf

check

sed -i ‘/\[mysqld\]/a\server_id = 2’ /tmp/my.cnf

check

;;

7)

pswd5_7=`/usr/local/mysql/bin/mysqld –user=mysql –datadir=/data/mysql –initialize 2>&1 |sed -r -n ‘/localhost: /p’|sed ‘s/.* //g’`

/usr/local/mysql/bin/mysql_ssl_rsa_setup –datadir=/data/mysql

check

sed -i ‘/^server_id/’d /tmp/my.cnf

check

sed -i ‘/\[mysqld\]/a\server_id = 2’ /tmp/my.cnf

check

;;

esac

###cp conf file

/bin/cp -rf /tmp/my.cnf /etc/my.cnf

check

/bin/cp -rf /tmp/mysqld /etc/init.d/

check

chmod 755 /etc/init.d/mysqld

chkconfig –add mysqld

chkconfig mysqld on

service mysqld start

check

####change mysql password

if [ $version -eq 7 ]

then

/usr/local/mysql/bin/mysql -uroot -p$pswd5_7 –connect-expired-password -e 「set password=password(‘$my_passwd’);」

check

else

/usr/local/mysql/bin/mysql -uroot -e 「set password=password(‘$my_passwd’);」

check

fi

###input date

if [ -f /tmp/all.sql ]

then

/usr/local/mysql/bin/mysql -uroot -p$my_passwd < /tmp/all.sql

check

else

echo 「date error.」

exit 1

fi

######binlog

slave_bin=`grep 「mysql-bin」 /tmp/slave.tmp`

slave_pos=`grep ‘^[0-9]’ /tmp/slave.tmp`

###stop slave

/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e 「stop slave;」

check

###configure slave

/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e 「change master to master_host=’$mas_ip’,master_port=3306,master_user=’$rp_user’,master_password=’$rp_passwd’,master_log_file=’$slave_bin’,master_log_pos=$slave_pos;」

check

###start slave

/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e 「start slave;」

check

###check repecation status

show=`/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e 「show slave status\G;」|grep ‘Slave_IO_Running:’`

slaveIO=`echo $show|awk -F’:’ ‘{print $2}’`

Slave_SQL=`echo $show|awk -F’:’ ‘{print $2}’`

if [ $slaveIO == Yes ] && [$Slave_SQL == Yes ]

then

echo 「mysql repliation is start」

/bin/rm -rf /tmp/all.sql /tmp/$mysql /tmp/mysqld /tmp/my.cnf /tmp/slave.tmp

else

echo 「error,please check the log.」

fi

master.sh

#!/bin/bash

#####this is building mysql replication###

##by lv.

ml=`pwd`

ar=`arch`

###mysql password conf

my_passwd=hd8832508

####replication user and password

rp_user=hd

rp_passwd=hd8832508

###slave conf

s_user=root

s_host=192.168.47.25

s_passwd=hd8832508

###check ok

check(){

if [ $? != 0 ]

then

echo 「error,please check log.」

exit 1

fi

}

####check the file is exist

for wj in $ml/cp_slave.expect $ml/ins_rsync.expect $ml/slave.expect $ml/slave.sh

do

if [ ! -f $wj ]

then

echo 「error,your miss $wj file.」

exit 1

else

/bin/chmod +x $wj

check

fi

done

##close seliux

sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/’ /etc/selinux/config

selinux_s=`getenforce`

if [ $selinux_s == 「Enforcing」  -o $selinux_s == 「enforcing」 ]

then

setenforce 0

fi

##close iptables

iptables-save > /etc/sysconfig/iptables_`date +%s`

iptables -F

service iptables save

##install the mirror.aliyun.com

aliyun(){

cd /etc/yum.repos.d/

if rpm -qa |grep epel-release >/dev/null

then

rpm -e epel-release

fi

if [ -f epel.repo ]

then

/bin/mv epel.repo epel.repo.bak

fi

yum install -y wget

if [ -f CentOS-Base.repo ]

then

/bin/mv CentOS-Base.repo CentOS-Base.repo.bak

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

wget http://mirrors.aliyun.com/repo/epel-6.repo -O /etc/yum.repos.d/epel.repo

fi

yum clean all

yum makecache

}

if [ `grep 「aliyun.com」 /etc/yum.repos.d/CentOS-Base.repo|wc -l` -eq 0 ]

then

aliyun

else

echo 「aliyun epel is already installed.」

fi

#first to update datetime

[ `rpm -qa |grep ntpdate|wc -l` -eq 1 ] || yum install -y ntpdate

ntpdate 0.openwrt.pool.ntp.org 2>&1 >/dev/null;clock -w

###install lib software

syum(){

if ! rpm -qa|grep -q $1

then

yum install -y $1

check

else

echo 「$1 is already installed」

fi

}

## install some packges for the first on setup.

for p in gcc perl perl-devel libaio libaio-devel pcre-devel zlib-devel cmake glibc pcre compat-libstdc++-33

do

syum $p

done

###variables,fuctions

mysql_5_1=http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.73-linux-$ar-glibc23.tar.gz

mysql_5_6=http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.31-linux-glibc2.5-$ar.tar.gz

mysql_5_7=http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.12-linux-glibc2.5-$ar.tar.gz

#######################################

conf_mysql(){

cd /usr/local/mysql

if ! grep 「^mysql:」 /etc/passwd

then

useradd -s /sbin/nologin -M mysql

check

fi

[ -d /data/mysql ] && /bin/mv /data/mysql /data/mysql_`date +%s`

mkdir -p /data/mysql

chown -R mysql:mysql /data/mysql

###initialize

case $version in

5.1)

./scripts/mysql_install_db –user=mysql –datadir=/data/mysql

check

;;

5.6)

./scripts/mysql_install_db –user=mysql –datadir=/data/mysql

check

;;

5.7)

pswd5_7=`./bin/mysqld –user=mysql –datadir=/data/mysql –initialize 2>&1 |sed -r -n ‘/localhost: /p’|sed ‘s/.* //g’`

./bin/mysql_ssl_rsa_setup –datadir=/data/mysql

check

;;

esac

}

cp_mysql(){

###my.cnf

if [ -f  /usr/local/mysql/support-files/my-huge.cnf ]

then

/bin/cp -rf support-files/my-huge.cnf /etc/my.cnf

check

sed -i ‘/^\[mysqld\]$/a\datadir = /data/mysql’ /etc/my.cnf

check

else

/bin/cp -rf support-files/my-default.cnf /etc/my.cnf

check

sed -i ‘/^\[mysqld\]$/a\socket = /tmp/mysql.sock’ /etc/my.cnf

sed -i ‘/^\[mysqld\]$/a\port = 3306’ /etc/my.cnf

sed -i ‘/^\[mysqld\]$/a\datadir = /data/mysql’ /etc/my.cnf

check

sed -i ‘/^\[mysqld\]$/a\basedir = /usr/local/mysql’ /etc/my.cnf

fi

####/etc/init.d/mysqld

if [ $version == 5.7 ]

then

/bin/cp support-files/mysql.server /etc/init.d/mysqld

check

sed -i ‘s#^datadir=#datadir=/data/mysql#’ /etc/init.d/mysqld

sed -i ‘s#^basedir=#basedir=/usr/local/mysql#’ /etc/init.d/mysqld

check

chmod 755 /etc/init.d/mysqld

chkconfig –add mysqld

chkconfig mysqld on

service mysqld start

check

else

/bin/cp support-files/mysql.server /etc/init.d/mysqld

sed -i ‘s#^datadir=#datadir=/data/mysql#’ /etc/init.d/mysqld

chmod 755 /etc/init.d/mysqld

chkconfig –add mysqld

chkconfig mysqld on

service mysqld start

check

fi

}

###install mysql

insall_mysql(){

echo 「Chose the version of mysql.」

select mysql_v in 5.1 5.6 5.7

do

case $mysql_v in

5.1)

cd /usr/local/src

[ -f ${mysql_5_1##*/} ] || wget $mysql_5_1

tar zxf ${mysql_5_1##*/}

check_ok

[ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_`date +%s`

mv `echo ${mysql_5_1##*/}|sed ‘s/.tar.gz//g’` /usr/local/mysql

check_ok

version=5.1

conf_mysql

cp_mysql

break

;;

5.6)

cd /usr/local/src

[ -f ${mysql_5_6##*/} ] || wget $mysql_5_6

tar zxf ${mysql_5_6##*/}

check_ok

[ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_bak

mv `echo ${mysql_5_6##*/}|sed ‘s/.tar.gz//g’` /usr/local/mysql

check_ok

version=5.6

conf_mysql

cp_mysql

break

;;

5.7)

cd /usr/local/src

[ -f ${mysql_5_7##*/} ] || wget $mysql_5_7

tar zxf ${mysql_5_7##*/}

check_ok

[ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_bak

mv `echo ${mysql_5_7##*/}|sed ‘s/.tar.gz//g’` /usr/local/mysql

check_ok

version=5.7

conf_mysql

cp_mysql

break

;;

*)

echo 「only 1(5.1) 2(5.6) or 3(5.7) 」

exit 1

;;

esac

done

}

####change mysql password

passwd_mysql(){

if [ $version == 5.7 ]

then

/usr/local/mysql/bin/mysql -uroot -p$pswd5_7 –connect-expired-password -e 「set password=password(‘$my_passwd’);」

check

else

/usr/local/mysql/bin/mysql -uroot -e 「set password=password(‘$my_passwd’);」

check

fi

}

######

if [ `ps aux|grep mysql|wc -l` -gt 1 ]

then

echo 「mysql is already start」

else

insall_mysql

passwd_mysql

fi

####start install slave

echo 「#############################」

echo 「##                         ##」

echo 「##      slave install      ##」

echo 「##                         ##」

echo 「#############################」

##first check master tool

if ! rpm -qa|grep -q rsync

then

yum install -y rsync

fi

if ! rpm -qa|grep -q expect

then

yum install -y expect

fi

###replication building for master first

if [ `ps aux|grep mysql|wc -l` -gt 1 ] && [ `grep 「log_bin = mysql-bin」 /etc/my.cnf|wc -l` -eq 0 ] && [ `grep 「log-bin=mysql-bin」 /etc/my.cnf|wc -l` -eq 0 ]

then

/etc/init.d/mysqld stop

check

sed -i ‘/^\[mysqld\]$/a\server_id = 1’ /etc/my.cnf

sed -i ‘/^\[mysqld\]$/a\log_bin = mysql-bin’ /etc/my.cnf

sed -i ‘/^\[mysqld\]$/a\binlog_format = 「MIXED」‘ /etc/my.cnf

check

/etc/init.d/mysqld start

check

fi

master_bin=`/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e 「show master status \G;」|grep File|awk ‘{print $2}’`

master_pos=`/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e 「show master status \G;」|grep Position|awk ‘{print $2}’`

echo $master_bin >>/tmp/slave.tmp

echo $master_pos >>/tmp/slave.tmp

/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e 「grant replication slave on *.* to $rp_user@’$s_host’ identified by ‘$rp_passwd’;」

check

/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e 「flush privileges;」

check

###dump date

/usr/local/mysql/bin/mysqldump -uroot -p$my_passwd –single-transaction -A > /tmp/all.sql

check

####cp file to slave

if [ `pwd` != $ml ]

then

cd $ml

fi

./ins_rsync.expect $s_user $s_host $s_passwd

for file in /usr/local/src/mysql-* /etc/my.cnf /etc/init.d/mysqld ./slave.sh /tmp/slave.tmp /tmp/all.sql

do

./cp_slave.expect $s_user $s_host $s_passwd $file

done

./slave.expect $s_host $s_passwd /tmp/slave.sh

 

十一、自動增長公鑰

寫一個shell腳本,當咱們執行時,提示要輸入對方的iproot密碼,而後能夠自動把本機的公鑰增長到對方機器上,從而實現密鑰認證。

 

參考答案:

#!/bin/bash

 

read -p "Input IP: " ip

ping $ip -w 2 -c 2 >> /dev/null

 

## 查看ip是否可用

while [ $? -ne 0 ]

do

    read -p "your ip may not useable, Please Input your IP: " ip

    ping $ip -w 2 -c 2 >> /dev/null

done

read -p "Input root\'s password of this host: " password

 

## 檢查命令子函數

 

check_ok() {

if [ $? != 0 ]

then

    echo "Error!."

    exit 1

fi

}

 

## yum須要用到的包

myyum() {

if ! rpm -qa |grep -q "$1"

then

    yum install -y $1

    check_ok

else

    echo $1  already installed

fi

}

 

for p in openssh-clients openssh expect

do

    myyum $p

done

 

## 在主機A上建立密鑰對

 

if [ ! -f ~/.ssh/id_rsa ] || [ ! -f ~/.ssh/id_rsa.pub ]

then

    if [ -d ~/.ssh ]

    then

        mv ~/.ssh/  ~/.ssh_old

    fi

    echo -e "\n" | ssh-keygen -t rsa -P ''

    check_ok

fi

 

## 傳私鑰給主機B

 

if [ ! -d /usr/local/sbin/rsync_keys ]

then

    mkdir /usr/local/sbin/rsync_keys

fi

cd /usr/local/sbin/rsync_keys

if [ -f rsync.expect ]

then

    d=`date +%F-%T`

    mv rsync.expect $d.expect

fi

 

#建立遠程同步的expect文件

 

cat >  rsync.expect <<EOF

#!/usr/bin/expect

set host [lindex \$argv 0]

#主機B的密碼

set passwd [lindex \$argv 1]

spawn rsync -av /root/.ssh/id_rsa.pub root@\$host:/tmp/tmp.txt

expect {

"yes/no" { send "yes\r"; exp_continue}

"password:" { send "\$passwd\r" }

}

expect eof

spawn ssh root@\$host

expect {

"password:" { send "\$passwd\r" }

}

expect "]*"                         

send "\[ -f /root/.ssh/authorized_keys \] && cat /tmp/tmp.txt >>/root/.ssh/authorized_keys \r"

expect "]*"

send "\[ -f /root/.ssh/authorized_keys \] || mkdir -p /root/.ssh/ \r"            

send "\[ -f /root/.ssh/authorized_keys \] || mv /tmp/tmp.txt /root/.ssh/authorized_keys\r"            

expect "]*"

send "chmod 700 /root/.ssh; chmod 600 /root/.ssh/authorized_keys\r"

expect "]*"

send "exit\r"

EOF

 

check_ok

/usr/bin/expect /usr/local/sbin/rsync_keys/rsync.expect $ip $password

echo "OK,this script is successful. ssh $ip  to test it"

 

十二、域名到期提醒

寫一個shell腳本,查詢指定域名的過時時間,並在到期前一週,天天發一封提醒郵件。

思路: 你們能夠在linux下使用命令「whois 域名,如」whois apelearn.com」,來獲取該域名的一些信息。

提示: whois命令,須要安裝jwhois

 

參考答案:

#!/bin/bash

 

t1=`date +%s`

is_install_whois()

{

    which whois >/dev/null 2>/dev/null

    if [ $? -ne 0 ]

    then

        yum install -y jwhois

    fi

}

 

notify()

{

    e_d=`whois $1|grep 'Expiry Date'|awk '{print $4}'|cut -d 'T' -f 1`

    e_t=`date -d "$e_d" +%s`

    n=`echo "86400*7"|bc`

    e_t1=$[$e_t-$n]

    if [ $t1 -ge $e_t1 ] && [ $t1 -lt $e_t ]

    then

        /usr/local/sbin/mail.py aming_test@163.com "Domain $1 will be expire." "Domain $1 expire date is $e_d."

    fi

}

is_install_whois

notify aminglinux.com

 

1三、取消後綴

至少用兩種方法,批量把當前目錄下面全部文件名後綴爲.bak的後綴去掉,好比1.txt.bak去掉後爲1.txt

參考答案:

假設取消的後綴爲.bak

方法一:

for i in `ls *.bak`

do 

    mv $i `echo $i|sed 's/\.bak//g'`

done

 

方法二:

for i in `ls *.bak`

do 

    newname=`echo $i|awk -F '.bak' '{print $1}'` 

    mv $i $newname

done

 

1四、重啓tomcat

在生產環境中,常常遇到tomcat沒法完全關閉,也就是說用tomcat自帶shutdown.sh腳本沒法將java進程徹底關掉。因此,須要藉助shell腳本,將進程殺死,而後再啓動。

寫一個shell腳本,實現上述功能。完全殺死一個進程的命令是 kill -9 pid.

 

參考答案:

說明:如下腳本爲猿課同窗實際線上跑的shell腳本,考慮的方面比較多,你們能夠學一學他的思路

 

#!/bin/bash

 

###功能: 重啓 tomcat 進程

###要求:對於tomcat中的某些應用,使用shutdown.sh是沒法徹底停掉全部服務的 實際操做中都須要killtomcat再重啓

##

### root can not run this script.

##

 

if [ $USER = root ]

then

        echo "root cann't run this script!please run with other user!"

        exit 1

fi

 

##

### check the Parameter

##

 

if [[ $# -ne 1 ]]

then

        echo "Usage:$0 tomcatname"

        exit 1

fi

 

##

### only one process can run one time

##

 

TMP_FILE_U=/tmp/.tmp.ps.keyword.$USER.956327.txt

#echo $TMP_FILE_U

KEYWORD1="$0"

KEYWORD2="$1"

 

# 使用賦值會多fork出一個進程,因此要先重定向到一個文本,再統計.

 

ps ux |grep "$KEYWORD1"|grep "\<$KEYWORD2\>"|grep -v "grep" > $TMP_FILE_U

Pro_count=`cat $TMP_FILE_U |wc -l`

if [ $Pro_count -gt 1 ]

then

        echo "An other process already running ,exit now!"

        exit 1

fi

 

###################################################

#                                                 #

#               begin of the script               #

#                                                 #

###################################################

 

##

### set the Parameter

##

 

TOM=`echo $1|sed 's#/##g'`

TOMCAT_DIRECTORY=~/usr/local/$TOM

STARTUP_SCRIPT=$TOMCAT_DIRECTORY/bin/startup.sh

TOMCAT_LOG=$TOMCAT_DIRECTORY/logs/catalina.out

CONF_FILE=$TOMCAT_DIRECTORY/conf/server.xml

TEMPFILE=/tmp/.tmpfile.x.89342.c4r3.tmp

 

##

### check if the tomcat directory exist

##

 

if [ ! -d "$TOMCAT_DIRECTORY" ]

then

        echo "the tomcat \"$TOM\" not exist.check again!"

        exit 1

fi

 

##

### log roteta and delete log one week ago

##

 

rotate_log(){

TIME_FORMART=$(date +%Y%m%d%H%M%S)

LOG_DIR=$(dirname $TOMCAT_LOG)

mv $TOMCAT_LOG ${TOMCAT_LOG}_${TIME_FORMART}

find $LOG_DIR -type f -ctime +7 -exec rm -rf {} \;

}

 

##

### function start the tomcat

##

 

start_tomcat()

{

#echo start-tomcat-func

if [ -x  "$STARTUP_SCRIPT" ]

then

      

        rotate_log

        $STARTUP_SCRIPT

        sleep 1

        tail -f $TOMCAT_LOG

else

        if [ -e $STARTUP_SCRIPT ]

        then

                chmod +x $STARTUP_SCRIPT

#               echo "permition added!"

                if [ -x  "$STARTUP_SCRIPT" ]

                then

                      

                        rotate_log

                        $STARTUP_SCRIPT

                        sleep 1

                        tail -f $TOMCAT_LOG

                else

                        echo "The script not have excute permision,Couldn't add permision to Script!"

                        exit 1

                fi

        else

                echo "error,the script \"startup.sh\" not exist!"

                exit 1

        fi

fi

}

 

##

### function stop the tomcat

##

 

stop_tomcat()

{

rm -rf $TEMPFILE

ps ux |grep /$TOM/ |grep -v "grep /$TOM/"|grep java > $TEMPFILE

Pro_Count=`cat $TEMPFILE|wc -l`

PIDS=`cat $TEMPFILE|awk '{print $2}'`

rm -rf $TEMPFILE

#echo $Pro_Count

 

if [ $Pro_Count -eq 0 ]

then

        echo "The tomcat not running now!"

else

        if [ $Pro_Count -ne 1 ]

        then

                echo "The have $Pro_Count process running,killed!"

                kill -9 `echo $PIDS`

                WC=`ps aux | grep "/$TOM/" | grep -v "grep /$TOM/" | grep java |wc -l`

                [ $WC -ne 0 ] && (echo "kill process failed!";exit 1)

        else

                echo "Process killed!"

                kill -9 `echo $PIDS`

                WC=`ps aux | grep "/$TOM/" | grep -v "grep /$TOM/" | grep java |wc -l`

                [ $WC -ne 0 ] && (echo "kill process failed!";exit 1)

        fi

fi

}

 

###########################

####                   ####

####  The main script  ####

####                   ####

###########################

 

echo -e "are you sure restart $TOM?(y or n)"

read ANS

if [ "$ANS"a != ya ]

then

   echo -e "bye! \n"

   exit 1

fi

 

stop_tomcat

echo "start tomcat ..."

sleep 2

start_tomcat

# end

 

1五、關閉服務

centos6系統裏,咱們可使用ntsysv關閉不須要開機啓動的服務,固然也可使用chkconfig工具來實現。

寫一個shell腳本,用chkconfig工具把不經常使用的服務關閉。腳本須要寫成交互式的,須要咱們給它提供關閉的服務名字。

 

參考答案:

#!/bin/bash

LANG=en

c="1"

while [ ! $c == "q" ]

do

    echo -e "\033[35mPlease chose a service to close from this list: \033[0m"

    chkconfig --list |awk '/3:on/ {print $1}'

    read -p "Which service to close: " s

    chkconfig $s off

    service $s stop

    read -p  "If you want's to quit this program, tab "q", or tab "Ctrl c": " c

done

 

1六、測試數字大小
#!/bin/bash
NUM=100
if (($NUM > 4));then
      echo "The NUM is $NUM greater 4."
else
      echo "The NUM is $NUM little 4."
fi
 

1七、測試目錄是否存在。不存在則新建(注意 。中括號之間必需要空格);

#!/bin/bash
#dir exitst
if [ ! -d /data/20160512 ];then
   mkdir -p /data/20160512
else

fi


#!/bin/bash
DIR=/tmp/2016/10/23
if [ ! -d $DIR ];then
        mkdir -p $DIR
else
       echo "The $DIR is exist"
       exit
fi

注意 :-d是判斷目錄是否存在

-p  是判斷文件是否存在

 

FILES替換DIR

:%s/DIR/FILES

1八、

 

1九、

 

20、

 

 

2一、

 

2二、

 

 

2三、

 

 

2四、

 

 

2五、

 

2六、

 

2七、

 

2八、

 

2九、

 

30、

相關文章
相關標籤/搜索