Shell習題100例(2)


找文件差異

grep -f 選項可以匹配到文件a在文件b中所有相關的行(取a中有b中有的)

[root@centos-04 tmp]# vim b.txt 
vvvv
root
[root@centos-04 tmp]# grep -f b.txt /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
dockerroot:x:994:991:Docker User:/var/lib/docker:/sbin/nologin
[root@centos-04 tmp]# 

取passwd中有b文件中沒有的

[root@centos-04 tmp]# grep -vf b.txt /etc/passwd
[root@centos-04 tmp]# vim 61.sh 
#!/bin/bash
#這個腳本用來比較文件差異
#作者:SYJ
#日期:2019-03-13

cat a.txt|while read line
do
        if ! grep -q "$line" b.txt
        then
                echo $line
        fi
done > c.txt
wc -l c.txt
[root@centos-04 tmp]# sh 61.sh  
3 c.txt
[root@centos-04 tmp]# cat c.txt 
aaa
1111
2222
[root@centos-04 tmp]# 

殺進程

[root@centos-04 tmp]# ps -u $USER|awk '$NF ~ /kworker/ {print $1}' |xargs kill
[root@centos-04 tmp]# vim 62.sh 
#!/bin/bash
#這個腳本用來殺進程
#作者:SYJ
#日期:2019-03-13
ps -u $USER|awk '$NF ~ /kworker/ {print $1}' |xargs kill

並發備份數據庫

split切割文件,默認文件名是aa、ab、ac這種,可以自定義名,我們下面例子用test為文件名前綴

[root@centos-04 tmp]# cat 1.txt 
1
2
3
4
5
6
[root@centos-04 tmp]# split -l 2 1.txt 
[root@centos-04 tmp]# wc -l xa*
 2 xaa
 2 xab
 2 xac
 6 總用量
[root@centos-04 tmp]# 
[root@centos-04 tmp]# cat xaa
1
2
[root@centos-04 tmp]# cat xab
3
4
[root@centos-04 tmp]# cat xac
5
6
[root@centos-04 tmp]# 
[root@centos-04 tmp]# split -l 2 1.txt test
[root@centos-04 tmp]# wc -l testa*
 2 testaa
 2 testab
 2 testac
 6 總用量
[root@centos-04 tmp]#  

scale運算結果保留幾位小數

[root@centos-04 tmp]# echo "scale=3;1000/265"|bc -l
3.773

wait命令會等后台運行的進程執行完再執行后面的命令

[root@centos-04 tmp]# sleep 10 &
[1] 38044
[root@centos-04 tmp]# wait
[1]+  完成                  sleep 10
[root@centos-04 tmp]#   

jobs命令查看后台進程

[root@centos-04 tmp]# jobs
[root@centos-04 tmp]# 
[root@centos-04 tmp]# sleep 10 &
[1] 38136
[root@centos-04 tmp]# jobs
[1]+  運行中               sleep 10 &
[root@centos-04 tmp]# 
[root@centos-04 tmp]# vim 63.sh 
#!/bin/bash
#這個腳本用來並發備份數據庫
#作者:SYJ
#日期:2019-03-14

N=2
mysql -uroot -p -h10.21.95.237 big -e "show tables"|sed '1d' > /tmp/table.txt

n=`wc -l /tmp/table.txt|awk '{print $1}'`

div()
{
        n=`echo "scale=1;$1/$2"|bc`
        n1=`echo "scale=1;$n+0.5"|bc`
        echo $n1|cut -d. -f1
}

n1=`div $n $N`

split -l $n1 /tmp/table.txt

myd(){
        for t in `cat $1`
        do
                mysqldump -uroot -p -h10.21.95.237 big $t > $t.sql
        done
}

for f in xaa xab
do
        myd $f &
done
wait

tar zcf mydb.tar.gz *.sql
rm -f $.sql
[root@centos-04 tmp]# sh 63.sh 

監控CDN節點  

cdn其實就是一個代理服務器,只不過這個代理服務器有緩存功能。用戶訪問的時候加入訪問的是圖片,用戶直接可以在最近的cdn服務器上訪問圖片。

[root@centos-04 tmp]# vim 64.sh
#這個腳本用來監控CDN節點
#作者:SYJ
#!/bin/bash
#這個腳本用來監控CDN節點
#作者:SYJ
#日期:2019-04-08
s_ip=88.88.88.88
url=www.syj.com/index.php
ipf=/data/ip.list
curl -x$s_ip:80 $url 2>/dev/null >/tmp/source.txt
for ip in `cat $ipf`
do
        curl -x$ip:80 $url 2>/dev/null >/tmp/$ip.txt
        diff /tmp/source.txt /tmp/$ip.txt > /tmp/$ip.diff
        n=`wc -l /tmp/$ip.diff|awk '{print $1}'`
        if [ $n -gt 0 ]
        then
                echo "節點$ip有異常。"
        fi
done

破解字符串

[root@centos-04 tmp]# vim 65.sh
#!/bin/bash
#這個腳本用來破解字符串
#作者:SYJ
#日期:2019-04-08
for i in `seq 0 32767`
do
        m=`echo $i|md5sum |cut -c 1-8 `
        echo $i $m
done > /tmp/md5.txt

cat > c.txt <<EOF
21029299
00205d1c
EOF

grep -f c.txt /tmp/md5.txt
[root@centos-04 tmp]# sh 65.sh 
1346 00205d1c
25667 21029299

判斷cpu廠商  

[root@centos-04 tmp]# cat /proc/cpuinfo 
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 58
model name      : Intel(R) Core(TM) i3-3220 CPU @ 3.30GHz
stepping        : 9
microcode       : 0x16
cpu MHz         : 3292.569
cache size      : 3072 KB
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl
xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer xsave avx f16c hypervisor lahf_lm epb fsgsbase
tsc_adjust smep dtherm arat pln pts bogomips : 6585.13 clflush size : 64 cache_alignment : 64 address sizes : 42 bits physical, 48 bits virtual power management: [root@centos-04 tmp]#
[root@centos-04 tmp]# grep '^vendor_id' /proc/cpuinfo |head -1
vendor_id       : GenuineIntel
[root@centos-04 tmp]# 
[root@centos-04 tmp]# grep '^vendor_id' /proc/cpuinfo |head -1 |awk -F ': ' '{print $2}'
GenuineIntel
[root@centos-04 tmp]#   

同步時間

[root@centos-04 tmp]# ntpdate time.windows.com
 8 Apr 15:59:35 ntpdate[8808]: step time server 13.65.245.138 offset -28350.105140 sec
[root@centos-04 tmp]# date
2019年 04月 08日 星期一 15:59:41 CST
[root@centos-04 tmp]# 
[root@centos-04 tmp]# vim 66.sh  
#company[GenuineIntel]='CPU廠商是Intel'
#for name in  GenuineIntel AMD
#do
#       if [ $name == $cpu ]
#       then
#               echo ${company[$name]}
#       else
#               echo "CPU廠商是非主流廠商"
#       fi
#done


#if [ $cpu == "AMD" ]
#then
#       echo "CPU廠商是AMD"
#elif [ $cpu == "GenuineIntel" ]
#then
#       echo "CPU廠商是Intel"
#else
#       echo "CPU廠商是非主流廠商"
#fi

case $cpu in
        AMD)
                echo "CPU廠商是AMD"
        ;;
        GenuineIntel)
                echo "CPU廠商是Intel"
        ;;
        *)
                echo "CPU廠商是非主流廠商"
        ;;
esac
[root@centos-04 tmp]# sh 66.sh  
CPU廠商是Intel
[root@centos-04 tmp]# 

監控CPU使用率  

用空白字符和百分號分割字符

[root@centos-04 tmp]# top -bn1 |grep 'Cpu(s):'|sed 's/^%//'|awk -F ' +|%' '{print $8}'             
93.8
[root@centos-04 tmp]# vim 67.sh
#!/bin/bash
#這個腳本用來計算CPU使用率
#作者:SYJ
#日期:2019-04-08

while :
do

        cpu_i=`top -bn1 |grep 'Cpu(s):'|sed 's/^%//'|awk -F ' +|%' '{print $8}'`
        cpu_u=`echo 100-$cpu_i|bc`

        if [ $cpu_u gt 90 ]
        then
                python mail.py xxx@xxx.com "CPU使用率偏高" "`top -bn1`"
        fi

        sleep 10
done

打印子進程  

查看父進程ID是7046的子進程

[root@centos-04 tmp]# ps -elf |awk '$5==7046 {print $4}'    
7052
7573
[root@centos-04 tmp]# 
[root@centos-04 tmp]# vim 68.sh 
#!/bin/bash
#這個腳本用來打印子進程
#作者:SYJ
#日期:2019-04-08

ps -elf > /tmp/pid.txt
read -p "Please input a pid: " p

if [ -z "$p" ]
then
        echo "Please input a pid"
        exit
fi

if ! grep -qw "$p" /tmp/pid.txt
then
        echo "你輸入的pid不存在"
        exit
fi

get_cpid()
{
        p1=$1
        ps -elf |awk -v p2=$p1 '$5==p2 {print $4}' > /tmp/$p1.txt
        n=`wc -l /tmp/$p1.txt|awk '{print $1}'`
        if [ $n -eq 0 ]
        then
                echo "進程$1下沒有子進程"
        else
                echo "進程$1下的子進程是:"
                cat /tmp/$p1.txt
        fi
}

get_cpid $p

for c_p in `cat /tmp/$p.txt`
do
        get_cpid $c_p
done
[root@centos-04 tmp]# sh 68.sh  
Please input a pid: 1
進程1下的子進程是:
3161
3184
3191
6542
6564
6565
6567
6568
6569
6598
6606
6610
6835
6836
6838
6969
7046
7772
進程3161下沒有子進程
進程3184下沒有子進程
進程3191下沒有子進程
進程6542下沒有子進程
進程6564下沒有子進程
進程6565下沒有子進程
進程6567下沒有子進程
進程6568下沒有子進程
進程6569下的子進程是:
6652
進程6598下沒有子進程
進程6606下沒有子進程
進程6610下沒有子進程
進程6835下沒有子進程
進程6836下的子進程是:
7189
進程6838下沒有子進程
進程6969下的子進程是:
6970
6971
進程7046下的子進程是:
7052
7573
進程7772下沒有子進程
[root@centos-04 tmp]# 

給lamp環境增加項目  

[root@centos-04 tmp]# vim 69.sh
#!/bin/bash
#本腳本的功能是在LAMP環境中增加站點,包括apache配置、FTP增加用戶、Mysql增加庫和用戶
#作者:SYJ
#日期:2019-04-09

#網站目錄
webdir=/data/wwwroot

#ftp的虛擬用戶配置文件目錄
ftpudir=/etc/vsftpd/vuuser

#ftp虛擬用戶密碼文件
ftpuserfile=/root/login

#mysql命令行登錄root
mysqlc="/usr/local/mysql/bin/mysql -uroot -proot"

#apache虛擬主機配置文件
httpd_config_f="/usr/local/apache2/conf/extra/httpd-vhosts.conf"

#定義增加Mysql庫和用戶的函數
add_mysql_user()
{
        #生成隨機密碼
        mysql_p=`mkpasswd -s 0 -l 12`

        #將密碼保存到臨時文件里,這里的$pro為用戶自定義的項目名字
        echo "$pro $mysql_p" >/tmp/$pro.txt

        #這里使用嵌入文檔的形式(需頂格),將創建用戶並授權的命令傳遞給mysql
$mysqlc <<EOF
create database $pro;
grant all on $pro.* to "$pro"@'127.0.0.1' identified by "$mysql_p";
#下面這個EOF必須要頂格
EOF
}

#定義增加FTP用戶的函數
add_ftp_user()
{
        ftp_P=`mkpasswd -s 0 -l 12`
        echo "$pro" >> $ftpuserfile
        echo "$ftp_p" >> $ftpuserfile

        #將用戶、密碼文件轉換為密碼db文件
        db_load -T -t hash -f $ftpuserfile /etc/vsftpd/vsftpd_login.db
        cd $ftpudir

        #這里的aaa是一個文件,是之前的一個項目,可以作為配置模板
        cp aaa $pro

        #把里面的aaa改為新的項目名字
        sed -i "s/aaa/$pro/" $pro

        #重啟vsftpd服務
        /etc/init.d/vsftpd restart

}


#定義增加apache虛擬主機的函數
config_httpd()
{
        #增加網站根目錄,和域名保持一致,這里的$dom為用戶自定義的域名
        mkdir $webdir/$dom

        #將網站根目錄屬主和屬組設置為ftp用戶
        chown vsftpd:vsftpd $webdir/$dom

        #用嵌入文檔(需頂格),把虛擬主機配置寫入到配置文件里
cat >> $httpd_config_f <<EOF
<VirtualHost *.80>
        DocumentRoot $webdir/$dom
        ServerName $dom
        <Directory $webdir/$dom>
                AllowOverride none
                Require all granted
        </Directory>
EOF

        #重載apache服務
        /usr/local/apache2/bin/apachectl graceful
}

read -p "input the project name: " pro
read -p "input the domain: " dom

add_mysql_user
add_ftp_user
config_httpd

簡易計算器   

[root@centos-04 tmp]# vim 70.sh       
#!/bin/bash
#這個腳本用來實現簡易計算器
#作者:SYJ
#日期:2019-04-09

if [ $# -ne 3 ]
then
        echo "你輸出的參數個數不對,應該給3個參數"
        exit
fi

if_number()
{
        n1=`echo $1|sed 's/[0-9.]//g'`
        if [ -n "$n1" ]
        then
                echo "$1不是數字"
                exit
        fi

        if echo $1|grep -q '^\.'
        then
                echo "數字$1不合法"
                exit
        fi
}

if_number $1
if_number $3

case $2 in
        +)
                echo "$1+$3"|bc
                ;;
        -)
                echo "$1-$3"|bc
                ;;
        \*)
                echo "$1*$3"|bc
                ;;
        /)
                echo "scale=2;$1/$3"|bc
                ;;
        *)
                echo "你給出的格式不對,第二個參數只能是+,-,*,/"
                ;;
esac

[root@centos-04 tmp]# sh 70.sh 1 2 3
你給出的格式不對,第二個參數只能是+,-,*,/
[root@centos-04 tmp]# sh 70.sh 1 + 3
4
[root@centos-04 tmp]# sh 70.sh 1 - 3
-2
[root@centos-04 tmp]# sh 70.sh 1 '*' 3
3
[root@centos-04 tmp]# sh 70.sh 1 \* 3  
3
[root@centos-04 tmp]# sh 70.sh 1 / 3
.33
[root@centos-04 tmp]# 

判斷文件並計算單詞

[root@centos-04 tmp]# vim 71.sh
#!/bin/bash
#這個腳本用來判斷文件是否存在並計算單詞個數
#作者:SYJ
#日期2019-04-09

if [ $# -ne 2 ]
then
        echo "請提供兩個參數,第一個參數是目錄名字,第二個參數是單詞"
        exit
fi

cd $1
for f in `ls .`
do
        if [ -d $f ]
        then
                if [ -f $f/test.txt ]
                then

                        n=`grep -cw "$2" $f/test.txt`
                        echo "$1/$f目錄下面有test.txt, 該test.txt里面有$n個$2"
                fi
        fi
done

打印正方形

[root@centos-04 tmp]# for i in `seq 1 3`; do for j in `seq 1 3`; do echo -n "口 "; done; echo ; done
口 口 口 
口 口 口 
口 口 口 
[root@centos-04 tmp]# 
[root@centos-04 tmp]# vim 72.sh
#!/bin/bash
#這個腳本用來打印正方形
#作者:SYJ
#日期:2019-04-09

while :
do

        read -p "Please input a number: " n
        n1=`echo $n|sed 's/[0-9]//g'`
        if [ -n "$n1" ]
        then
                echo "$n is not a number"
                continue
        else
                break
        fi
done

for i in `seq 1 $n`
do
        for j in `seq 1 $n`
        do
                echo -n "口 "
        done
        echo
done

[root@centos-04 tmp]# sh 72.sh 
Please input a number: 4
口 口 口 口 
口 口 口 口 
口 口 口 口 
口 口 口 口 
[root@centos-04 tmp]# sh 72.sh 
Please input a number: 5
口 口 口 口 口 
口 口 口 口 口 
口 口 口 口 口 
口 口 口 口 口 
口 口 口 口 口 
[root@centos-04 tmp]# 

問候用戶

[root@centos-04 tmp]# awk -F ':' '{print "Hello, "$1", your uid is "$3"."}' /etc/passwd
Hello, root, your uid is 0.
Hello, bin, your uid is 1.
Hello, daemon, your uid is 2.
Hello, adm, your uid is 3.
Hello, lp, your uid is 4.
Hello, sync, your uid is 5.
[root@centos-04 tmp]# vim 73.sh
#!/bin/bash
#這個腳本用來問候用戶
#作者:SYJ
#日期:2019-04-09
cat /etc/passwd |while read line
do
        username=`echo $line|awk -F ':' '{print $1}'`
        uid=`echo $line|awk -F ':' '{print $3}'`
        echo "Hello,$username,your uid is $uid"
done
[root@centos-04 tmp]# sh 73.sh 
Hello,root,your uid is 0
Hello,bin,your uid is 1
Hello,daemon,your uid is 2
Hello,adm,your uid is 3

格式化輸出xml

[root@centos-04 tmp]# vim 74.sh
#!/bin/bash
#這個腳本用來格式化xml文件
#作者:SYJ
#日期:2019-04-09

sed '/<!--.*-->/d' test.xml > test2.xml #注釋在一行的情況
egrep -n '<!--|\-\->' test2.xml |awk -F ':' '{print $1}' > /tmp/line_number1.txt #注釋在兩行的情況計算行號
n=`wc -l /tmp/line_number1.txt|awk '{print $1}'`
n1=$[$n/2]
for i in `seq 1 $n1`
do
        j=$[$i*2]
        k=$[$j-1]
        x=`sed -n "$k"p /tmp/line_number1.txt`
        y=`sed -n "$j"p /tmp/line_number1.txt`
        sed -i "$x,$y"d test2.xml
done

grep -n 'artifactItem>' test2.xml |awk '{print $1}' |sed 's/://' > /tmp/line_number2.txt
n2=`wc -l /tmp/line_number2.txt|awk '{print $1}'`

get_value()
{
        sed -n "$1,$2"p test2.xml|awk -F '<' '{print $2}' |awk -F '>' '{print $1,$2}' > /tmp/value.txt

        cat /tmp/value.txt|while read line
        do
                x=`echo $line|awk '{print $1}'`
                y=`echo $line|awk '{print $2}'`
                echo artifactItem:$x:$y
        done
}

n3=$[$n2/2]
for j in `seq 1 $n3`
do
        m1=$[$j*2-1]
        m2=$[$j*2]
        nu1=`sed -n "$m1"p /tmp/line_number2.txt`
        nu2=`sed -n "$m2"p /tmp/line_number2.txt`
        nu3=$[$nu1+1]
        nu4=$[$nu2-1]
        get_value $nu3 $nu4
done

小函數

[root@centos-04 tmp]# vim 75.sh
#!/bin/bash
#這個腳本用來寫一個小函數
#作者:SYJ
#日期:2019-04-09

f_judge()
{
        if [ -d /home/log ]
        then
                #find /home -name "tmp*" |xargs -i mv {} /home/log/
                find /home -name "tmp*" -exec mv {} /home/log/ \;
        else
                mkdir /home/log
                exit
        fi

}

f_judge

批量殺進程

殺死tomcat進程

[root@centos-04 tmp]# ps aux |grep tomcat |awk '{print $2}' |xargs kill
[root@centos-04 tmp]# vim 76.sh
#!/bin/bash
#這個腳本用來批量殺tomcat進程
#作者:SYJ
#日期:2019-04-09
cat > kill_tomcat.expect <<EOF
#!/usr/bin/expect
set passwd [lindex \$argv 0]
set host [lindex \$argv 1]
spawn ssh root@\$host

expect {
        "yes/no" { send "yes\r"; exp_continue}
        "password:" { send "\$passwd\r" }

}

expect "]*"
send "killall java\r"
expect "]*"
send "exit\r"
EOF

chmod a+x kill_tomcat.expect

cat ip-pwd.ini|while read line
do
        ip=`echo $line |awk -F ',' '{print $1}'`
        pw=`echo $line |awk -F ',' '{print $3}'`
        ./kill_tomcat.expect $pw $ip
done

查找老日志打包  

[root@centos-04 tmp]# vim 77.sh    
#!/bin/bash
#這個腳本用來查找老日志打包
#作者:SYJ
#日期:2019-04-09

cd /data/log
fin . -type f -name "*.log" -mtime +3 > /tmp/old_log

d=`date +%F`
tar czf $d.tar.gz  `cat /tmp/old_log|xargs`
rsync -a $d.tar.gz 192.168.1.2:/data/log/
cat /tmp/old_log|xargs rm

處理文本

[root@centos-04 tmp]# vim 78.sh
#!/bin/bash
#這個腳本用來處理文本
#作者:SYJ
#日期:2019-04-09

for w in `awk -F ':' '{print $1}' 3.txt |sort |uniq`
do
        echo "[$w]"
        awk -v w2=$w -F ':' '$1==W2 {print $2}' 3.txt
done

批量刪除日志

[root@centos-04 tmp]# vim 79.sh
#!/bin/bash
#這個腳本用來刪除老日志
#作者:SYJ
#日期:2019-04-09

dir1=/opt/cloud/log/
dir2=/opt/cloud/instance/

if [ -d $dir1 ]
then
        find $dir1 -type f -mtime +7 |xargs rm
elif [ -d $dir2 ]
then
        find $dir2 -name "*.log" -type f -mtime +15 |xargs rm
fi

房貸計算器

#!/bin/bash
#這個腳本用來實現簡易的房貸計算器
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-12

read -p "請輸入貸款總額(單位:萬元):" sum_w
read -p "請輸入貸款年利率(如年利率為6.5%,直接輸入6.5):" y_r
read -p "請輸入貸款年限(單位:年):" y_n
echo "貸款計算方式:"
echo "1)等額本金計算法"
echo "2)等額本息計算法"
read -p "請選擇貸款方式(1|2)" type
#貸款總額
sum=`echo "scale=2;$sum_w*10000 " | bc -l`
#年利率
y_r2=`echo "scale=6;$y_r/100 " | bc -l`
#月利率
m_r=`echo "scale=6;$y_r2/12 " | bc -l`
#期數
count=$[$y_n*12]
echo "期次 本月還款額 本月利息 未還款額"

jin()
{
    #月還款本金m_jin=貸款總額sum/期數count
    m_jin=`echo "scale=2;($sum/$count)/1 " | bc -l`
    #定義未還本金r_jin(第一期應該是貸款總額)
    r_jin=$sum
    for((i=1;i<=$count;i++))
    do
        #本月利息m_xi=剩余本金*月利率
        m_xi=`echo "scale=2;( $r_jin*$m_r)/1"|bc -l`
        #本月還款m_jinxi=月還本金m_jin+本月利息m_xi
        m_jinxi=`echo "scale=2;( $m_jin+$m_xi)/1"|bc -l`
        #已還款本金jin=月還本金m_jin*期數i
        jin=`echo "scale=2;( $m_jin*$i)/1"|bc -l`
        #剩余本金r_jin=貸款總額sum-已還本金jin
        r_jin=`echo "scale=2;( $sum-$jin)/1"|bc -l`
        if [ $i -eq $count ]
        then
            #最后一月的還款額應該是每月還款本金+本月利息+剩余本金
            m_jinxi=`echo "scale=2;( $m_jin+$r_jin+$m_xi)/1"|bc -l`
            #最后一月的剩余本金應該是0
            r_jin=0
        fi
        echo "$i  $m_jinxi  $m_xi  $r_jin"
     done   
}

xi()
{
    #每期還款m_jinxi=(貸款總額sum*月利率m_r*((1+月利率m_r)^期數count))/(((1+月利率m_r)^期數count)-1)
    m_jinxi=`echo "scale=2;(($sum*$m_r*((1+$m_r)^$count))/(((1+$m_r)^$count)-1))/1 " | bc -l`
    #定義未還本金r_jin(第一期應該是貸款總額)
    r_jin=$sum
    for((i=1;i<=$count;i++))
    do
        #本期利息m_xi=剩余本金r_jin*月利率m_r
        m_xi=`echo "scale=2;( $r_jin*$m_r)/1"|bc -l`
        #本期本金m_jin=本期本息m_jinxi-本期利息m_xi
        m_jin=`echo "scale=2;($m_jinxi-$m_xi)/1 " | bc -l`
        #未還本金r_jin=上期未還本金r_jin-本期應還本金m_jin
        r_jin=`echo "scale=2;($r_jin-$m_jin)/1 " | bc -l`
        if [ $i -eq $count ]
        then
            #最后一月本息m_jinxi=本期本金m_jin+未還本金r_jin
            m_jinxi=`echo "scale=2;($m_jin+$r_jin)/1 " | bc -l`
            #最后一月的剩余本金應該是0
            r_jin="0.00"
        fi
        echo "$i $m_jinxi $m_xi $r_jin"
    done
}

case $type in
    1) 
        jin
        ;;
    2) 
	xi
        ;;
    *) 
	exit 1
        ;;
esac

監控磁盤  

[root@centos-04 tmp]# vim 81.sh
#!/bin/bash
#這個腳本用來監控磁盤IO
#作者:SYJ
#日期:2019-04-10

if ! while iostat &>/dev/null
then
        yum install -y sysstat
fi

while :
do
        t=`date +%T`
        iostat -xd 1 5 |grep '^sda' > /tmp/io.log
        sum=`awk '{sum=sum+$NF} END {print sum}' /tmp/io.log`
        a=`echo "scale=2;$sum/5" |bc`
        b=`echo $a|cut -d . -f 1`
        if [ $b -gt 90 ]
        then
                mysql -uroot -pxxx -e "show processlist" > mysql_$t.log
        fi
        sleep 1
done

 查看tomcat日志

#!/bin/bash
#這個腳本用來查看Tomcat日志
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-12

LANG=en
logfile="/opt/TOM/$1/logs/catalina.out"

#將當天的英文月、數字日期、數字年作為變量賦值給d_mdy
d_mdy=`date "+%b %d, %Y"`

#判斷參數個數
if [ $# -ne 2 ] && [ $# -ne 3 ]
then
    echo "你提供的參數個數不對,請提供2個或者3個參數。例:sh $0 t1 08:01:00 14:00:00" 
    exit 1
fi

#判斷第一個參數是否符合要求
if ! echo $1|grep -qE '^t1$|^t2$|^t3$|^t4$'
then
    echo "第一個參數必須是t1、t2、t3或t4"
    exit 1
fi 

#判斷時間有效性
judge_time()
{
    date -d "$1" +%s &>/dev/null
    if [ $? -ne 0 ]
    then
        echo "你提供的時間$1格式不正確"
        exit 1
    fi
}

#將24小時制時間轉換為12小時
tr_24_12()
{
    date -d "$1" +%r
}

#判斷提供的時間點是否在日志中出現
judge_time_in_log()
{
    if ! grep -q "$d_mdy $(tr_24_12 $1)" $logfile
        then
            echo "你提供的時間$1在日志$logfile中不曾出現,請換一個時間點"
            exit 1
        fi    
}

#判斷第2個參數是否合法
judge_time $2

#判斷起始時間點是否出現在日志里
judge_time_in_log $2

#如果提供第3個參數
if [ $# -eq 3 ]
then
    #判斷第3個參數是否合法
    judge_time $3

    #判斷起始時間是否早於結束時間
    t1=`date -d "$2" +%s`
        t2=`date -d "$3" +%s`
        if [ $t2 -lt $t1 ]
        then
            echo "你提供的時間$2比$3要晚,應該把早的時間放到前面"
            exit
        fi

        #判斷提供的結束時間點是否出現在日志中
        judge_time_in_log $3
fi


#取起始時間所在行行號
begin_n=`grep -n "$d_mdy $(tr_24_12 $2)" $logfile|head -1|awk -F ':' '{print $1}'`

#取結束時間所在行行號,並用sed截取日志內容
if [ $# -eq 3 ]
then
    n=`grep -n "$d_mdy $(tr_24_12 $3)" $logfile|tail -1|awk -F ':' '{print $1}'`
    #結束日期所在行的下一行才是日志的內容
    end_n=$[$n+1]
    sed -n "$begin_n,$end_n"p $logfile
else
    sed -n "$begin_n,$"p $logfile
fi

打印城市名字

打印數組

[root@centos-04 tmp]# a=(1,2,3,4,5)
[root@centos-04 tmp]# echo ${a[@]}
1,2,3,4,5
[root@centos-04 tmp]# 
[root@centos-04 tmp]# a="1 2 3" 
[root@centos-04 tmp]# echo $a
1 2 3
[root@centos-04 tmp]# b=($a)(把帶空格的字符串變成數組加一個()就行了,這樣b就變成一個數組了)
[root@centos-04 tmp]# echo $b
1
[root@centos-04 tmp]# echo ${b[@]}
1 2 3
[root@centos-04 tmp]#   

獲取數組的個數

[root@centos-04 tmp]# echo ${#b[@]}
3
[root@centos-04 tmp]# 
[root@centos-04 tmp]# vim 83.sh 
#!/bin/bash
#這個腳本用來打印城市名字
#作者:SYJ
#日期:2019-04-10
read -p "輸入不少於5個城市的名字,用空格分隔開。" name

n=`echo $name|awk '{print NF}'`
if [ $n -lt 5 ]
then
        echo "請輸入至少5個城市的名字。"
        exit
fi

city=($name)

#echo ${city[@]}

for i in `seq 0 $[${#city[@]}-1]`
do
        echo ${city[$i]}
done
[root@centos-04 tmp]# sh 83.sh  
輸入不少於5個城市的名字,用空格分隔開。1 2 3 4 5
1
2
3
4
5
[root@centos-04 tmp]# 

代碼上線  

[root@centos-04 tmp]# vim 84.sh   
#!/bin/bash
#這個腳本用來代碼上線
#作者:SYJ
#日期:2019-04-10
dir=/data/wwwroot/www.aaa.com
B_Ip=1.1.1.1
C_Ip=2.2.2.2

rs()
{
        rsync -azP --exclude="logs" \
        --exclude="tmp" --exclude="upload" \
        --exclude="caches" $dir/ $1:$dir/

}

read -p "該腳本將會把本機的$dir下的文件同步到$B_IP和$c_ip上,是否要繼續?y|n" c

case $c in
        y|Y)
                rs B_IP
                rs C_IP
                ;;
        n|N)
                exit
                ;;
        *)
                echo "你只能輸入y或者n。"
                ;;
esac

統計並發量  

[root@centos-04 tmp]# date -d "-1 second" +%d/%b/%Y:%T
10/4月/2019:23:15:59
[root@centos-04 tmp]# 
[root@centos-04 tmp]# vim 85.sh
#!/bin/bash
#這個腳本用來計算網站並發量
#作者:SYJ
#日期:2019-04-10

LANG=en
t=`date -d "-1 second" + %d/%b/%Y:%T`
log=/data/logs/www.aaa.com_access.log

tail -1000 $log |grep -c "$t"

關閉服務

chkconfig --list查看到服務的狀態(查看第三列)

[root@centos-04 tmp]# chkconfig --list

注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。 

      要列出 systemd 服務,請執行 'systemctl list-unit-files'。
      查看在具體 target 啟用的服務請執行
      'systemctl list-dependencies [target]'。

netconsole      0:關    1:關    2:關    3:關    4:關    5:關    6:關
network         0:關    1:關    2:開    3:開    4:開    5:開    6:關
nginx           0:關    1:關    2:開    3:開    4:開    5:開    6:關
[root@centos-04 tmp]# 
[root@centos-04 tmp]# LANG=en
[root@centos-04 tmp]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@centos-04 tmp]# 
[root@centos-04 tmp]# chkconfig --list |grep '3:on' |awk '{print $1}'

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

network
nginx
[root@centos-04 tmp]# 
[root@centos-04 tmp]# vim 86.sh  
#!/bin/bash
#這個腳本用來關閉服務
#作者:SYJ
#日期:2019-04-10

LANG=en

while :
do
        chkconfig --list 2>/dev/null|grep '3:on' |awk '{print $1}' > /tmp/on_sev.txt
        echo -e "\033[32m系統里開啟了這些服務: \033[0m"
        cat /tmp/on_sev.txt
        echo 
        read -p "Please select a service from this list." s

        if ! grep -qw "$s" /tmp/on_sev.txt
        then
                echo -e "\033[31m The service is not in the service list.\33[0m"
                continue
        fi
        chkconfig $s off
        break
done
~                                                                                                                               
~                                                                                                                               
~                                                                                                                               
~                                                                                                                               
~                                                                                                                               
~                                                                                                                               
~                                                                                                                               
~                                                                                                                               
~                                                                                                                               
~                                                                                                                               
"86.sh" 23L, 474C 已寫入                                                                                      
[root@centos-04 tmp]# sh 86.sh 
系統里開啟了這些服務: 
network
nginx

Please select a service from this list.nginx
[root@centos-04 tmp]# sh 86.sh 
系統里開啟了這些服務: 
network

Please select a service from this list.

 徹底關閉tomcat服務

[root@centos-04 tmp]# vim 87.sh
#!/bin/bash
#這個腳本用來徹底殺死Tomcat進程
#作者:SYJ
#日期:2019-04-10

dir=/usr/local/tomcat/bin/

java_pc()
{
        pgrep java|wc -l
}

cd $dir
./shutdown.sh

count=0

while [ $count -lt 5 ]:
do
        n=`java_pc`
        if [ $n -gt 0 ]
        then
                killall java
                count=$[$count+1]
                sleep 1
        else
                break
        fi
done

n=`java_pc`
if [ $n -gt 0 ]
then
        killall -9 java
fi

n=`java_pc`
if [ $n -gt 0 ]
then
        echo "Tomcat can not killed" 
        exit
fi

cd $dir
./startup.sh

去掉文件名后綴  

[root@centos-04 tmp]# vim 88.sh
#!/bin/bash
#這個腳本用來去掉文件名后綴
#作者:SYJ
#日期:2019-04-10

for f in `ls -d ./*.bak`
do
#       mv $f `echo $f|sed 's/.bak$//'`
        f1=`echo $f|awk -F '.bak$' {print $1}`
        mv $f $f1
done

檢查域名是否到期  

[root@centos-04 tmp]# whois baidu.com |grep 'Expiration Date'
Registrar Registration Expiration Date: 2026-10-11T00:00:00-0700
[root@centos-04 tmp]# 
#!/bin/bash
#這個腳本用來檢查域名是否到期
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-14

mail_u=admin@admin.com
#當前日期時間戳,用於和域名的到期時間做比較
t1=`date +%s`

#檢測whois命令是否存在,不存在則安裝jwhois包
is_install_whois()
{
    which whois >/dev/null 2>/dev/null
    if [ $? -ne 0 ]
    then
	yum install -y epel-release
        yum install -y jwhois
    fi
}

notify()
{
    #e_d=`whois $1|grep 'Expiry Date'|awk '{print $4}'|cut -d 'T' -f 1`
    e_d=`whois $1|grep 'Expiration'|tail -1 |awk '{print $5}' |awk -F 'T' '{print $1}'`
    #如果e_d的值為空,則過濾關鍵詞'Expiration Time'
    if [ -z "$e_d" ]
    then
        e_d=`whois $1|grep 'Expiration Time'|awk '{print $3}'`
    fi
    #將域名過期的日期轉化為時間戳
    e_t=`date -d "$e_d" +%s`
    #計算一周一共有多少秒
    n=`echo "86400*7"|bc`
    e_t1=$[$e_t-$n]
    e_t2=$[$e_t+$n]
    if [ $t1 -ge $e_t1 ] && [ $t1 -lt $e_t ]
    then
        python mail.py  $mail_u "Domain $1 will  to be expired." "Domain $1 expire date is $e_d."
    fi
    if [ $t1 -ge $e_t ] && [ $t1 -lt $e_t2 ]
    then
        python mail.py $mail_u "Domain $1 has been expired" "Domain $1 expire date is $e_d." 
    fi
}

#檢測上次運行的whois查詢進程是否存在
#若存在,需要殺死進程,以免影響本次腳本執行
if pgrep whois &>/dev/null
then
    killall -9 whois
fi

is_install_whois

for d in aaa.com bbb.com  aaa.cn
do
    notify $d
done

自動密鑰認證  

#!/bin/bash
#這個腳本用來自動配置密鑰認證
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-14

read -p "輸入一個IP地址: " ip
read -p "輸入此機器的root密碼: " pasd

is_install()
{
    if ! rpm -q $1 &>/dev/null
    then
	yum install -y $1
    fi
}

is_install openssh-clients
is_install expect

if [ ! -f ~/.ssh/id_rsa.pub ]
then
    echo -e "\n" |ssh-keygen  -P ''
fi

cat > key.expect <<EOF
#!/usr/bin/expect
set host [lindex \$argv 0]
set passwd [lindex \$argv 1]
spawn ssh-copy-id root@\$host
expect {
    "yes/no" { send "yes\r"; exp_continue}
    "password:" { send "\$passwd\r" }
}
expect eof
EOF

chmod a+x key.expect

./key.expect $ip $pasd  

普通我們將本機的ssh公鑰放到另一台機器的方法(id_rsa.pub文件內容放到對方機器的authorized_keys里面)

root@centos-04 tmp]# cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7oSVWcFIwNAku8g3gXBD79HH1H/7hZg5hshKyQWvPT7q+pzFGiZ0H3zZgG0kHU6j9BB1t/NAooiKmOF5sJmzuDed6
rBeLOw210kG7r9cfU/yHrUljTXjETQQVrlG7LRrW6kwNOoXaJceau60s2P9b91hN3unLqu+wscAC6CfCyIJm2epniltMOgBrRXx7ghOQ3XVkvC84N/9koyC8fypZNeTb
7l/QzeGZNsXDUVChuP9z9mCMaedGGvhfGfPY7qZO0b0aeU8didYSXubekp4rI8JUuPuhOSK49+TxXe0QivtByiZycM69D1XZU5Pilr4f3gbFQc4bSBpbgXkKVpu7 root@centos-04 [root@centos-04 tmp]# vim ~/.ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7oSVWcFIwNAku8g3gXBD79HH1H/7hZg5hshKyQWvPT7q+pzFGiZ0H3zZgG0kHU6j9BB1t/NAooiKmOF5sJmzuDed6 rBeLOw210kG7r9cfU/yHrUljTXjETQQVrlG7LRrW6kwNOoXaJceau60s2P9b91hN3unLqu+wscAC6CfCyIJm2epniltMOgBrRXx7ghOQ3XVkvC84N/9koyC8fypZNeTb 7l/QzeGZNsXDUVChuP9z9mCMaedGGvhfGfPY7qZO0b0aeU8didYSXubekp4rI8JUuPuhOSK49+TxXe0QivtByiZycM69D1XZU5Pilr4f3gbFQc4bSBpbgXkKVpu7 roo t@centos-04  

我們也可以用ssh-copy-id命令實現,輸入對方的用戶名和ip

[root@centos-04 tmp]# ssh-copy-id root@1.1.1.1
[root@centos-04 tmp]# vim 1.expect                   
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd [lindex $argv 1]
spawn ssh-copy-id root@$host
expect {
        "yes/no" { send "yes\r"; exp_continue}
        "password:" { send "$passwd\r" }
}
expect eof
[root@centos-04 tmp]# ./1.expect 127.0.0.1 root
spawn ssh-copy-id root@127.0.0.1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.
                (if you think this is a mistake, you may want to use -f option)

expect: spawn id exp6 not open
    while executing
"expect eof"
    (file "./1.expect" line 9)
[root@centos-04 tmp]# 
[root@centos-04 tmp]# ssh-copy-id root@127.0.0.1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.
                (if you think this is a mistake, you may want to use -f option)

[root@centos-04 tmp]# ssh-copy-id -f root@127.0.0.1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@127.0.0.1'"
and check to make sure that only the key(s) you wanted were added.

[root@centos-04 tmp]# 
[root@centos-04 tmp]# cat /root/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7oSVWcFIwNAku8g3gXBD79HH1H/7hZg5hshKyQWvPT7q+pzFGiZ0H3zZgG0kHU6j9BB1t/NAooiKmOF5sJmzuDed6rBeLOw210kG7r9cfU/
yHrUljTXjETQQVrlG7LRrW6kwNOoXaJceau60s2P9b91hN3unLqu+wscAC6CfCyIJm2epniltMOgBrRXx7ghOQ3XVkvC84N/9koyC8fypZNeTb7l/QzeGZNsXDUVChuP9z9mCMaedGGvhfGfPY
7qZO0b0aeU8didYSXubekp4rI8JUuPuhOSK49+TxXe0QivtByiZycM69D1XZU5Pilr4f3gbFQc4bSBpbgXkKVpu7 root@centos-04 [root@centos-04 tmp]# [root@centos-04 tmp]# cat /root/.ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7oSVWcFIwNAku8g3gXBD79HH1H/7hZg5hshKyQWvPT7q+pzFGiZ0H3zZgG0kHU6j9BB1t/NAooiKmOF5sJmzuDed6rBeLOw210kG7r9cfU/
yHrUljTXjETQQVrlG7LRrW6kwNOoXaJceau60s2P9b91hN3unLqu+wscAC6CfCyIJm2epniltMOgBrRXx7ghOQ3XVkvC84N/9koyC8fypZNeTb7l/QzeGZNsXDUVChuP9z9mCMaedGGvhfGfPY
7qZO0b0aeU8didYSXubekp4rI8JUuPuhOSK49+TxXe0QivtByiZycM69D1XZU5Pilr4f3gbFQc4bSBpbgXkKVpu7 root@centos-04 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7oSVWcFIwNAku8g3gXBD79HH1H/7hZg5hshKyQWvPT7q+pzFGiZ0H3zZgG0kHU6j9BB1t/NAooiKmOF5sJmzuDed6rBeLOw210kG7r9cfU/
yHrUljTXjETQQVrlG7LRrW6kwNOoXaJceau60s2P9b91hN3unLqu+wscAC6CfCyIJm2epniltMOgBrRXx7ghOQ3XVkvC84N/9koyC8fypZNeTb7l/QzeGZNsXDUVChuP9z9mCMaedGGvhfGfPY
7qZO0b0aeU8didYSXubekp4rI8JUuPuhOSK49+TxXe0QivtByiZycM69D1XZU5Pilr4f3gbFQc4bSBpbgXkKVpu7 root@centos-04 [root@centos-04 tmp]#   

查看一個命令由那個包裝的

[root@centos-04 tmp]# which ssh-copy-id 
/usr/bin/ssh-copy-id
[root@centos-04 tmp]# rpm -qf /usr/bin/ssh-copy-id 
openssh-clients-7.4p1-16.el7.x86_64
[root@centos-04 tmp]# 

部署MySQL主從

[root@centos-04 tmp]# vim 91.sh
#!/bin/bash
#這個腳本用來配置MySQL主從同步
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-17

#!/bin/bash
master_ip=192.168.100.12
slave_ip=192.168.100.13
mysqlc="mysql -uroot -paminglinux"

check_ok()
{
    if [ $? -ne 0 ]
    then
        echo "$1 出錯了。"
        exit 1
    fi
}

f_exist()
{
    d=`date +%F%T`
    if [ -f $1 ]
    then
        mv $1 $1_$d
    fi
}

## 設置主mysql配置
if ! grep '^server-id' /etc/my.cnf
then

if ! grep '^log-bin.*=.*' /etc/my.cnf
then
    sed -i '/^\[mysqld\]$/a\log-bin = aminglinux' /etc/my.cnf
fi

sed -i '/^log-bin.*/a\binlog-ignore-db = mysql ' /etc/my.cnf

/etc/init.d/mysqld restart
check_ok "主上重啟mysql"

## 登錄mysql,授權用戶、鎖表以及show master status。
$mysqlc <<EOF
    grant replication slave on *.* to 'repl'@$slave_ip identified by 'yourpassword';
    flush tables with read lock;
EOF
$mysqlc -e "show master status" > /tmp/master.log
file=`tail -1 /tmp/master.log|awk '{print $1}'`
pos=`tail -1 /tmp/master.log|awk '{print $2}'`

## 創建在從上配置和操作的腳本
f_exist /tmp/slave.sh

cat > /tmp/slave.sh << EOF
#!/bin/bash
if ! grep '^server-id' /etc/my.cnf
then
    sed -i '/^\[mysqld\]$/a\server-id = 1002' /etc/my.cnf
fi

/etc/init.d/mysqld restart
check_ok "從上重啟mysql"

$mysqlc  <<EOF
    stop slave;
    start slave;
EOF 
EOF

## 創建傳輸slave.sh的expect腳本
f_exist /tmp/rs_slave.expect

cat > /tmp/rs_slave.expect <<EOF
#!/usr/bin/expect
set passwd "aminglinux"
spawn rsync -a /tmp/slave.sh root@$slave_ip:/tmp/slave.sh
expect {
    "yes/no" { send "yes\r"}
    "password:" { send "\$passwd\r" }
}
expect eof
EOF

## 執行expect腳本
chmod +x /tmp/rs_slave.expect
/tmp/rs_slave.expect
check_ok "傳輸slave.sh"

## 創建遠程執行命令的expect腳本
f_exist /tmp/exe.expect

cat > /tmp/exe.expect <<EOF
#!/usr/bin/expect
set passwd "aminglinux"
spawn ssh root@$slave_ip
expect {
    "yes/no" { send "yes\r"}
    "password:" { send "\$passwd\r" }
}
expect "]*"
send "/bin/bash /tmp/slave.sh\r"
expect "]*"
send "exit\r"
EOF

## 執行expect腳本
chmod +x /tmp/exe.expect
/tmp/exe.expect
check_ok "遠程執行slave.sh"

## 主上解鎖表
$mysqlc -e "unlock tables"

管理docker  

[root@centos-04 tmp]# vim 92.sh
#!/bin/bash
#這個腳本用來管理docker容器
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-17

while true
do
    read -p "請輸入你要執行的操作:(stop/start/rm) " opt
    if [ -z "$opt" ]
    then
        echo "請輸入要執行的操作。"
        continue
    else
        break
    fi
done

docker ps -a |awk '{print $1}' > /tmp/id.txt
case $opt in
  stop)
    for id in `cat /tmp/id.txt`
    do
        docker stop $id
    done
    ;;
  start)
    for id in `cat /tmp/id.txt`
    do
        docker start $id
    done
  rm)
    for id in `cat /tmp/id.txt`
    do
        read -p "將要刪除容器$id,是否繼續?(y|n)" c
        case $c in
          y|Y)
            docker rm -f $id
            ;;
          n|N)
            echo "容器$id不會被刪除。"
            ;;
          *)
            echo "你只能輸入'y'或者'n'。"
            ;;
        esac
    done
  *)
    echo "你只能輸入start/stop/rm。"
    ;;
esac

安裝配置samba  

[root@centos-04 tmp]# vim 93.sh
#!/bin/bash
#這個腳本用來一鍵安裝並配置samba
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-17

if [ "$#" -ne 1 ]
then
    echo "運行腳本的格式為:$0 /dir/"
    exit 1
else
    if ! echo $1 |grep -q '^/.*'
    then
        echo "請提供一個絕對路徑。"
        exit 1
    fi
fi

if ! rpm -q samba >/dev/null
then
    echo "將要安裝samba"
    sleep 1
    yum install -y samba
    if [ $? -ne 0 ]
    then
        echo "samba安裝失敗"
        exit 1
    fi
fi

cnfdir="/etc/samba/smb.conf"
cat >> $cnfdir <<EOF
[share]
        comment = share all
        path = $1
        browseable = yes
        public = yes
        writable = no
EOF

if [ ! -d $1 ]
then
    mkdir -p $1
fi

chmod 777 $1
echo "test" > $1/test.txt

#假設系統為CentOS7
systemctl start smb
if [ $? -ne 0 ]
then
    echo "samba服務啟動失敗,請檢查配置文件是否正確。"
else
    echo "samba配置完畢,請驗證。"
fi

批量查看多台機器負載  

指定文件名字(aaa)的秘鑰

[root@centos-04 tmp]# ssh-keygen -f /tmp/aaa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /tmp/aaa.
Your public key has been saved in /tmp/aaa.pub.
The key fingerprint is:
SHA256:1P7xXAJP++vRPNHXETWP/3LZDHJRHVHdxoNUewrA868 root@centos-04
The key's randomart image is:
+---[RSA 2048]----+
|         .. ..oB%|
|         .o. . +@|
|        . .oo +++|
|       . .  .= ==|
|        S . o.B.*|
|           . *.BB|
|            ..++O|
|            E  o+|
|              .o |
+----[SHA256]-----+
[root@centos-04 tmp]# ls
1.expect  3191.txt  6542.txt  6569.txt  66.sh     68.sh     71.sh  76.sh     80.sh  86.sh  93.sh    nginx_proxy_tmp
1.txt     3.txt     6564.txt  6598.txt  67.sh     6969.txt  72.sh  7772.txt  81.sh  87.sh  aaa      on_sev.txt
2.txt     456.log   6565.txt  65.sh     6835.txt  69.sh     73.sh  77.sh     83.sh  88.sh  aaa.pub  pid.txt
3161.txt  4.txt     6567.txt  6606.txt  6836.txt  7046.txt  74.sh  78.sh     84.sh  91.sh  c.txt    proxy.log
3184.txt  64.sh     6568.txt  6610.txt  6838.txt  70.sh     75.sh  79.sh     85.sh  92.sh  md5.txt
[root@centos-04 tmp]# cat aaa(私鑰)
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,6471B3E0E269F92672EABB4201EC8314

PF7gcBUGljlSYCX1IazbSPaqRkqA3M5BuDiBe9YkUFXiTTWOcx1eo0aus/GGK4P1
kb1wJEoa7kVjSlB6h0Ed9d2oB08faRKj6yzLGdpQp4I+ZKQMYsXii+QhDT3R0c2c
c9zImVq5Jub6DZtYP7LTs7k2A7Sb1GIio+S3cEEZ1hun84HriFWfViiLKD43crDb
jHTuHv7f4V+J85XHjljB+k7DgZxGx7N+F+/DNjacR/AXP48rq5SD3DWRk0IOmS9T
Iihf7v6euKG3SlKeqRCuJbseeCDVIRNcOuMzrHCzYdj2zAOfNTYNtB9MdEa3rucT
Cg4xaMyJDwDMtGCI/swQ/vTDV/Of/nOC359HHFdFOEdurXULH3nPsU1/qfElLpYe
ims5JYnA5cfvo75iMQowzYHv+VZaKl7lUYldfE5z/n/tgcd5O8HxnzEm1pV1SC6L
dkNM2bjMxlIcpW+nZGLztcF9A5MUy4lRTcjYhgjqTM9LPMpDA1rnQ9FajxQoP1IU
hYsGYCjbjkvy41xtmDH2EgaIoZBI+VDmE6EKlgh4E2H0xROvg1bgYjauF2m2ZVrS
A8tSy7/ox6JB6imUoc8taxGAUP9Cr8Nbe3l7iFkAtqeW+MSFZ7ho53iupUFirmYO
TwDzro9A1z8n4lMc0hn+ZMhVnhSGwr8N+ON7U4puBVRwoM3jyH7HlTbWxsGlDGqG
4A19p8SgJKStfbDSgIICvGE7hPeegpEMeo/syWXAKj8jNBGmvKg5dG4rPMPGKecI
YTETnYO0Q5a5YyEw8gXu16R7Zbs9YYYbq+pKTQjQC5AeW+kokl416/qG1oF7Yizi
/JZ6lVBTLw3z96/xvd6VW0ZpVCshGL1iZr1k3R4mmO+brNyuCgKjzaS6iOCIc4Xz
doZdEEJsJqiEDbo1lzMc5cZGhNVVdcGiN0BmnaoYWl4OtMX00vAz/ECTyiZt+en+
y1sLR/RpCVzSGACLYwgPHJY/2FQWAN6RDhmQQcCQTWPo7SZdY6j2lM3fmG1MV6Lc
Phb/xUXwm/zsCqSr6Lb0RPL36C/UPBJKz2i8do/xADEaqSardV+WIHWIkz1bSdVo
JRPmW517m/SgzCI74jZ8mW7//DdQ0ciXUd+23DRGcDW+2TqC78vxZmfr7FdzUTZZ
RgVX9TC8XnCxUA4ZiMOpaLdzHDfjllAul3ns6sB/UNQ0ckVX61Wsb81WFoj0VLuN
F5wS4sS3NTYhZ5qqkI0eejkiNraSPtYWd0JTNYuKYPXhiJn3beW3XuWy3AdPannD
Pw0y1RXcwKd79ls+/OvFV/UvWRrSEzwh5qsNZE7gfo9JzHCNaOuHUvLcsrh50I1P
1hEnMYCpfCPCJBNd0d7JE0bNICTPE6W/7uypxp3POkfi2dqICEhjMUAJ77rhx3ub
X2/Y6NCkZqHfpV8aE1wHKCVHrb5Fj/KP3FZ9xt0eojcp196ZSvNhfO5wrJafRHaE
u56tHnYbtAeKwVp+6NUiv00RjIrsYLtwgAID8OSmL02+E1QlemnU61BrbEXFDUcM
LHjNvSSyG+h98+S5NF7RpXK8DjF6mI+T6u3pVNdjPpZwzXgxiZAsRs8Ss9rld5eD
-----END RSA PRIVATE KEY-----
[root@centos-04 tmp]# cat aaa.pub (公鑰)
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWPuKc4lKJzZ+Zunq2RTi7IUP2FkhzZ7fXfH0lUvzom9+3wsarU1cCC5UU1JibgmhuU68
/yTlj/ZAHa80Sgdyxn2/ZBJFMJcIKyHk5XyZJhhfdLvEB1QORHBYn0rcbPkqpCJcciZLNx1N0soSRVHgOIVimFa1eIAa8zDBjvEXTtl7coL
MlxzMIj+DOvoDTy7gq+VlPcpk9hnY+AA4TtxEUZ+btc+ZnaaFGxjPkOxiSRp4nOHwtagJrJ4YQYNhfoAuOqNzFQOfoTw2HnCuJF4uM0kjDY
0Anm9CjQkbWwH4zSinvLeU4fG4POLDqLwbZX7YLDPlGVFShRNZJtiPyoQNv root@centos-04 [root@centos-04 tmp]#     

由於我們在腳本中輸入密碼不合適,除了用expect我們還可以用ssh-agent命令把密碼放到內存里,現在我們在本機登錄本機,出現讓我們輸入密碼,這說明我們的秘鑰沒有生效

[root@centos-04 tmp]# ssh -i /tmp/aaa root@127.0.0.1
root@127.0.0.1's password:   

我們將公鑰寫到authorized_keys文件里

[root@centos-04 tmp]# cat aaa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWPuKc4lKJzZ+Zunq2RTi7IUP2FkhzZ7fXfH0lUvzom9+
3wsarU1cCC5UU1JibgmhuU68/yTlj/ZAHa80Sgdyxn2/ZBJFMJcIKyHk5XyZJhhfdLvEB1QORHBYn0rcbPk
qpCJcciZLNx1N0soSRVHgOIVimFa1eIAa8zDBjvEXTtl7coLMlxzMIj+DOvoDTy7gq+VlPcpk9hnY+AA4Tt
xEUZ+btc+ZnaaFGxjPkOxiSRp4nOHwtagJrJ4YQYNhfoAuOqNzFQOfoTw2HnCuJF4uM0kjDY0Anm9CjQkbW
wH4zSinvLeU4fG4POLDqLwbZX7YLDPlGVFShRNZJtiPyoQNv root@centos-04 [root@centos-04 tmp]# vim /root/.ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWPuKc4lKJzZ+Zunq2RTi7IUP2FkhzZ7fXfH0lUvzom9+3
wsarU1cCC5UU1JibgmhuU68/yTlj/ZAHa80Sgdyxn2/ZBJFMJcIKyHk5XyZJhhfdLvEB1QORHBYn0rcbPkqp
CJcciZLNx1N0soSRVHgOIVimFa1eIAa8zDBjvEXTtl7coLMlxzMIj+DOvoDTy7gq+VlPcpk9hnY+AA4TtxEU
Z+btc+ZnaaFGxjPkOxiSRp4nOHwtagJrJ4YQYNhfoAuOqNzFQOfoTw2HnCuJF4uM0kjDY0Anm9CjQkbWwH4z
SinvLeU4fG4POLDqLwbZX7YLDPlGVFShRNZJtiPyoQNv root@centos-04  

再次測試讓我們輸入公鑰的密碼,輸入之后即登錄成功

[root@centos-04 tmp]# ssh -i /tmp/aaa root@127.0.0.1 
Enter passphrase for key '/tmp/aaa': 
Last login: Wed Apr 17 18:22:40 2019 from 192.168.242.1
[root@centos-04 ~]# 

ssh-agent可以將私鑰放到內存里去,執行ssh-agent命令后再執行命令結果中的前兩行。

[root@centos-04 ~]# ssh-agent 
SSH_AUTH_SOCK=/tmp/ssh-ZhiIYiItxeaK/agent.8521; export SSH_AUTH_SOCK;
SSH_AGENT_PID=8522; export SSH_AGENT_PID;
echo Agent pid 8522;
[root@centos-04 ~]# SSH_AUTH_SOCK=/tmp/ssh-ZhiIYiItxeaK/agent.8521; export SSH_AUTH_SOCK;
[root@centos-04 ~]# SSH_AGENT_PID=8522; export SSH_AGENT_PID;
[root@centos-04 ~]#   

這樣就有了一個秘鑰代理,我們把私鑰加進去

[root@centos-04 ~]# ssh-add /tmp/aaa
Enter passphrase for /tmp/aaa: 
Identity added: /tmp/aaa (/tmp/aaa)
[root@centos-04 ~]#  

我們再次登錄發現不需要密碼直接登錄成功了

[root@centos-04 ~]# ssh -i /tmp/aaa root@127.0.0.1
Last login: Thu Apr 18 01:43:51 2019 from localhost
[root@centos-04 ~]# 
[root@centos-04 ~]# ps aux|grep ssh-agent
root       8522  0.0  0.0  72312  1228 ?        Ss   01:46   0:00 ssh-agent
root       8579  0.0  0.0 112724   984 pts/2    S+   01:53   0:00 grep --color=auto ssh-agent
[root@centos-04 ~]# killall ssh-agent
[root@centos-04 ~]# ps aux|grep ssh-agent
root       8582  0.0  0.0 112724   988 pts/2    R+   01:53   0:00 grep --color=auto ssh-agent
[root@centos-04 ~]# 

eval直接執行命令,用ssh-agent > 的方式再執行ssh-add 不能成功,因為這樣執行不在一個終端里ssh-add執行失敗。

[root@centos-04 ~]# eval echo 123
123
[root@centos-04 ~]# ssh-agent > /tmp/123.sh
[root@centos-04 ~]# sh /tmp/123.sh 
Agent pid 8588
[root@centos-04 ~]# 
[root@centos-04 ~]# ssh-add /tmp/aaa
Could not open a connection to your authentication agent.
[root@centos-04 ~]# 
[root@centos-04 ~]# killall ssh-agent
[root@centos-04 ~]# ps aux|grep ssh-agent
root       8615  0.0  0.0 112724   988 pts/2    R+   02:00   0:00 grep --color=auto ssh-agent
[root@centos-04 ~]# 

但是我們用eval可以

[root@centos-04 ~]# eval `ssh-agent`
Agent pid 8651
[root@centos-04 ~]# !ps
ps aux|grep ssh-agent
root       8651  0.0  0.0  72312   776 ?        Ss   02:02   0:00 ssh-agent
root       8653  0.0  0.0 112724   988 pts/2    R+   02:02   0:00 grep --color=auto ssh-agent
[root@centos-04 ~]# ssh-add /tmp/aaa
Enter passphrase for /tmp/aaa: 
Identity added: /tmp/aaa (/tmp/aaa)
[root@centos-04 ~]# 
[root@centos-04 ~]#  ssh -i /tmp/aaa root@127.0.0.1
Last login: Thu Apr 18 01:50:32 2019 from localhost
[root@centos-04 ~]# 
[root@centos-04 ~]# killall ssh-agent
[root@centos-04 ~]# eval `ssh-agent`; ssh-add /tmp/aaa
Agent pid 8686
Enter passphrase for /tmp/aaa: 
Identity added: /tmp/aaa (/tmp/aaa)
[root@centos-04 ~]#   

給命令起個別名

[root@centos-04 ~]# alias ak='eval `ssh-agent`; ssh-add /tmp/aaa'
[root@centos-04 ~]# !kill
killall ssh-agent
[root@centos-04 ~]# ak
Agent pid 8691
Enter passphrase for /tmp/aaa: 
Bad passphrase, try again for /tmp/aaa: 
Identity added: /tmp/aaa (/tmp/aaa)
[root@centos-04 ~]# 
[root@centos-04 tmp]# vim 94.sh
#!/bin/bash
#這個腳本用來批量查機器負載
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-17

for ip in `cat /tmp/ip.list`
do
    echo $ip 
    ssh $ip "uptime"
done

自動掛雲盤

[root@centos-04 tmp]# vim 95.sh
#!/bin/bash
#這個腳本用來自動掛載磁盤
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-17

if [ $# -ne 2 ]
then
    echo "Useage $0 盤符 掛載點, 如: $0 /dev/xvdb /data"
    exit 1
fi

if [ ! -b $1 ]
then
    echo "你提供的盤符不正確,請檢查后再操作"
    exit 1
fi

echo "格式化$1"
mkfs -t ext4 $1

if [ ! -d $2 ] ;then
        mkdir -p $2
fi

n=`awk '$NF == "$2"' /etc/fstab|wc -l`
if [ $n -eq 0 ]
then
    echo "$1              $2                      ext4   defaults  0  0" >> /etc/fstab
    mount -a
else
    echo "配置文件/etc/fstab中已經存在掛載點$2,請檢查一下."
    exit 1
fi

並發備份數據庫

 

[root@centos-04 tmp]# screen
[root@centos-04 tmp]# mkfifo 123.fifo
[root@centos-04 tmp]# echo "123" > 123.fifo
ctrl+a d
[root@centos-04 tmp]# cat 123.fifo 
123
[root@centos-04 tmp]# screen -r

[root@centos-04 ~]# mkfifo test10.fifo
[root@centos-04 ~]# exec 10<>test10.fifo
[root@centos-04 ~]# ls -l /dev/fd/10
lrwx------ 1 root root 64 4月  18 19:00 /dev/fd/10 -> /root/test10.fifo
[root@centos-04 ~]# read -u10 b

[root@centos-04 ~]# ls -l /dev/fd/(查看是否有fifo)
總用量 0
lrwx------ 1 root root 64 4月  18 19:09 0 -> /dev/pts/0
lrwx------ 1 root root 64 4月  18 19:09 1 -> /dev/pts/0
lrwx------ 1 root root 64 4月  18 19:09 10 -> /root/test10.fifo
lrwx------ 1 root root 64 4月  18 19:09 2 -> /dev/pts/0
lr-x------ 1 root root 64 4月  18 19:09 3 -> /proc/11917/fd
[root@centos-04 ~]#   

如果沒有,需要執行

[root@centos-04 ~]# exec 10<> test10.fifo
[root@centos-04 ~]# 
[root@centos-04 ~]# echo "3333" >&10
[root@centos-04 ~]#   
screen -r
[root@centos-04 ~]# echo $b 3333  

總結:第一步需要將10描述符和test10.fifo命名管道綁定到一起,然后用read命令向10要一個字符串賦值給a,如果有將直接輸出,如果沒有處於等待狀態,我們需要用echo “123” >&10(在終端)將123寫入,這樣a就有值了也就是123,在screen里echo $a就輸出123了。

[root@centos-04 tmp]# vim 96.sh
#!/bin/bash
#這個腳本用來並發備份數據庫
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-19

##假設100個庫的庫名、host、port以及配置文件路徑存到了一個文件里,文件名字為/tmp/databases.list
##格式:db1 10.10.10.2 3308 /data/mysql/db1/my.cnf
##備份數據庫使用xtrabackup(由於涉及到myisam,命令為inoobackupex)

exec &> /tmp/mysql_bak.log

if ! which innobackupex &>/dev/nll
then
    echo "安裝xtrabackup工具"
    rpm -ivh http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm  && \ 
    yum install -y percona-xtrabackup-24
    if [ $? -ne 0 ]
    then
        echo "安裝xtrabackup工具出錯,請檢查。"
        exit 1
    fi
fi

bakdir=/data/backup/mysql
bakuser=vyNctM
bakpass=99omeaBHh

function bak_data {
    db_name=$1
    db_host=$2
    db_port=$3
    cnf=$4
    [ -d $bakdir/$db_name ] || mkdir -p $bakdir/$db_name
    innobackupex --defaults-file=$4  --host=$2  --port=$3 --user=$bakuser --password=$bakpass  $bakdir/$1
        if [ $? -ne 0 ]
        then
            echo "備份數據庫$1出現問題。"
        fi
}

fifofile=/tmp/$$
mkfifo $fifofile
exec 1000<>$fifofile


thread=10
for ((i=0;i<$thread;i++))
do
    echo >&1000
done

cat /tmp/databases.list | while read line
do
    read -u1000
    {
        bak_data `echo $line`
        echo >&1000
    } &
done

wait
exec 1000>&-
rm -f $fifofile

打印三角形

[root@centos-04 tmp]# vim 97.sh
#!/bin/bash
#這個腳本用來打印三角形
#作者:SYJ
#日期:2019-04-18
#!/bin/bash
#這個腳本用來打印三角形
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-19

while true
do
    read -p "please input the lenth: " n
    if [ -z $n ]
    then
        echo "要輸入一個數字。"
        continue
    else
        n1=`echo $n|sed 's/[0-9]//g'`
        if [ -n "$n1" ]
        then
            echo "你輸入的不是純數字,重新輸入。"
            continue
        else
            break
        fi
    fi
done

for i in `seq 1 $n`
do
    j=$[$n-$i]
    for m in `seq $j`
    do
        echo -n " "
    done

    for p in `seq 1 $i`
    do
        echo -n "* "
    done
    echo
done
[root@centos-04 tmp]# sh 97.sh
please input the lenth: 9
        * 
       * * 
      * * * 
     * * * * 
    * * * * * 
   * * * * * * 
  * * * * * * * 
 * * * * * * * * 
* * * * * * * * * 
[root@centos-04 tmp]#

 截取字符串

[root@centos-04 tmp]# vim 98.sh
#!/bin/bash
#這個腳本用來截取字符串
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-19
var=http://www.aaa.com/root/123.htm
echo "1.取出www.aaa.com/root/123.htm"
echo $var |awk -F '//' '{print $2}'

echo "2.取出123.htm"
echo $var |awk -F '/' '{print $5}'

echo "3.取出http://www.aaa.com/root"
echo $var |sed 's#/123.htm##'

echo "4.取出http:"
echo $var |awk -F '//' '{print $1}'

echo "5.取出http://"
echo $var |awk -F 'www' '{print $1}'

echo "6.取出root/123.htm"
echo $var |awk -F 'com/' '{print $2}'
echo $var |awk -F '/' '{print $4"/"$5}'

echo "7.取出123"
echo $var |sed 's/[^0-9]//g'
[root@centos-04 tmp]# sh 98.sh
1.取出www.aaa.com/root/123.htm
www.aaa.com/root/123.htm
2.取出123.htm
123.htm
3.取出http://www.aaa.com/root
http://www.aaa.com/root
4.取出http:
http:
5.取出http://
http://
6.取出root/123.htm
root/123.htm
root/123.htm
7.取出123
123
[root@centos-04 tmp]# 

 修改文本格式  

[root@centos-04 tmp]# vim 99.sh
#!/bin/bash
#這個腳本用來格式化文本
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-19

n=`wc -l test3.txt|awk '{print $1}'`
n2=$[$n/2]

for i in `seq 1 $n2`
do
    i2=$[$i*2]
    j=$[$i2-1]
    l1=`sed -n "$i2"p test3.txt`
    l2=`sed -n "$j"p test3.txt`
    echo $l2:$l1
done

自定義rm  

[root@centos-04 tmp]# vim 100.sh
1. 簡單
#!/bin/bash
#這個腳本用來自定義rm
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-19

filename=$1
big_filesystem=/data/

if [ ! -e $1 ]
then
    echo "$1 不存在,請使用絕對路徑"
    exit
fi
d=`date +%Y%m%d%H%M`
read -p "Are U sure delete the file or directory $1? y|n: " c
case $c in
      y|Y)
          mkdir -p $big_filesystem/.$d && rsync -aR $1 $big_filesystem/.$d/$1 && /bin/rm -rf $1
          ;;
      n|N)
          exit 0
          ;;
      *)
          echo "Please input 'y' or 'n'."
          ;;
esac

2.復雜
#!/bin/bash
#這個腳本用來自定義rm
#作者:猿課-阿銘 www.apelearn.com
#日期:2018-12-19

#!/bin/bash
filename=$1

if [ ! -e $1 ]
then
    echo "$1 不存在,請使用絕對路徑"
    exit
fi
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
    case $c in
      y|Y)
          mkdir -p $big_filesystem/.$d && rsync -aR $1 $big_filesystem/.$d/$1 && /bin/rm -rf $1
          ;;
      n|N)
          exit 0
          ;;
      *)
          echo "Please input 'y' or 'n'."
          ;;
     esac
else
    echo "The disk size is not enough to backup the files $1."
    read -p "Do you want to delete $1? y|n: " c
    case $c in
      y|Y)
        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
        ;;
     n|N)
        echo "It will not delete $1."
        exit 0
        ;;
      *)
        echo "Please input 'y' or 'n'."
        ;;
    esac
fi

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM