本次作业内容:

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

                             

第五次作业_作业

第五次作业_Linux_02

第五次作业_作业_03




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