天天看點

Linux歸檔壓縮、腳本程式設計之循環控制

一、Linux歸檔壓縮

在一些備份伺服器上為了節省硬碟空間以存儲更多的内容,壓縮是一個不錯的選擇,雖然壓縮是拿cpu的時間去換硬碟空間有時并不合算。Linux的壓縮工具一般隻能壓縮單個文本檔案,如果要壓縮一個目錄,就必須先将目錄打包歸檔。Linux有很多不同的壓縮的工具,一般壓縮比越大可能占用cpu的時間更長,下面來瞧一瞧Linux有哪些常用的工具吧。

1、compress、uncompress和zcat:壓縮,展開資料,壓縮檔案字尾是.Z,此壓縮工具已過時。

compress壓縮預設會删除原檔案

文法:compress [-dfvcVr] [-b maxbits] [files......]

選項:

-d:解壓縮相當于uncompress

-c:将壓縮後的檔案輸出到标準輸出,不會删除原檔案,可通過重定向來實作壓縮并保留原檔案

-b:設定共同字串數的上限,以位元計算,可以設定的值為 9 至 16 bits 。由于值越大,能使用的共同字串就 越多,壓縮比例就越大,是以一般使用預設值 16 bits (bits)

[root@localhost blog1]# ll -h
total 203M
-rw-r--r--. 1 root root 102M Aug 16 21:02 locale-archive
-rw-r--r--. 1 root root 102M Aug 16 21:04 locale-archive.bak
[root@localhost blog1]# compress locale-archive      
[root@localhost blog1]# ll -h
total 148M
-rw-r--r--. 1 root root 102M Aug 16 21:04 locale-archive.bak
-rw-r--r--. 1 root root  47M Aug 16 21:02 locale-archive.Z      
[root@localhost blog1]# compress -b 9 -c locale-archive.bak > locale-archivebak.Z
[root@localhost blog1]# ll -h
total 213M
-rw-r--r--. 1 root root 102M Aug 16 21:04 locale-archive.bak
-rw-r--r--. 1 root root  66M Aug 16 21:08 locale-archivebak.Z
-rw-r--r--. 1 root root  47M Aug 16 21:02 locale-archive.Z      

-f:如果壓縮後的檔案名在指定的目錄已存在,預設将提示使用者是否覆寫,-f選項将無提示覆寫已存在檔案。注:如果要壓縮的檔案有多個硬連接配接,compresse預設無法壓縮,-f選項可強制壓縮,壓縮後硬連接配接數減一。

-v:列印壓縮資訊,将會列印壓縮掉的大小占原檔案大小的百分比

-V:列印版本資訊和編譯選項

-r:遞歸壓縮,如果目标是一個目錄,-r選項将進入到目錄裡将裡面的檔案壓縮,如果有子目錄,則進入子目錄壓縮

zcat在解壓之前檢視壓縮包裡的檔案,zcat通過重定向可實作解壓縮

[root@linux blog1]# zcat local.Z > local
[root@linux blog1]# ll -h
total 249M
-rw-r--r--. 1 root root 102M Aug 17 18:04 local
-rw-r--r--. 1 root root 102M Aug 17 18:03 locale-archive
-rw-r--r--. 1 root root  47M Aug 17 18:03 local.Z      

2、gzip/gunzip:比較流行的一個壓縮工具,壓縮預設會删除原檔案,字尾為:.gz

用法:gzip [option]... [file]...

-c:保留原檔案,将壓縮後的文本輸出到标準輸出,可通過重定向生成壓縮檔案

-d:解壓縮

-f:強制覆寫同名檔案,強制壓縮硬連接配接檔案

-l:列出壓縮檔案的内容

[root@linux blog1]# gzip -l local.gz 
         compressed        uncompressed  ratio uncompressed_name
           23874038           106065056  77.5% local      

-L:顯示軟體許可

-n:壓縮時不儲存原始名和時間戳,解壓時也不保留,解壓時為預設選項

-N:壓縮時保留原始名和時間戳,解壓時也保留,壓縮時為預設選項

[root@linux opt]# ll
total 103580
-rw-r--r--. 1 root root 106065067 Aug 17 10:07 local
[root@linux opt]# gzip -cn local > loc.gz
[root@linux opt]# gzip -cN local > loca.gz
[root@linux opt]# ll
total 150212
-rw-r--r--. 1 root root  23874048 Aug 17 10:10 loca.gz
-rw-r--r--. 1 root root 106065067 Aug 17 10:07 local
-rw-r--r--. 1 root root  23874042 Aug 17 10:09 loc.gz
[root@linux opt]# gunzip -n loc.gz
[root@linux opt]# gunzip -N loca.gz
gzip: local already exists; do you wish to overwrite (y or n)? y      
[root@linux opt]# ll
total 207160
-rw-r--r--. 1 root root 106065067 Aug 17 10:09 loc  #########
-rw-r--r--. 1 root root 106065067 Aug 17 10:07 local#########時間戳都保留下來了      
[root@linux opt]# date
Wed Aug 17 10:24:38 CST 2016
[root@linux opt]# gzip -c local > loc.gz
[root@linux opt]# gzip -c local > local.gz
[root@linux opt]# ll
total 253792
-rw-r--r--. 1 root root 106065067 Aug 17 10:09 loc
-rw-r--r--. 1 root root 106065067 Aug 17 10:07 local
-rw-r--r--. 1 root root  23874048 Aug 17 10:25 local.gz
-rw-r--r--. 1 root root  23874048 Aug 17 10:25 loc.gz
[root@linux opt]# mv local.gz /mnt/
[root@linux opt]# mv loc.gz /mnt/
[root@linux opt]# cd /mnt/
[root@linux mnt]# ll
total 46632
-rw-r--r--. 1 root root 23874048 Aug 17 10:25 local.gz
-rw-r--r--. 1 root root 23874048 Aug 17 10:25 loc.gz
[root@linux mnt]# gunzip -N local.gz 
[root@linux mnt]# gunzip -n loc.gz
[root@linux mnt]# ll
total 207160
-rw-r--r--. 1 root root 106065067 Aug 17 10:25 loc   ########保留了上一次壓縮檔案的時間戳
-rw-r--r--. 1 root root 106065067 Aug 17 10:07 local########保留最原始的時間戳      

-q:忽略所有的警告資訊

-r:遞歸壓縮

-S:更改壓縮字尾字元串

-t:測試壓縮

-v:冗長的資訊

-1到-9:指定壓縮比,1的壓縮比最小,壓縮耗費時間最少;9壓縮比最大,最耗費時間;預設為6

3、bzip2/bunzip2/bzcat:塊類檔案的壓縮工具,對于大檔案有比gzip更好的壓縮效果,但對于小檔案二者壓縮效果差别不大bzip2将耗費更多的時間。壓縮字尾為.bz2

常用選項:

-d:強制解壓縮

-z:強制解壓縮

-k:壓縮不删除原檔案,預設會删除原檔案

-f:覆寫已存在的同名檔案

-t:壓縮測試

-c:将壓縮檔案送到标準輸出

-q:屏蔽非緊急的錯誤資訊

-v/-vv:列印冗長的資訊

-s:使用最少的記憶體(最少是2500K)

-1..-9:設定塊大小100K..900K,壓縮時的區塊大小

http://www.jb51.net/LINUXjishu/424315.html

4、xz/unxz/xzcat,和xz類似的壓縮工具lzma/unlzma/lzcat,壓縮字尾為.xz

-z:強制壓縮

-l:列出.xz壓縮檔案的資訊

-k:保留原檔案,預設删除

-f:強制覆寫

-c:壓縮檔案送到标準輸出

-e:通過使用更多的CPU時間來進一步提高壓縮比

-T:多線程壓縮,使用給定的數字設定能使用的最多線程,預設為1,設為0時使用核心數

[root@linux blog1]# xz -e -T8 -k local      
[root@linux blog1]# ll -h
total 296M
-rw-r--r--. 1 root root 102M Aug 17  2016 local
-rw-r--r--. 1 root root  21M Aug 17  2016 local.bz2
-rw-r--r--. 1 root root 102M Aug 17  2016 locale-archive
-rw-r--r--. 1 root root  23M Aug 17 11:06 local.gz
-rw-r--r--. 1 root root 3.7M Aug 17  2016 local.xz ####喪心病狂的壓縮比和等待時間
-rw-r--r--. 1 root root  47M Aug 17  2016 local.Z      

-H:顯示詳細的幫助,包括進階選項

當沒有指定檔案時或使用-,從标準輸入讀取

以上工具都不支援打包歸檔操作,當要壓縮整個目錄時,需要先将目錄打包,常用的工具有tar、cpio和zip。

5、先來看看比較簡單的zip/unzip,zip支援多個平台(Linux、Windows等),但是壓縮的效率不好

usage:zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]

基本指令:zip options archive_name file1 file2 ......

-r:壓縮目錄

6、tar:堪稱Linux的打包壓縮神器,日常工作用的最多,預設保留原檔案。tar指令的用法有許多,其info幫助文檔非常冗長詳細,這裡将介紹一些常用的用法。

usage:tar [option]... [file]...

-f:通用選項,指定file.tar的路徑

  • 建立歸檔

-c -f:建立歸檔,tar建立歸檔時會自動删掉目前的根目錄号,以防解壓時覆寫了原來的目錄

注意:cf順序不能颠倒。

  • 展開歸檔

-x -f:展開歸檔,預設展開到目前目錄

-C;指定展開的路徑

  • 檢視歸檔中的檔案清單

-t -f:檢視歸檔中的檔案

  • 歸檔後壓縮

-z:gzip,以.gz格式壓縮歸檔

    -zcf 歸檔壓縮後的檔案名 file或dir...

    -zxf 展開壓縮歸檔,展開時tar可自己識别壓縮格式,即任何壓縮都可用xf解壓。

-j:bzip2

    -jcf

    -jxf

-J:xz

    -Jcf

    -Jxf

7、cpio :通過重定向的方式将檔案進行打包備份,還原恢複的工具,它可以解壓以“.cpio”或者“.tar”結尾的檔案。

usage:

cpio [選項] > 檔案名或者裝置名

cpio [選項] < 檔案名或者裝置名

-o:将檔案拷貝打包成檔案或者将檔案輸出到裝置上

-i:解包,将打封包件解壓或裝置上的備份還原到系統

-t:預覽,檢視檔案内容或者輸出到裝置上的檔案内容

-v;顯示打包過程中的檔案名稱

-d:解壓生成目錄,cpio時,自動建立目錄

-c:一種較新的存儲方式

[root@linux opt]# find /etc/ -user root |cpio -ovc > hh.cpio      
[root@linux opt]# ll -h
total 244M
-rw-r--r--. 1 root root  19M Aug 17 14:47 hh.cpio      
[root@linux ~]# cpio -vt < /opt/hh.cpio      
drwxr-x---   2 root     root            0 Nov 21  2015 /etc/sudoers.d
-rw-r--r--   1 root     root          112 Mar  6  2015 /etc/e2fsck.conf
-rw-r--r--   1 root     root          936 Mar  6  2015 /etc/mke2fs.conf
-rw-r--r--   1 root     root           37 Aug 17  2016 /etc/vconsole.conf
-rw-r--r--   1 root     root           19 Aug 17  2016 /etc/locale.conf
-rw-r--r--   1 root     root            6 Aug 17  2016 /etc/hostname
-rw-r--r--   1 root     root          163 Aug 17  2016 /etc/.updated
-rw-r--r--   1 root     root        12288 Aug 17  2016 /etc/aliases.db      

注意:cpio打包壓縮時不會删掉根目錄,還原時一定要注意

[root@linux ~]# cpio -iv < /opt/hh.cpio
[root@linux ~]# cpio -idv < /opt/hh.cpio      
cpio: /etc/mke2fs.conf not created: newer or same age version exists
/etc/mke2fs.conf
cpio: /etc/vconsole.conf not created: newer or same age version exists
/etc/vconsole.conf
cpio: /etc/locale.conf not created: newer or same age version exists
/etc/locale.conf
cpio: /etc/hostname not created: newer or same age version exists
/etc/hostname
cpio: /etc/.updated not created: newer or same age version exists
/etc/.updated
cpio: /etc/aliases.db not created: newer or same age version exists      

二、腳本程式設計之循環控制

1、for循環

for 變量名 in 清單;do

    循環體

done

依次将清單中的元素指派給“變量名”; 每次指派後即執行一次循環體; 直到清單中的元素耗盡,循環結束

循環清單的生成方法:

  1. 直接給出清單,注意以空格分割
  2. 整數清單

    {start .. end}、$(seq [start [step]] end)

  3. 傳回清單的指令

    $(command)

  4. 使用glob,如:*.sh
  5. 變量引用:$*、$@

for的循環清單可以用(()),括号裡可以實作c語言風格的循環

2、while循環

while condition;do

    循環體

CONDITION:循環控制條件;進入循環之前,先做一次判斷;每一次循環之後會再次做判斷;條件為“true”,則執行一次循環;直到條件測試狀态為“false”終止循環,是以:CONDTION一般應該有循環控制變量;而此變量的值會在循環體不斷地被修正

特殊用法:

while read line;do

done < /PATHOFFILE

依次讀取/PATH/FROM/SOMEFILE檔案中的每一行,且将行指派給變量line

3、until循環

until condition ;do

同while循環相反,進入條件:CONDITION 為false,退出條件:CONDITION 為true。

4、循環控制語句continue、break和exit

continue:

continue [N]:提前結束第N層的本輪循環,而直接進入下一次循環,最内層為第一層

[root@linux blog1]# bash continue.sh 
最外層: 1
第一層: 1
第一層: 2
最外層: 2
第一層: 1
第一層: 2
最外層: 3
第一層: 1
第一層: 2
最外層: 4
第一層: 1
第一層: 2
最外層: 5
第一層: 1
第一層: 2
[root@linux blog1]# cat continue.sh
#!/bin/bash
for i in {1..5};do
    echo "最外層: $i"
        for j in {1..5};do
            if [ $j -eq 3 ];then
                continue 2
            fi
            echo "第一層: $j"
        done
done      

break:

break[N]:提前結束第N層循環,最内層為第一層

[root@linux blog1]# bash break.sh
The last layer: 1
The second layer: 1
The first layer: 1
The first layer: 2
The last layer: 2
The second layer: 1
The first layer: 1
The first layer: 2
The last layer: 3
The second layer: 1
The first layer: 1
The first layer: 2
The last layer: 4
The second layer: 1
The first layer: 1
The first layer: 2
The last layer: 5
The second layer: 1
The first layer: 1
The first layer: 2
[root@linux blog1]# cat break.sh
#!/bin/bash
for i in {1..5};do
    echo "The last layer: $i"
    for j in {1..5};do
        echo "The second layer: $j"
        for k in {1..5};do
            if [ $k -eq 3 ];then
                break 2
            fi
            echo "The first layer: $k"
        done
    done
done      

exit:

exit:退出整個程序,隻退出目前程序

[root@linux blog1]# bash exit.sh
The last layer: 1
The second layer: 1
The first layer: 1
The first layer: 2
The second layer: 2
The first layer: 1
The first layer: 2
The second layer: 3
The first layer: 1
The first layer: 2
The second layer: 4
The first layer: 1
The first layer: 2
The second layer: 5
The first layer: 1
The first layer: 2
The last layer: 2
The second layer: 1
The first layer: 1
The first layer: 2
The second layer: 2
The first layer: 1
The first layer: 2
The second layer: 3
The first layer: 1
The first layer: 2
The second layer: 4
The first layer: 1
The first layer: 2
The second layer: 5
The first layer: 1
The first layer: 2
The last layer: 3
The second layer: 1
The first layer: 1
The first layer: 2
The second layer: 2
The first layer: 1
The first layer: 2
The second layer: 3
The first layer: 1
The first layer: 2
The second layer: 4
The first layer: 1
The first layer: 2
The second layer: 5
The first layer: 1
The first layer: 2
The last layer: 4
The second layer: 1
The first layer: 1
The first layer: 2
The second layer: 2
The first layer: 1
The first layer: 2
The second layer: 3
The first layer: 1
The first layer: 2
The second layer: 4
The first layer: 1
The first layer: 2
The second layer: 5
The first layer: 1
The first layer: 2
The last layer: 5
The second layer: 1
The first layer: 1
The first layer: 2
The second layer: 2
The first layer: 1
The first layer: 2
The second layer: 3
The first layer: 1
The first layer: 2
The second layer: 4
The first layer: 1
The first layer: 2
The second layer: 5
The first layer: 1
The first layer: 2
[root@linux blog1]# cat exit.sh
#!/bin/bash
for i in {1..5};do
    echo "The last layer: $i"
    (for j in {1..5};do
        echo "The second layer: $j"
        (for k in {1..5};do
            if [ $k -eq 3 ];then
                exit 2
            fi
            echo "The first layer: $k"
        done)
    done)
done      

和break 1的效果一樣

5、select循環,主要用于建立菜單,按數字順序排列的菜單項将顯示在标準錯誤上,并顯示PS3 提示符,等待使用者輸入

使用者輸入菜單清單中的某個數字,執行相應的指令

使用者輸入被儲存在内置變量REPLY 中

select var in list;do

    循環體指令

select 是個無限循環,是以要記住用break 指令退出循環,或用exit 指令終止腳本。也可以按ctrl+c 退出循環。

select 經常和case 聯合使用

與for 循環類似,可以省略in list ,此時使用位置參量

練習:

用until實作下列的問題

1、每隔3秒鐘到系統上擷取已經登入的使用者的資訊;如果發現使用者hacker登入,則将登入時間和主機記錄于日志/var/log/login.log中,并提示該使用者退出系統。

[root@linux blog1]# bash userlogin.sh
hacker doesnot login
hacker doesnot login
......      
###使用hacker登陸後
Message from root@linux on <no tty> at 17:16 ...
You are forbidden to login,please logout at once!
EOF
Message from root@linux on <no tty> at 17:17 ...
You are forbidden to login,please logout at once!
EOF      
hacker doesnot login
Message to hacker is sent
hacker doesnot login
Message to hacker is sent      
[root@linux blog1]# cat userlogin.sh
#!/bin/bsh
until false;do
    if   w -h|grep hacker &>/dev/null 
    then
        w -h|grep hacker|tr -s ' ' |cut -d ' ' -f3,4 >> /var/log/login.log
        echo "You are forbidden to login,please logout at once!"|write hacker &> /dev/null
        echo "Message to hacker is sent"
    fi
    echo "hacker doesnot login"
    sleep 3
done      

2、随機生成10以内的數字,實作猜字遊戲,提示比較大或小,相等則退出

[root@linux blog1]# bash guessnum.sh
please enter your num[0-10]: 9
you are wrong,guess again!
please enter your num[0-10]: 2
you are wrong,guess again!
please enter your num[0-10]: 3
you are wrong,guess again!
please enter your num[0-10]: 4
you are wrong,guess again!
please enter your num[0-10]: 5
you are wrong,guess again!
please enter your num[0-10]: 6
You are very luchy!      
[root@linux blog1]# cat guessnum.sh
#!/bin/bash
until false;do
    rannum=$[$RANDOM%11]
    read -p "please enter your num[0-10]: " num
    if [ $rannum -eq $num ];then
        echo "You are very luchy!"
        break
    else
        echo "you are wrong,guess again!"
    fi
done      

3、編寫腳本,求100以内所有正整數之和

[root@linux blog1]# bash sum100.sh
Please enter the bigger num: 100
Please enter the smaller num: 1
The total num is 5050
[root@linux blog1]# bash sum100.sh
Please enter the bigger num: 1000
Please enter the smaller num: 500
The total num is 375750
[root@linux blog1]# cat sum100.sh
#!/bin/bash
read -p "Please enter the bigger num: " bigger
read -p "Please enter the smaller num: " smaller
sum=0
until [ $bigger -lt $smaller ];do
    let sum+=$smaller
    let smaller++
done
echo "The total num is $sum"      

4、編寫腳本,通過ping指令探測172.16.250.1-254範圍内的所有主機的線上狀态,統計線上主機和離線主機各多少。

[root@linux blog1]# bash ping.sh
Please enter the ip(like 1.1.1.1): 10.1.253.0
10.1.253.0 is down
10.1.253.1 is down
10.1.253.2 is down
10.1.253.3 is down
10.1.253.4 is down
10.1.253.5 is down
10.1.253.6 is down
10.1.253.7 is down
10.1.253.8 is up
10.1.253.9 is down
.........      
10.1.253.253 is down
10.1.253.254 is down
10.1.253.255 is down
The total ip which is up:14
The total ip which is down:242
[root@linux blog1]#      
[root@linux blog1]# cat ping.sh
#!/bin/bash
read -p "Please enter the ip(like 1.1.1.1): " ipall
ip_addr=`echo $ipall |cut -d. -f1-3`
last=0
pingS=0
pingF=0
until [ $last -gt 255 ];do
    if ping -c1 -w1 ${ip_addr}.$last &>/dev/null
    then
        echo "${ip_addr}.$last is up"
        let pingS++
    else
        echo "${ip_addr}.$last is down"
        let pingF++
    fi
    let last++
    sleep 0.1
done
echo "The total ip which is up:$pingS"
echo "The total ip which is down:$pingF"      

5、編寫腳本,列印九九乘法表

[root@linux blog1]# bash multitable.sh
1x1=1    
2x1=2    2x2=4    
3x1=3    3x2=6    3x3=9    
4x1=4    4x2=8    4x3=12    4x4=16    
5x1=5    5x2=10    5x3=15    5x4=20    5x5=25    
6x1=6    6x2=12    6x3=18    6x4=24    6x5=30    6x6=36    
7x1=7    7x2=14    7x3=21    7x4=28    7x5=35    7x6=42    7x7=49    
8x1=8    8x2=16    8x3=24    8x4=32    8x5=40    8x6=48    8x7=56    8x8=64    
9x1=9    9x2=18    9x3=27    9x4=36    9x5=45    9x6=54    9x7=63    9x8=72    9x9=81      
[root@linux blog1]# cat multitable.sh
#!/bin/bash
line=1
until [ $line -gt 9 ];do
    row=1
    until [ $row -gt $line ];do
        echo -ne "${line}x${row}=$[line*row]\t"
        let row++
    done
    echo
    let line++
done      

6、編寫腳本,利用變量RANDOM生成10個随機數字,輸出這個10數字,并顯示其中的最大者和最小者

[root@linux blog1]# cat random.sh
#!/bin/bash
max=$RANDOM
min=$RANDOM
echo "The random is: $max"
echo "The random is: $min"
if [ $max -lt $min ];then
    mid=$max
    max=$min
    min=$mid
fi
i=1
until [ $i -gt 8 ];do
    mid=$RANDOM
    echo "The random is: $mid"
    if [ $mid -gt $max ];then
        c=$mid
        mid=$max
        max=$c
    fi
    if [ $mid -lt $min ];then
        c=$min
        min=$mid
        mid=$c
    fi
    let i++
done
echo -e "\033[31m-------------------------\033[0m"
echo "the max num is: $max"
echo "the min num is: $min"
[root@linux blog1]# bash random.sh
The random is: 25641
The random is: 18437
The random is: 20789
The random is: 1052
The random is: 1937
The random is: 28962
The random is: 28954
The random is: 2717
The random is: 29918
The random is: 10578
-------------------------
the max num is: 29918
the min num is: 1052      
[root@linux blog1]# bash random2.sh
the random is 4013
the random is 13009
the random is 24430
the random is 18854
the random is 20264
the random is 20586
the random is 31832
the random is 6138
the random is 11674
the random is 31771
max: 31832
min: 4013
[root@linux blog1]# cat random2.sh
#!/bin/bash
mid=$RANDOM
max=$mid
min=$mid
echo "the random is $mid"
i=1
until [ $i -gt 9 ];do
    mid=$RANDOM
    echo "the random is $mid"
    if [ $mid -gt $max ];then
        max=$mid
    fi
    if [ $mid -lt $min ];then
        min=$mid
    fi
    let i++
done
echo "max: $max"
echo "min: $min"      

7、編寫腳本,實作列印國際象棋棋盤

[root@linux blog1]# cat chess.sh
#!/bin/bash
read -p "Please enter the num:" num
line=1
until [ $line -gt $num ];do
    row=1
    until [ $row -gt $num ];do
        let sum=line+row
        let sum=$[$sum%2]
        if [ $sum -eq 0 ];then
            echo -en "\033[47m  \033[0m"
        else
            echo -en "\033[43m  \033[0m"
        fi
        let row++
    done
    echo
    let line++
done      
Linux歸檔壓縮、腳本程式設計之循環控制

8、列印等腰三角形

[root@linux blog1]# cat trigon.sh
#!/bin/bash
read -p "please enter a odd: " line
if [ $(($line%2)) -eq 0 ];then
    echo "Must a odd"
fi
total=$[2*$line-1]
i=1
until [ $i -gt $total ];do
    if [ $i -le $line ];then
        j=1
        until [ $j -gt $(($line-$i)) ];do
            echo -en " "
            let j++
        done
        k=1
        until [ $k -gt $((2*$i-1)) ];do
            echo -en "*"
            let k++
        done
    else
        l=1
        until [ $l -gt $(($i-$line)) ];do
            echo -en " "
            let l++
        done
        m=1
        until [ $m -gt $(($total+2*$line-2*$i)) ];do
            echo -en "*"
            let m++
        done
    fi
    echo
    let i++
done      
Linux歸檔壓縮、腳本程式設計之循環控制

9、安裝centos6.7,用centos6.8kernel更新(由于我隻有centos7.2和centos6.8的鏡像,實驗将給centos7.2安裝centos6.8的核心)

[root@linux ~]# rpm -q kernel
kernel-3.10.0-327.el7.x86_64
[root@linux ~]# rpm -ivh kernel-2.6.32-642.el6.x86_64.rpm --oldpackage
warning: kernel-2.6.32-642.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:kernel-2.6.32-642.el6            ################################# [100%]
depmod: WARNING: could not open /lib/modules/2.6.32-642.el6.x86_64/modules.builtin: No such file or directory
depmod: WARNING: could not open /var/tmp/initramfs.UZubZb/lib/modules/2.6.32-642.el6.x86_64/modules.builtin: No such file or directory
[root@linux ~]# rpm -q kernel
kernel-3.10.0-327.el7.x86_64
kernel-2.6.32-642.el6.x86_64      
[root@linux ~]# ls /boot/
config-2.6.32-642.el6.x86_64
config-3.10.0-327.el7.x86_64
grub
grub2
initramfs-0-rescue-6f7a172ba61f472db6b9ac28b9c75e61.img
initramfs-2.6.32-642.el6.x86_64.img
initramfs-3.10.0-327.el7.x86_64.img
initramfs-3.10.0-327.el7.x86_64kdump.img
initrd-plymouth.img
symvers-2.6.32-642.el6.x86_64.gz
symvers-3.10.0-327.el7.x86_64.gz
System.map-2.6.32-642.el6.x86_64
System.map-3.10.0-327.el7.x86_64
vmlinuz-0-rescue-6f7a172ba61f472db6b9ac28b9c75e61
vmlinuz-2.6.32-642.el6.x86_64
vmlinuz-3.10.0-327.el7.x86_64      

繼續閱讀