1.编写脚本 systeminfo.sh,显示当前主机系统信息,包括主机名, IPv4地址,操作系统版本,内核版本, CPU型号,内存大小,硬盘大小
[root@localhost data]# cat sysinfo.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: ???
#Date: 2020-08-10
#FileName: sysinfo.sh
#********************************************************************
BEGINCOLOR="\033[$[RANDOM%7+31]m"
ENDCOLOR="\033[0m"
echo -e $BEGINCOLOR"--------------Systeminfo----------------"$ENDCOLOR
echo -e $BEGINCOLOR"My hostname is `hostname`"$ENDCOLOR
echo -e $BEGINCOLOR"IP address is `ifconfig ens33|grep "netmask"|tr -s " "|cut -d" " -f3`"$ENDCOLOR
echo -e $BEGINCOLOR"OS version is `cat /etc/centos-release`"$ENDCOLOR
echo -e $BEGINCOLOR"Kernel version is `uname -r`"$ENDCOLOR
echo -e $BEGINCOLOR"CPU type is `lscpu|grep "Model name"|cut -d: -f2|tr -s " "`"$ENDCOLOR
echo -e $BEGINCOLOR"Memtotal is `cat /proc/meminfo |head -1|cut -d: -f2|tr -s " "`"$ENDCOLOR
echo -e $BEGINCOLOR"Disk space is `lsblk|grep "disk"|tr -s " "|cut -d" " -f4`"$ENDCOLOR
echo -e $BEGINCOLOR"-----------------END--------------------"$ENDCOLOR
[root@localhost data]# bash sysinfo.sh
--------------Systeminfo----------------
My hostname is localhost.localdomain
IP address is 10.50.100.12
OS version is CentOS Linux release 7.6.1810 (Core)
Kernel version is 3.10.0-957.el7.x86_64
CPU type is Intel(R) Core(TM) i5-8500 CPU @ 3.00GHz
Memtotal is 2028116 kB
Disk space is 200G
-----------------END--------------------
2.编写脚本 backup.sh,可实现每日将/etc/目录备份到/backup/etcYYYYmm-dd中
[root@localhost data]# cat backup.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: ???
#Date: 2020-08-10
#FileName: backup.sh
#********************************************************************
set -u
set -e
BEGINCOLOR="\033[$[RANDOM%7+31]m"
ENDCOLOR="\033[0m"
DIR=/data/backup/
echo -e $BEGINCOLOR"Start backup!"$ENDCOLOR
if [ ! -d "$DIR" ];then
mkdir -p "$DIR/etc`date +F%`"
fi
(sleep 10)
(cp -a /etc/ /data/backup/etc`date +%F`)
echo -e $BEGINCOLOR"Backup complete!"$ENDCOLOR
[root@localhost data]# bash backup.sh
Start backup!
Backup complete!
[root@localhost data]# ll backup/
total 12
drwxr-xr-x. 78 root root 8192 Aug 7 16:00 etc2020-08-10
3.编写脚本 disk.sh,显示当前硬盘分区中空间利用率最大的值
[root@localhost ~]# bash /data/disk.sh
------------System Maximum Disk Space-------------
/dev/sda1 1014M 127M 888M 13% /boot
----------------------END------------------------
[root@localhost ~]# cat /data/disk.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: ???
#Date: 2020-08-11
#FileName: disk.sh
#********************************************************************
set -u
set -e
BENGINCOLOR="\033[$[RANDOM%7+31]m"
ENDCOLOR="\033[0m"
echo -e $BENGINCOLOR"------------System Maximum Disk Space-------------"$ENDCOLOR
echo -e $BENGINCOLOR"`df -h|grep "/dev/sd"|tr -s " "|sort -t " " -k5|head -1`"$ENDCOLOR
echo -e $BENGINCOLOR"----------------------END------------------------"$ENDCOLOR
4.编写脚本 links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序
[root@localhost ~]# vim /data/links.sh
[root@localhost ~]# bash /data/links.sh
--------Maximum number of user connections------------
1 10.0.0.110
---------------------END------------------------------
[root@localhost ~]# cat /data/links.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ:???
#Date: 2020-08-11
#FileName: /data/links.sh
#********************************************************************
set -u
set -e
BEGINCOLOR="\033[$[RANDOM%7+31]m"
ENDCOLOR="\033[0m"
echo -e $BEGINCOLOR"--------Maximum number of user connections------------"$ENDCOLOR
echo -e $BEGINCOLOR"`netstat -tan|grep "ESTAB"|tr -s " " ":"|cut -d: -f6 |sort |uniq -c|sort -nr`"$ENDCOLOR
echo -e $BEGINCOLOR"---------------------END------------------------------"$ENDCOLOR
5.编写脚本 sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的UID之和
[root@localhost ~]# bash /data/sumid.sh
UID of the 10th user is 11
UID of the 20th user is 1001
Sum of UID of two users is 1012
[root@localhost ~]# cat /data/sumid.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: ???
#Date: 2020-08-11
#FileName: /data/sumid.sh
#********************************************************************
set -u
set -e
BEGINCOLOR="\033[$[RANDOM%7+31]m"
ENDCOLOR="\033[0m"
user10=`cat /etc/passwd |head -10|tail -1|cut -d: -f3`
user20=`cat /etc/passwd |head -20|tail -1|cut -d: -f3`
echo -e $BEGINCOLOR"UID of the 10th user is $user10"$ENDCOLOR
echo -e $BEGINCOLOR"UID of the 20th user is $user20"$ENDCOLOR
let sum=$user10+$user20
echo -e $BEGINCOLOR"Sum of UID of two users is $sum"$ENDCOLOR
6.编写脚本 sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和
[root@localhost data]# bash sumspace.sh
Please enter the path1 value: /data/f2.txt
/data/f2.txt
Please enter the path2 value: /data/f3.txt
/data/f3.txt
Sum of blank lines in the first file is 6
Sum of blank lines in the second file is 7
Sum of all blank lines is 13
[root@localhost data]# cat sumspace.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: ???
#Date: 2020-08-11
#FileName: sumspace.sh
#********************************************************************
set -u
set -e
BEGINCOLOR="\033[$[RANDOM%6+31]m"
ENDCOLOR="\033[0m"
read -p "Please enter the path1 value: " path1
echo -e $BEGINCOLOR"$path1"$ENDCOLOR
read -p "Please enter the path2 value: " path2
echo -e $BEGINCOLOR"$path2"$ENDCOLOR
space1=`cat $path1 |grep "^[[:space:]]*$"|wc -l`
echo -e $BEGINCOLOR"Sum of blank lines in the first file is $space1"$ENDCOLOR
space2=`cat $path2 |grep "^[[:space:]]*$"|wc -l`
echo -e $BEGINCOLOR"Sum of blank lines in the second file is $space2"$ENDCOLOR
let sum=$space1+$space2
echo -e $BEGINCOLOR"Sum of all blank lines is $sum"$ENDCOLOR
7.编写脚本 sumfile.sh, 统计/etc, /var, /usr 目录中共有多少个一级子目录和文件
[root@localhost data]# bash sumfile.sh
The sum of the first-level subdirectories and files is 182
The sum of the first-level subdirectories and files is 20
The sum of the first-level subdirectories and files is 12
The sum of the three directories is 214
[root@localhost data]# cat sumfile.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: ???
#Date: 2020-08-11
#FileName: sumfile.sh
#********************************************************************
set -u
set -e
BEGINCOLOR="\033[$[RANDOM%6+31]m"
ENDCOLOR="\033[0m"
etc=$[$(ls -l /etc/ |wc -l)-1]
echo -e $BEGINCOLOR"The sum of the first-level subdirectories and files is $etc"$ENDCOLOR
var=$[$(ls -l /var/ |wc -l)-1]
echo -e $BEGINCOLOR"The sum of the first-level subdirectories and files is $var"$ENDCOLOR
usr=$[$(ls -l /usr/ |wc -l)-1]
echo -e $BEGINCOLOR"The sum of the first-level subdirectories and files is $usr"$ENDCOLOR
echo -e $BEGINCOLOR"The sum of the three directories is $[$etc+$var+$usr]"$ENDCOLOR