马哥博客作业第四周

1.编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
[root@Centos8 ~]# mkdir /root/bin
[root@Centos8 ~]# cat /root/bin/systeminfo.sh
#!/bin/bash
echo -e "My hostname is `hostname`"
echo -e "IP address is `ifconfig ens33 |grep -Eo '([0-9]{1,3}.){3}[0-9]{1,3}'|head -n1`"
echo -e "OS version is `cat /etc/redhat-release`"
echo -e "Kernel version is `uname -r`"
echo -e "CPU type is `lscpu|grep "Model name" |cut -d: -f2 |tr -s " "`"
echo -e "Memtotol is `cat /proc/meminfo |head -n1 |grep -Eo '[0-9]+.*'`"
echo -e "Disk space is `lsblk |grep 'sda>'|grep -Eo '[0-9]+[[:upper:]]'`"

[root@Centos8 ~]# bash -n /root/bin/systeminfo.sh
[root@Centos8 ~]# bash /root/bin/systeminfo.sh
My hostname is Centos8
IP address is 192.168.88.128
OS version is CentOS Linux release 8.1.1911 (Core)
Kernel version is 4.18.0-147.el8.x86_64
CPU type is Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
Memtotol is 4011592 kB
Disk space is 200G

2.编写脚本/root/bin/backup.sh,可实现每日将/etc目录备份到/root/etcYYYY-mm-dd中
[root@Centos8 ~]# cat /root/bin/backup.sh
#!/bin/bash
cp -rv /etc/ /root/etc`date +%F`

[root@Centos8 ~]# bash /root/bin/backup.sh

[root@Centos8 ~]# ls -ld /root/etc2020-05-07/* | wc -l
260


3.编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
[root@Centos8 ~]# cat /root/bin/disk.sh
#!/bin/bash
df -TH | grep /dev/sd.* | tr -s ' ' % | cut -d % -f 6 | sort -nr | head -n1
[root@Centos8 ~]# bash -n /root/bin/disk.sh
[root@Centos8 ~]# bash /root/bin/disk.sh
29

4.编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序
[root@Centos8 ~]# cat /root/bin/links.sh
#!/bin/bash
ss -nt | grep 'ESTAB' | tr -s ' ' ':' | cut -d: -f6 | sort | uniq -c | sort -nr

[root@Centos8 ~]# bash -n /root/bin/links.sh
[root@Centos8 ~]# bash /root/bin/links.sh
1 192.168.88.1

5.使用sed命令在test.txt文件中每一行后增加一空行
[root@Centos8 ~]# sed G test.txt

6.使用sed命令打印/etc/passwd的奇数行
[root@Centos8 ~]# sed -nr '1~2p' /etc/passwd

原文地址:https://www.cnblogs.com/zt88/p/12841539.html