第五次作業

本次作業內容:

1、寫一個腳本,完成如下功能

(1) 傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在;

(2) 如果存在,則顯示此設備上的所有分區信息;

 

[root@wangyafei ~]# vi week5-01.sh
[root@wangyafei ~]# cat week5-01.sh

#!/bin/bash
#author:Wangyafei
#
read -p "Please enter a disk device path:" path

    while [ -z"$path" ];do
        echo "Pleaseenter a correct disk device path."
        read -p "Pleaseenter a disk device path:" path
    done

    if [ -b"$path" ];then
        fdisk -l $path
    else
        echo "This isnot a disk device path."
    fi
[root@wangyafei ~]# bash week5-01.sh
Please enter a disk device path:/dev/sda

磁盤 /dev/sda128.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@wangyafei ~]# bash week5-01.sh
Please enter a disk device path:/dev/sdb

磁盤 /dev/sdb53.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@wangyafei ~]# bash week5-01.sh
Please enter a disk device path:/dev/sdc
This is not a disk device path.
[root@wangyafei ~]#

 

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@wangyafei ~]# vi week5-02.sh
[root@wangyafei ~]# 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@wangyafei ~]# bash week5-02.sh
pelase input a argu(gzip/bzip2/xz):gzip
[root@wangyafei ~]# ls /backups
etc-20170318.tar.gz
[root@wangyafei ~]# bash week5-02.sh
pelase input a argu(gzip/bzip2/xz):bzip2
[root@wangyafei ~]# ls /backups
etc-20170318.tar.bz2  etc-20170318.tar.gz
[root@wangyafei ~]# bash week5-02.sh
pelase input a argu(gzip/bzip2/xz):xz   
[root@wangyafei ~]# ls /backups
etc-20170318.tar.bz2  etc-20170318.tar.gz  etc-20170318.tar.xz
[root@wangyafei ~]# bash week5-02.sh
pelase input a argu(gzip/bzip2/xz):wangyafei
error compression tools

 

3、寫一個腳本,接受一個路徑參數:

(1) 如果爲普通文件,則說明其可被正常訪問;

(2) 如果是目錄文件,則說明可對其使用cd命令;

(3) 如果爲符號鏈接文件,則說明是個訪問路徑;

(4) 其它爲無法判斷;

 

[root@wangyafei ~]# vi week5-03.sh
[root@wangyafei ~]# 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@wangyafei ~]# bash week5-03.sh
invalid argument, one argument
[root@wangyafei ~]# bash week5-03.sh /dev
folder,you can use cd .....
[root@wangyafei ~]# bash week5-03.sh /backups/etc-20170318.tar.bz2
general file,you can use ls ....
[root@wangyafei ~]# bash week5-03.sh /backups/?
sorry, i can not judge...
[root@wangyafei ~]#

 

4、寫一個腳本,取得當前主機的主機名,判斷

(1) 如果主機名爲空或爲localhost,或爲"(none)",則將其命名爲mail.magedu.com;

(2) 否則,顯示現有的主機名即可;

 

[root@wangyafei ~]# vi week5-04.sh
[root@wangyafei ~]# 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@wangyafei ~]# 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@wangyafei ~]# vi week5-05.sh
[root@wangyafei ~]# 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@wangyafei ~]# bash week5-05.sh
ready to copy file wait a moment please.
Operation completed!

 

6、請詳細描述CentOS系統的啓動流程(詳細到每個過程系統做了哪些事情)

 

Linux系統啓動流程分析與關機流程 -Argorse - 51CTO技術博客 http://wangyafei.blog.51cto.com/4789821/1908149

 

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. This may 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 of the 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=0 init=/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

                             

wKioL1jOf3Xx5d9JAAE5M8YyTRE862.jpg-wh_50

wKiom1jOf3WQh2jtAABC1r0IoiU919.jpg-wh_50

wKioL1jOf3aw9SgbAAC3nWoDPR8518.jpg-wh_50




8、寫一個腳本

(1) 能接受四個參數:start,stop, restart, status

start: 輸出“starting 腳本名 finished.”

...

(2) 其它任意參數,均報錯退出;

 

[root@wangyafei ~]# vi week5-08.sh
[root@wangyafei ~]# 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@wangyafei ~]# bash week5-08.sh
script argument
======================================
start   ) start the program       ||
======================================
Select the parameters to be operated: start
Starting the week5-08.sh finished.
[root@wangyafei ~]# 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@wangyafei ~]#

 

9、寫一個腳本,判斷給定的用戶是否登錄了當前系統;

(1) 如果登錄了,則顯示用戶登錄,腳本終止;

(2) 每3秒鐘,查看一次用戶是否登錄;

 

[root@wangyafei ~]# vi week5-09.sh
[root@wangyafei ~]# cat week5-09.sh

#!/bin/bash
#author:wangyafei
#
read -p "please input username:" username
until who | grep "\b$username\b" &>/dev/null ;do
echo "$username not login "
sleep 3
done
echo "$username login!!"
exit 0

[root@wangyafei ~]# bash week5-09.sh
please input username:wangyafei
wangyafei not login
wangyafei not login
wangyafei not login
^Z
[3]+ 
已停止               bash week5-09.sh
[root@wangyafei ~]# bash week5-09.sh
please input username:root
root login!!
[root@wangyafei ~]#

 

10、寫一個腳本,顯示用戶選定要查看的信息;

cpu) display cpu info

mem) display memory info

disk) display disk info

quit) quit

非此四項選擇,則提示錯誤,並要求用戶重新選擇,只到其給出正確的選擇爲止;

 

[root@wangyafei ~]# vi week5-10.sh
[root@wangyafei ~]# 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@wangyafei ~]# 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 cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ssht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts noplxtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fmacx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avxf16c rdrand hypervisor lahf_lm abm 3dnowprefetch ida arat pln pts dtherm hwphwp_noitfy hwp_act_window hwp_epp fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2invpcid rtm rdseed 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 cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ssht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts noplxtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fmacx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avxf16c rdrand hypervisor lahf_lm abm 3dnowprefetch ida arat pln pts dtherm hwphwp_noitfy hwp_act_window hwp_epp fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2invpcid rtm rdseed adx smap xsaveopt
bogomips    : 6384.49
clflush size : 64
cache_alignment : 64
address sizes   : 42 bits physical, 48bits virtual
power management:

[root@wangyafei ~]# bash week5-10.sh
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
Input an option: disk

磁盤 /dev/sda128.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/sdb53.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-root42.9 GB,42945478656 字節,83877888 個扇區
Units =
扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理)512字節 / 512 字節
I/O
大小(最小/最佳)512 字節 / 512 字節


磁盤 /dev/mapper/cl-swap4294 MB,4294967296 字節,8388608 個扇區
Units =
扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理)512字節 / 512 字節
I/O
大小(最小/最佳)512 字節 / 512 字節


磁盤 /dev/mapper/myvg-mylv1-real7516 MB,7516192768 字節,14680064 個扇區
Units =
扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理)512字節 / 512 字節
I/O
大小(最小/最佳)512 字節 / 512 字節


磁盤 /dev/mapper/myvg-mylv17516 MB,7516192768 字節,14680064 個扇區
Units =
扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理)512字節 / 512 字節
I/O
大小(最小/最佳)512 字節 / 512 字節


磁盤 /dev/mapper/myvg-snaplv-cow33 MB,33554432 字節,65536 個扇區
Units =
扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理)512字節 / 512 字節
I/O
大小(最小/最佳)512 字節 / 512 字節


磁盤 /dev/mapper/myvg-snaplv7516 MB,7516192768 字節,14680064 個扇區
Units =
扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理)512字節 / 512 字節
I/O
大小(最小/最佳)512 字節 / 512 字節


磁盤 /dev/mapper/myvg-mylv1_bak-cow3221 MB,3221225472 字節,6291456 個扇區
Units =
扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理)512字節 / 512 字節
I/O
大小(最小/最佳)512 字節 / 512 字節


磁盤 /dev/mapper/myvg-mylv1_bak7516 MB,7516192768 字節,14680064 個扇區
Units =
扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理)512字節 / 512 字節
I/O
大小(最小/最佳)512 字節 / 512 字節

[root@wangyafei ~]# 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@wangyafei ~]# 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 ~]#

 

11、寫一個腳本

(1) 用函數實現返回一個用戶的UID和SHELL;用戶名通過參數傳遞而來;

(2) 提示用戶輸入一個用戶名或輸入“quit”退出;

當輸入的是用戶名,則調用函數顯示用戶信息;

當用戶輸入quit,則退出腳本;進一步地:顯示鍵入的用戶相關信息後,再次提醒輸出用戶名或quit:

 

[root@wangyafei ~]# vi week5-11.sh
[root@wangyafei ~]# 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 -v FS=: -v OFS=: '{print $1,$3,$6}'
    fi 
   fi 
done
echo "quit!"

[root@wangyafei ~]# 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@wangyafei ~]#

 

12、寫一個腳本,完成如下功能(使用函數)

(1) 提示用戶輸入一個可執行命令的名字;獲取此命令依賴的所有庫文件;

(2) 複製命令文件至/mnt/sysroot目錄下的對應的rootfs的路徑上,例如,如果複製的文件原路徑是/usr/bin/useradd,則複製到/mnt/sysroot/usr/bin/目錄中;

(3) 複製此命令依賴的各庫文件至/mnt/sysroot目錄下的對應的rootfs的路徑上;規則同上面命令相關的要求;

 

[root@wangyafei ~]# vi week5-12.sh
[root@wangyafei ~]# 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@wangyafei ~]# 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@wangyafei ~]#


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章