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、写一个脚本,完成如下功能

传递一个参数给脚本,此参数为gzipbzip2或者xz三者之一;

(1) 如果参数1的值为gzip,则使用targzip归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.gz

(2) 如果参数1的值为bzip2,则使用tarbzip2归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.bz2

(3) 如果参数1的值为xz,则使用tarxz归档压缩/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提供bashlscat程序及所依赖的库文件;

(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) 用函数实现返回一个用户的UIDSHELL;用户名通过参数传递而来;

(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 ~]#