1、寫一個腳本,完成如下功能
(1) 傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在;
(2) 如果存在,則顯示此設備上的所有分區信息;
[root@moban ~]# vi week5-01.sh
[root@wmoban ~]# cat week5-01.sh
#!/bin/bash
#author:Wangyafei
#
read -p "Please enter a disk device path:" path
while [ -z"$path" ];do
echo "Pleaseenter a correctdisk device path."
read -p "Pleaseenter a diskdevice path:" path
done
if [ -b"$path" ];then
fdisk -l $path
else
echo "This isnot a disk devicepath."
fi
[root@wangyafei ~]# bash week5-01.sh
Please enter a disk device path:/dev/sda
磁盤 /dev/sda:128.8 GB,128849018880 字節,251658240個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512字節 /512 字節
I/O 大小(最小/最佳):512字節 /512 字節
磁盤標籤類型:dos
磁盤標識符:0x000aaf3f
設備Boot Start End Blocks Id System
/dev/sda1 * 2048 4196351 2097152 83 Linux
/dev/sda2 4196352 96479231 46141440 8e Linux LVM
[root@moban ~]# bash week5-01.sh
Please enter a disk device path:/dev/sdb
磁盤 /dev/sdb:53.7 GB,53687091200 字節,104857600個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512字節 /512 字節
I/O 大小(最小/最佳):512字節 /512 字節
磁盤標籤類型:dos
磁盤標識符:0x9a210386
設備Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 8e Linux LVM
/dev/sdb2 20973568 41945087 10485760 8e Linux LVM
[root@moban ~]# bash week5-01.sh
Please enter a disk device path:/dev/sdc
This is not a disk device path.
[root@moban ~]#
2、寫一個腳本,完成如下功能
傳遞一個參數給腳本,此參數爲gzip、bzip2或者xz三者之一;
(1) 如果參數1的值爲gzip,則使用tar和gzip歸檔壓縮/etc目錄至/backups目錄中,並命名爲/backups/etc-20160613.tar.gz;
(2) 如果參數1的值爲bzip2,則使用tar和bzip2歸檔壓縮/etc目錄至/backups目錄中,並命名爲/backups/etc-20160613.tar.bz2;
(3) 如果參數1的值爲xz,則使用tar和xz歸檔壓縮/etc目錄至/backups目錄中,並命名爲/backups/etc-20160613.tar.xz;
(4) 其它任意值,則顯示錯誤壓縮工具,並執行非正常退出;
[root@moban ~]# vi week5-02.sh
[root@ moban ~]# cat week5-02.sh
#!/bin/bash
#author:wangyafei
#
[ -d /backups ] || mkdir /backups
read -p "pelase input a argu(gzip/bzip2/xz):" argu
case $argu in
gzip)
tar -Pzcf /backups/etc-`date +%Y%m%d`.tar.gz /etc
;;
bzip2)
tar -Pjcf /backups/etc-`date +%Y%m%d`.tar.bz2 /etc
;;
xz)
tar -PJcf /backups/etc-`date +%Y%m%d`.tar.xz /etc
;;
*)
echo "error compression tools"
;;
esac
[root@ moban ~]# bash week5-02.sh
pelase input a argu(gzip/bzip2/xz):gzip
[root@ moban ~]# ls /backups
etc-20170318.tar.gz
[root@ moban ~]# bash week5-02.sh
pelase input a argu(gzip/bzip2/xz):bzip2
[root@ moban ~]# ls /backups
etc-20170318.tar.bz2 etc-20170318.tar.gz
[root@ moban ~]# bash week5-02.sh
pelase input a argu(gzip/bzip2/xz):xz
[root@ moban ~]# ls /backups
etc-20170318.tar.bz2 etc-20170318.tar.gz etc-20170318.tar.xz
[root@ moban ~]# bash week5-02.sh
pelase input a argu(gzip/bzip2/xz):wangyafei
error compression tools
3、寫一個腳本,接受一個路徑參數:
(1) 如果爲普通文件,則說明其可被正常訪問;
(2) 如果是目錄文件,則說明可對其使用cd命令;
(3) 如果爲符號鏈接文件,則說明是個訪問路徑;
(4) 其它爲無法判斷;
[root@moban ~]# vi week5-03.sh
[root@ moban ~]# cat week5-03.sh
#!/bin/bash
#author:wangyafei
#
if [ $# -ne 1 ];then
echo "invalidargument, one argument"
exit 1
else
if [ -f $1 ];then
echo "generalfile,you can use ls ...."
elif [ -d $1 ];then
echo "folder,youcan use cd ....."
elif [ -L $1 ];then
echo "symbolic linkfile.........."
else
echo "sorry, i cannot judge..."
fi
fi
[root@ moban ~]# bash week5-03.sh
invalid argument, one argument
[root@ moban ~]# bash week5-03.sh /dev
folder,you can use cd .....
[root@ moban ~]# bash week5-03.sh/backups/etc-20170318.tar.bz2
general file,you can use ls ....
[root@ moban ~]# bash week5-03.sh /backups/?
sorry, i can not judge...
[root@moban ~]#
4、寫一個腳本,取得當前主機的主機名,判斷
(1) 如果主機名爲空或爲localhost,或爲"(none)",則將其命名爲mail.magedu.com;
(2) 否則,顯示現有的主機名即可;
[root@moban ~]# vi week5-04.sh
[root@moban ~]# cat week5-04.sh
#!/bin/bash
#author:wangyafei
#
while read line;do
hostname=$(echo $line | cut -d .-f1)
if [ $hostname=="localhost" -o $hostname =="(none)" ];then
echo"HOSTNAME=mail.magedu.com" > /etc/sysconfig/network
else
echo"The current hostname is:$line"
fi
done < /proc/sys/kernel/hostname
[root@moban ~]# bash week5-04.sh
The current host name is:wangyafei
5、寫一個腳本,完成如下任務:
(1) 按順序分別複製/var/log目錄下的每個直接文件或子目錄至/tmp/test1-testn目錄中;
(2) 複製目錄時,才使用cp -r命令;
(3) 複製文件時使用cp命令;
(4) 複製鏈接文件時使用cp -d命令;
(5) 餘下的所有類型,使用cp -a命令;
[root@moban~]# vi week5-05.sh
[root@moban~]# cat week5-05.sh
#!/bin/bash
#author:wangyafei
#
src=/var/log
dsrc=/tmp/test1-testn
[ -d $dsrc ] || mkdir $dsrc
echo "ready to copy file wait a moment please."
for i in ${src}/* ;do
if [ -d $i ];then
cp -r $i ${dsrc}/
elif [ -f $i ];then
cp -r $i ${dsrc}/
elif [ -l $i ];then
cp -d $i ${dsrc}/
else
cp -a $i ${dsrc}/
fi
done
echo -e "Operation completed!"
[root@moban ~]# bash week5-05.sh
ready to copy file wait a moment please.
Operation completed!
6、請詳細描述CentOS系統的啓動流程(詳細到每個過程系統做了哪些事情)
啓動過程 (PC架構) POST--> Boot Sequence(BIOS) --> Boot Loader (MBR)-->GRUB—> Kernel(ramdisk) --> rootfs --> switchroot -->/sbin/init-->(/etc/inittab, /etc/init/*.conf) --> 設定默認運行級別> 系統初始化腳本 --> 關閉或啓動對應級別下的服務 --> 啓動終端
第一步:加電自檢(POST)
系統加電之後,首先進行的硬件自檢,一但通電後主板會自動讀取ROM(只讀)中的程序,進行加載,計算機會首先加載BIOS信息,BIOS信息是如此的重要,以至於計算機必須在最開始就找到它。檢查各種硬件設備是否完整存在,如內存,硬盤,顯示,IO設備等。如果有硬件故障的話將按兩種情況理:對於嚴重故障(致命性故障)則停機,此時由於各種初始化操作還沒完成,不能給出任何提示或信號;對於非嚴重故障則給出提示或聲音報警信號,等待用戶處理),如果沒有故障,POST完成自己 的接力任務,將尾部工作交接給BIOS處理。
第二步:系統引導過程Boot Sequence(BIOS)
POST 過程結束後,系統的控制權從BISO 轉交到 boot loader。Bootloader 一般存儲在系統的硬盤上(傳統的 BIOS/MBR 系統),這個時候機器不能獲取外部的存儲或者網絡信息,一些重要的值(日期、時間、其他外部值)都是從CMOS裏讀取.
POST(POST-power onself test)—>ROM->CMOS(互補金屬氧化物)->BIOS (Basic Input Output System,基礎輸入輸出系統)
BIOS(BasicInput/Output System)啓動初始化硬件的工作,包括屏幕和鍵盤,內存檢測,這個過程也被成爲POST(Power On Self Test),通過ROM加載自檢程序,然後按照 CMOS RAM 中設置的啓動設備查找順序,來尋找可啓動設備 。注:BIOS 程序嵌在主板的 ROM 芯片上的。
第三步:啓動加載器階段Master Boot Loader(MBR)
硬盤上第0磁道第一個扇區被稱爲MBR,也就是Master Boot Record,即主引導記錄,決定啓動介質按照BIOS所設定的系統啓動流程,根據引導次序(Boot Sequence)自上而下的尋找對應存儲設備上操作系統的MBR。它的大小僅有512字節,但裏面卻存放了預啓動信息、分區表信息。可分爲兩部分:
第一部分爲引導(PRE-BOOT)區,佔了 446個字節;
第二部分爲分區表(PARTITION TABLE),共有64個字節,每個主分區佔用16字節,記錄硬盤的分區信息。(這就是爲什麼一塊硬盤只能有4個主分區)分區表有效性標記會佔用2字節。
預引導區的作用之一是找到標記爲活動(ACTIVE)的分區,並將活動分區的引導區讀入內存。剩餘兩個字節爲結束標記。尋找 grub,讀取配置文件/etc/grub.conf,決定默認啓動項根據MBR所指引的活動分區上尋找系統分區中的 bootloader.在bootloader當中配置了所要引導操作系統的內核所在的位置,因此BIOS被載入內存以後,當它實現將控制權限轉交給bootloader以後,bootloader接收整個系統的控制權限,而後根據用戶的選擇去讀取相應操作系統中的內核,並將內核裝載入內存的某個空間位置,解壓縮,這時kernel就可以在內存中活動,並根據kernel本身功能在內存當中探索硬件並加載硬件驅動程序並完成內核初始化,bootloader會將控制權限轉交給內核。
第四步:引導加載器階段(GRUB加載器)
對於GRUB來說,一個比較好的方面就是它包含了linux文件系統的支持。GRUB能夠從ext2或者ext3文件系統中加載linux內核。一旦Bootloader的第一階段已完成MBR(啓動加載器階段),並能找到實際的引導加載程序位置,第1階段啓動加載器加載引導程序到內存中開始第二階段。GRUB引導加載器階段它是通過將本來兩階段的boot loader轉換成三個階段的boot loader。
stage1個階段 :BIOS加載MBR裏面的GRUB(屬於第1階段的文件),由於只有GRUB只佔用446字節所以不能實現太多的功能,所以就有此階段裏面的文件來加載第1.5階段的文件(/boot/grub下的文件)
stage1.5個階段:這個階段裏面的就是加載識別文件系統的程序,來識別文件系統,不加載就無法識別文件系統,進而就找不到boot目錄,由於GRUB是無法識別LVM,所以你不能把/boot分區設置爲LVM,所以必須要把/boot單獨分區
stage2個階段:這裏面纔是正在的開始尋找內核的過程,然後是啓動內核
(當stage1.5的bootloader被加載並運行時,stage2 的bootloader才能被加載。)
當stage2被加載時,GRUB能根據請求的情況顯示一個可選內核的清單(在 /etc/grub.conf 中進行定義,同時還有幾個軟符號鏈接/etc/grub/menu.lst 和 /etc/grub.conf)。你可以選擇一個內核,修改其附加的內核參數。同時,你可以選擇使用命令行的shell來對啓動過程進行更深層次的手工控制。
GRUB的配置文件中的配置哪些信息
在第二階段boot loader加載到內存中後,就可以對文件系統進行查詢了,同時,默認的內核鏡像以及初始化內存盤鏡像也被加載到內存中。
根據grub設定的內核映像所在路徑,系統讀取內存映像,並進行解壓縮操作。此時,屏幕一般會輸出“Uncompressing Linux(解壓內核中)”的提示。當解壓縮內核完成後,屏幕輸出“OK, booting the kernel(正在啓動內核)”。
**第五步:加載kernel **
GRUB把內核加載到內存後展開並運行,此時GRUB的任務已經完成,接下來內核將會接管並完成:
探測硬件—>加載驅動—>掛載根文件系統—>切換至根文件系統(rootfs)—>運行/sbin/init完成系統初始化
內核一般都是壓縮的,所以它的首要任務是解壓縮,然後檢查和分析系統的硬件並初始化內核裏的硬件驅動程序。內核剛加載到內存的時候,文件系統還不能使用,它使用的是 Boot Loader 加載進內存的 initramfs。系統將解壓後的內核放置在內存之中,並調用start_kernel()函數來啓動一系列的初始化函數並初始化各種設備,完成Linux核心環境的建立。至此,Linux內核已經建立起來了,基於Linux的程序應該可以正常運行了。
第六步:初始化initrd /etc/inittab
在覈心加載完畢,進行完硬件偵測與驅動程序加載後,內核會啓動第一個進程/sbin/init, init進程將會讀取/etc/inittab,init進程是系統所有進程的起點,你可以把它比擬成系統所有進程的老祖宗,沒有這個進程,系統中任何進程都不會啓動。
/etc/inittab最主要的功能就是準備軟件運行的環境,包括系統的主機名稱、網絡配置、語系處理、文件系統格式及其他服務的啓動等,而所有的動作都根據在/etc/inittab中的配置.將會執行/etc/inittab來設定系統運行的默認級別,
init進程首先會讀取/etc/inittab文件,根據inittab文件中的內容依次執行 設定系統運行的默認級別(id:3:initdefault:)執行系統初始化腳本文件(si::sysinit:/etc/rc.d/rc.sysinit) 執行在該運行級別下所啓動或關閉對應的服務(l3:3:wait:/etc/rc.d/rc 3) 啓動6個虛擬終端
0-6:7個級別的定義
0:關機, shutdown
1:單用戶模式(singleuser),root用戶,無須認證;維護模式;
2:多用戶模式(multiuser),會啓動網絡功能,但不會啓動NFS;維護模式;
3:多用戶模式(mutliuser),完全功能模式;文本界面;
4:預留級別:目前無特別使用目的,但習慣以同3級別功能使用;
5:多用戶模式(multi user),完全功能模式,圖形界面;
6:重啓,reboot
許多程序需要開機啓動。它們在Windows叫做"服務"(service),在Linux就叫做"守護進程"(daemon)。init進程的一大任務,就是去運行這些開機啓動的程序。但是,不同的場合需要啓動不同的程序,比如用作服務器時,需要啓動Apache,用作桌面就不需要。
Linux允許爲不同的場合,分配不同的開機啓動程序,這就叫做"運行級別"(runlevel)。也就是說,啓動時根據"運行級別",確定要運行哪些程序。
要訪問根文件系統必須要加載根文件系統所在的設備,而這時根文件系統又沒有掛載,要掛載根文件系統有需要根文件系統的驅動程序,這是一個典型的先有雞先有蛋的問題!爲解決這個問題,GRUB在加載內核同時,也把initrd加載到內存中並運行.那麼initrd又起到了什麼作用哪?
initrd展開後的文件
linux中/下的文件
我們可以看到,其實initrd文件其實是一個虛擬的根文件系統,裏面有bin、lib、lib64、sys、var、etc、sysroot、dev、proc、tmp等根目錄,它的功能就是講內核與真正的根建立聯繫,內核通過它加載根文件系統的驅動程序,然後以讀寫方式掛載根文件系統,至此,內核加載完成。
第七步:運行/sbin/init,進行系統初始化
/sbin/init 最主要的功能就是準備軟件運行的環境,包括系統的主機名稱、網絡配置、語系處理、文件系統格式及其他服務的啓動等,而所有的動作都根據在/etc/inittab中的配置.init首先運行/etc/init/rcS.conf腳本,如下圖
第八步:啓動系統服務/etc/rc.d/rc.sysinit
可以看到,init進程通過執行/etc/rc.d/rcS.conf首先調用了/etc/rc.d/rc.sysinit,對系統做初始化設置,設置好整個系統環境。我們來看看這個腳本都是做了些什麼哪?
事實上init執行/etc/rc.d/rc.sysinit的初始化將會做很多設置:
1、獲得網絡環境
2、掛載設備
3、開機啓動畫面Plymouth(取替了過往的 RHGB)
4、判斷是否啓用SELinux
5、顯示於開機過程中的歡迎畫面
6、初始化硬件
7、用戶自定義模塊的加載
8、配置內核的參數
9、設置主機名
10、同步存儲器
11、設備映射器及相關的初始化
12、初始化軟件磁盤陣列(RAID)
13、初始化 LVM 的文件系統功能
14、檢驗磁盤文件系統(fsck)
15、設置磁盤配額(quota)
16、重新以可讀寫模式掛載系統磁盤
17、更新quota(非必要)
18、啓動系統虛擬隨機數生成器
19、配置機器(非必要)
20、清除開機過程當中的臨時文件
21、創建ICE目錄
22、啓動交換分區(swap)
23、將開機信息寫入/var/log/dmesg文件中
第九步:啓動配置文件/etc/rc.d/rc.n
設定玩系統默認運行級別以後,接着調用/etc/rc.d/rc腳本,/etc/rc.d, 裏面存放了rc.local, rc.sysinit,init.d, rcX.d (X包括0-6對應相對runlevel).這個腳本接收默認運行級別參數後,依腳本設置啓用或停止/etc/rc.d/rc[0-6].d/中相應的程序。
/etc/rc.d/rc3].d/下的腳本文件在系統初始化階段,腳本名字以K開頭的,表示STOP動作(關閉),名字以S開頭,表示Start動作(啓動),文件名K/S 後面的的數字代表優先級,名稱中的數字表示執行次序(優先級),數字越小表示越先執行,優先級越高
第十步:用戶自定義開機啓動程序 (/etc/rc.d/rc.local)
系統根據runlevel啓動完rcX.d中的腳本之後,會調用rc.local腳本,如果你有一個腳本命令不論在3和5都想開機啓動,那麼就添加於此,免去rc3.d和rc5.d分別增加啓動腳本工作量.最後,將執行/etc/rc.d/rc.local腳本,可以根據自己的需求將一些執行命令或者腳本寫到其中,當開機時就可以加載。
第十一步:打印登錄提示符
系統初始化完成後,init給出用戶登錄提示符(login)或者圖形化登錄界面,用戶輸入用戶和密碼登陸後,系統會爲用戶分配一個用戶ID(uid)和組ID(gid),這兩個ID是用戶的身份標識,用於檢測用戶運行程序時的身份驗證。登錄成功後,整個系統啓動流程運行完畢!
7、爲運行於虛擬機上的CentOS 6添加一塊新硬件,提供兩個主分區;
(1) 爲硬盤新建兩個主分區;併爲其安裝grub;
(2) 爲硬盤的第一個主分區提供內核和ramdisk文件;爲第二個分區提供rootfs;
(3) 爲rootfs提供bash、ls、cat程序及所依賴的庫文件;
(4) 爲grub提供配置文件;
(5) 將新的硬盤設置爲第一啓動項並能夠正常啓動目標主機;
# fdisk/dev/sdb
~]# fdisk -l| grep sdb
Disk/dev/sdb: 21.5 GB, 21474836480 bytes
/dev/sdb1 1 10 80293+ 83 Linux
/dev/sdb2 11261020884500 83 Linux
~]# mke2fs-t ext4 /dev/sdb1
~]# mke2fs-t ext4 /dev/sdb2
~]# mount/dev/sdb1 /mnt
~]#grub-install --root-directory=/mnt/dev/sdb
Probingdevices to guess BIOS drives. Thismay take a long time.
Installationfinished. No error reported.
This is thecontents of the device map/mnt/boot/grub/device.map.
Check ifthis is correct or not. If any ofthe lines is incorrect,
fix it andre-run the script `grub-install‘.
(fd0)/dev/fd0
(hd0)/dev/sda
(hd1)/dev/sdb
~]#cp/boot/initramfs-2.6.32-431.el6.x86_64.img /mnt/initramfs
~]# cp/boot/vmlinuz-2.6.32-431.el6.x86_64/mnt/vmlinuz
~]# vim/mnt/boot/grub/grub.conf
default=0
timeout=5
titleCentOS6(test)
root (hd0,0)
kernel/vmlinuz ro root=/dev/sdb2 selinux=0init=/bin/bash
initrd/initramfs
~]# umount/dev/sdb1
~]# mount/dev/sdb2 /mnt
~]# mkdir-p/mnt/{bin,sbin,lib,lib64,etc,home,root,media,mnt,dev,tmp}
~]# mkdir -p/mnt/{usr/{bin,sbin,lib,lib64},var/{lib,lib64,log,local,cache},proc,sys,selinux}
~]# which ls
alias ls=‘ls--color=auto‘
/bin/ls
~]# whichbash
/bin/bash
~]# whichcat
/bin/cat
~]# cp/bin/{bash,ls,cat} /mnt/bin
~]# ldd/bin/{bash,ls,cat}|grep -Eo"/lib.*[[:space:]]"| sort -u
/lib64/ld-linux-x86-64.so.2
/lib64/libacl.so.1
/lib64/libattr.so.1
/lib64/libcap.so.2
/lib64/libc.so.6
/lib64/libdl.so.2
/lib64/libpthread.so.0
/lib64/librt.so.1
/lib64/libselinux.so.1
/lib64/libtinfo.so.5
~]# cp `ldd/bin/{bash,ls,cat}|grep -Eo"/lib.*[[:space:]]"| sort -u` /mnt/lib64
~]# sync
~]#init 6
8、寫一個腳本
(1) 能接受四個參數:start, stop, restart, status
start: 輸出“starting 腳本名 finished.”
...
(2) 其它任意參數,均報錯退出;
[root@moban~]# vi week5-08.sh
[root@moban ~]# cat week5-08.sh
#!/bin/bash
#author: wangyafei
#
cat <<EOF
script argument
======================================
start ) start the program ||
======================================
EOF
declare -i flag=0
read -p "Select the parameters to be operated: " ARGU
START="Starting the ${0} finished."
case $ARGU in
start)
echo "$START"
flag=0
;;
*)
echo "You select the wrongoption,${0} exit!"
exit 12 #wrong option
;;
esac
[root@moban ~]# bash week5-08.sh
script argument
======================================
start ) start the program ||
======================================
Select the parameters to be operated: start
Starting the week5-08.sh finished.
[root@moban ~]# bash week5-08.sh
script argument
======================================
start ) start the program ||
======================================
Select the parameters to be operated: stop
You select the wrong option,week5-08.sh exit!
[root@moban ~]#
10、寫一個腳本,顯示用戶選定要查看的信息;
cpu) display cpuinfo
mem) displaymemory info
disk) displaydisk info
quit) quit
非此四項選擇,則提示錯誤,並要求用戶重新選擇,只到其給出正確的選擇爲止;
[root@moban~]# vi week5-10.sh
[root@moban ~]# cat week5-10.sh
#!/bin/bash
#author:wangyafei
#
choice=‘null‘
until [ $choice == ‘cpu‘ -o$choice == ‘mem‘ -o $choice == ‘disk‘ -o $choice ==‘quit‘ ];do
cat <<EOF
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
EOF
read -p "Input an option: "choice
choice=${choice:=‘null‘}
done
case $choice in
cpu)
cat /proc/cpuinfo
;;
mem)
free -mh
;;
disk)
fdisk -l
;;
quit)
echo "exit the script."
;;
esac
[root@moban ~]# bash week5-10.sh
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
Input an option: cpa
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
Input an option: cpu
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 94
model name : Intel(R) Core(TM) i5-6500CPU @ 3.20GHz
stepping : 3
microcode : 0x74
cpu MHz : 3191.663
cache size : 6144 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 2
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr paemce cx8apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ssht syscallnx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts noplxtopologytsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fmacx16 pcidsse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avxf16c rdrandhypervisor lahf_lm abm 3dnowprefetch ida arat pln pts dtherm hwphwp_noitfyhwp_act_window hwp_epp fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2invpcid rtmrdseed adx smap xsaveopt
bogomips : 6384.49
clflush size : 64
cache_alignment : 64
address sizes : 42 bits physical, 48bits virtual
power management:
processor : 1
vendor_id : GenuineIntel
cpu family : 6
model : 94
model name : Intel(R) Core(TM) i5-6500CPU @ 3.20GHz
stepping : 3
microcode : 0x74
cpu MHz : 3191.663
cache size : 6144 KB
physical id : 0
siblings : 2
core id : 1
cpu cores : 2
apicid : 1
initial apicid : 1
fpu : yes
fpu_exception : yes
cpuid level : 22
wp : yes
flags : fpu vme de pse tsc msr paemce cx8apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ssht syscallnx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts noplxtopologytsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fmacx16 pcidsse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avxf16c rdrandhypervisor lahf_lm abm 3dnowprefetch ida arat pln pts dtherm hwphwp_noitfyhwp_act_window hwp_epp fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2invpcid rtm rdseedadx smap xsaveopt
bogomips : 6384.49
clflush size : 64
cache_alignment : 64
address sizes : 42 bits physical, 48bits virtual
power management:
[root@moban ~]# bash week5-10.sh
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
Input an option: disk
磁盤 /dev/sda:128.8 GB,128849018880 字節,251658240個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512字節 /512 字節
I/O 大小(最小/最佳):512 字節 /512 字節
磁盤標籤類型:dos
磁盤標識符:0x000aaf3f
設備 Boot Start End Blocks Id System
/dev/sda1 * 2048 4196351 2097152 83 Linux
/dev/sda2 4196352 96479231 46141440 8e Linux LVM
磁盤 /dev/sdb:53.7 GB,53687091200 字節,104857600個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512字節 /512 字節
I/O 大小(最小/最佳):512 字節 /512 字節
磁盤標籤類型:dos
磁盤標識符:0x9a210386
設備 Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 8e Linux LVM
/dev/sdb2 20973568 41945087 10485760 8e Linux LVM
磁盤 /dev/mapper/cl-root:42.9GB,42945478656 字節,83877888 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512字節 /512 字節
I/O 大小(最小/最佳):512 字節 /512 字節
磁盤 /dev/mapper/cl-swap:4294MB,4294967296 字節,8388608 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512字節 /512 字節
I/O 大小(最小/最佳):512 字節 /512 字節
磁盤 /dev/mapper/myvg-mylv1-real:7516 MB,7516192768 字節,14680064 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512字節 /512 字節
I/O 大小(最小/最佳):512 字節 /512 字節
磁盤 /dev/mapper/myvg-mylv1:7516MB,7516192768 字節,14680064 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512字節 /512 字節
I/O 大小(最小/最佳):512 字節 /512 字節
磁盤 /dev/mapper/myvg-snaplv-cow:33 MB,33554432 字節,65536 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512字節 /512 字節
I/O 大小(最小/最佳):512 字節 /512 字節
磁盤 /dev/mapper/myvg-snaplv:7516 MB,7516192768 字節,14680064 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512字節 /512 字節
I/O 大小(最小/最佳):512 字節 /512 字節
磁盤 /dev/mapper/myvg-mylv1_bak-cow:3221 MB,3221225472 字節,6291456 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512字節 /512 字節
I/O 大小(最小/最佳):512 字節 /512 字節
磁盤 /dev/mapper/myvg-mylv1_bak:7516 MB,7516192768 字節,14680064 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512字節 /512 字節
I/O 大小(最小/最佳):512 字節 /512 字節
[root@moban~]# bash week5-10.sh
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
Input an option: mem
total used free shared buff/cache available
Mem: 1.9G 243M 1.4G 8.9M 343M 1.5G
Swap: 4.0G 0B 4.0G
[root@moban~]# bash week5-10.sh
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
Input an option: quit
exit the script.
[root@wangyafei ~]#
[root@moban ~]# fdisk /dev/sdc
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): 10G
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (1-10, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-10, default 10):
Using default value 10
Command (m for help): w
The partition table has been altered!
11、寫一個腳本
(1) 用函數實現返回一個用戶的UID和SHELL;用戶名通過參數傳遞而來;
(2) 提示用戶輸入一個用戶名或輸入“quit”退出;
當輸入的是用戶名,則調用函數顯示用戶信息;
當用戶輸入quit,則退出腳本;進一步地:顯示鍵入的用戶相關信息後,再次提醒輸出用戶名或quit:
[root@moban ~]# cat week5-11.sh
#!/bin/bash
#author:wangyafei
#
choice=‘null‘
until [ $choice == ‘quit‘ ];do
echo "input quit could exit thisprogram."
read -p "Input one user name:" choice
choice=${choice:=null}
if [ $choice != ‘quit‘ -a $choice !=‘null‘];then
id $choice &>/dev/null
if [ $? -eq 0 ];then
cat /etc/passwd |grep $choice|awk -vFS=: -v OFS=: ‘{print $1,$3,$6}‘
fi
fi
done
echo "quit!"
[root@moban ~]# bash week5-11.sh
input quit could exit this program.
Input one user name: wangyafei
wangyafei:1000:/home/wangyafei
input quit could exit this program.
Input one user name: root
root:0:/root
operator:11:/root
input quit could exit this program.
Input one user name: quit
quit!
[root@moban ~]#
12、寫一個腳本,完成如下功能(使用函數)
(1) 提示用戶輸入一個可執行命令的名字;獲取此命令依賴的所有庫文件;
(2) 複製命令文件至/mnt/sysroot目錄下的對應的rootfs的路徑上,例如,如果複製的文件原路徑是/usr/bin/useradd,則複製到/mnt/sysroot/usr/bin/目錄中;
(3) 複製此命令依賴的各庫文件至/mnt/sysroot目錄下的對應的rootfs的路徑上;規則同上面命令相關的要求;
[root@moban ~]# vi week5-12.sh
[root@ moban ~]# cat week5-12.sh
#!/bin/bash
#author:wangyafei
#
command_target_dir=/mnt/sysroot/
lib_target_dir=/mnt/sysroot/rootfs/
function copy
{
if $1 &>/dev/null;then
command=$(which $1 | tail -1)
cp $command $command_target_dir
echo "$1 copy finished"
for i in { $(ldd $command | cut -d"" -f3) };do
cp $i $lib_target_dir &>/dev/null
done
echo "$1 lib file copy finished"
else
echo "error input .."
fi
}
echo "input a command or quit forquit:"
read input
until [ $input == "quit" ];do
if [ cd $command_target_dir&>/dev/null -a cd $lib_target_dir&>/dev/null ] ;then
copy $input
else
mkdir -p $command_target_dir&>/dev/null
mkdir -p $lib_target_dir &>/dev/null
copy $input
fi
echo "you can input a command again orquit for quit:"
read input
done
[root@ moban ~]# bash week5-12.sh
input a command or quit for quit:
ls
ls copy finished
ls lib file copy finished
you can input a command again or quit forquit:
quit
[root@ moban ~]#