您的位置:首页 > 运维架构 > Shell

练习-shell 脚本入门

2020-08-11 08:22 971 查看

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

#!/bin/bash
#********************************************************************
#Author:dawn
#Date:2020-08-09 13:05:39
#FileName:backup.sh
#URL:https://blog.csdn.net/xiao_dan_
#Version:1.0
#Description:The test script
#********************************************************************
HOSTNAME=`hostname`
IPV4=`ifconfig|head -n2|tail -n +2|tr -s ' '|cut -d' ' -f3`
VERSION=`cat /etc/redhat-release`
KERNEL=`uname -r`
CPU=`lscpu|grep "Model name"|tr -s " "|cut -d: -f2`
MEMORY=`free -h|grep Mem|tr -s ' ' :|cut -d: -f2`
DISK=`lsblk|grep '^sd'|tr -s ' '|cut -d' ' -f4`

BLUE="\033[1;36m"
RED="\033[1;31m"
END="\033[0m"

echo -e "$BLUE---------------Host systeminfo--------------------$END"

echo -e "HOSTNAME:     ${RED}${HOSTNAME}${END}"
echo -e "IPV4:         ${RED}${IPV4}${END}"
echo -e "VERSION:      ${RED}${VERSION}${END}"
echo -e "KERNEL:       ${RED}${KERNEL}${END}"
echo -e "CPU:         ${RED}${CPU}${END}"
echo -e "MEMORY:       ${RED}${MEMORY}${END}"
echo -e "DISK:         ${RED}${DISK}${END}"
echo -e "${BLUE}---------------------------------------------------${END}"

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

#!/bin/bash
#********************************************************************
#Author:dawn
#Date:2020-08-09 13:05:39
#FileName:backup.sh
#URL:https://blog.csdn.net/xiao_dan_
#Version:1.0
#Description:The test script
#********************************************************************
SRC=$1
if [ -z  ${SRC} ];then
echo -e "\033[1;31m未输入要复制的目录或文件\033[0m"
elif [[ $1 =~ /$ ]];then
DATE_DIR="${SRC%/*}`date +%F`/"
else
DATE_DIR="${SRC}`date +%F`/"
fi

DIR="/data/backup${DATE_DIR}"
echo -e "\033[1;32m*******************Begining mv*******************\033[0m"
cp -rvf ${SRC} ${DIR}
echo -e "\033[1;32m*******************MV Is Finished*******************\033[0m"

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

#!/bin/bash
#********************************************************************
#Author:dawn
#Date:2020-08-09 14:16:50
#FileName:disk.sh
#URL:https://blog.csdn.net/xiao_dan_
#Version:1.0
#Description:The test script
#********************************************************************
DISK=`df|tr -s ' ' '%'|cut -d% -f5|tail -n +2|sort -nr|head -n1`

echo -e "当前硬盘分区空间利用率最大的值是:\033[1;32m${DISK}\033[0m"

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

#!/bin/bash
#********************************************************************
#Author:dawn
#Date:2020-08-09 14:30:25
#FileName:links.sh
#URL:https://blog.csdn.net/xiao_dan_
#Version:1.0
#Description:The test script
#********************************************************************
LINKS=`ss -nt|grep "ESTAB"|tr -s ' ' ':'|cut -d: -f6|uniq -c|sort -nr`

printf "%-10s %-s\n" 连接数 IPv4 ${LINKS}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: