预习作业(四)

1、编写脚本/root/bin/systeminfo.sh显示当前主机系统信息,包括主机名,IPV4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小

#!/bin/bash
# 
#***********************************************************
#Author:        heyijie
#Data:          2020-05-03
#FileName:      bin/systeminfo.sh
#Description:   The test script
#***********************************************************

BLUECOLOR="e[34m"
COLOREND="e[0m"

echo -e "$BLUECOLOR主机名为:$COLOREND`hostname`"
echo -e "${BLUECOLOR}ip地址为:$COLOREND`ifconfig | egrep -o "[0-9.]{7,15}" | head -1`"                                                                                   
echo -e "$BLUECOLOR操作系统版本为:$COLOREND`cat /etc/centos-release | grep -Eo "[0-9]+" | head -1`"
echo -e "$BLUECOLOR内核版本为:$COLOREND`uname -r`"
echo -e "${BLUECOLOR}CPU型号:$COLOREND`lscpu | grep "Model name" | tr -s ' '`"
echo -e "$BLUECOLOR内存大小为:$COLOREND`free -h | egrep -o "[0-9.]+[MG]i" | head -1`"
echo -e "$BLUECOLOR硬盘大小为:$COLOREND`lsblk | grep "^sd" | tr -s ' ' | cut -d' ' -f1,4`"

[root@centos8 ~]#bash bin/systeminfo.sh
 
主机名为:centos8
ip地址为:192.168.1.3
操作系统版本为:8
内核版本为:4.18.0-147.el8.x86_64
CPU型号:Model name: Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz
内存大小为:1.8Gi
硬盘大小为:sda 100G
sdb 20G
sdc 20G

2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中

#!/bin/bash
# 
#***********************************************************
#Author:        heyijie
#Data:          2020-05-03
#FileName:      bin/backup.sh
#Description:   The test script
#***********************************************************


cp -a /etc /root/etc`date +%F`            

3、编写脚本/root/bin/disk.sh显示当前硬盘分区中空间利用率最大的值

#!/bin/bash
# 
#***********************************************************
#Author:        heyijie
#Data:          2020-05-03
#FileName:      bin/disk.sh
#Description:   The test script
#***********************************************************

REDCOLOR='e[31m'
COLOREND='e[0m'

use=`df | grep "^/dev" | tr -s ' ' % | cut -d % -f5 | sort -n | tail -1`
echo -e "$REDCOLOR硬盘分区中空间利用率最大的值为:$COLOREND$use"            
[root@centos8 ~]#bash bin/disk.sh 
硬盘分区中空间利用率最大的值为:97

4、编写脚本/root/bin/links.sh显示正连接本机的每个远程主机的IPV4地址和连接数,并按连接数从大到小排序

#!/bin/bash
# 
#***********************************************************
#Author:        heyijie
#Data:          2020-05-03
#FileName:      bin/links.sh
#Description:   The test script
#***********************************************************


who | cut -d'(' -f2 | grep -Eo "[0-9.]+" | sort -n | uniq -c | sort -rn    
[root@centos8 ~]#bash bin/links.sh 
      2 192.168.1.6
      1 192.168.1.5
      1 192.168.1.10

5、使用sed命令在test.txt文件每一行后增加一空行

[root@centos8 ~]#cat test.txt 
a
b
c
d
e
f
[root@centos8 ~]#sed -r 's#$#
#' test.txt 
a

b

c

d

e

f

6、使用sed命令打印/etc/passwd的奇数行?

root@centos8 ~]#sed -n '1~2p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
原文地址:https://www.cnblogs.com/jojohyj/p/12822238.html