题目:编写shell脚本,显示当前主机系统信息,包括主机名,IP地址,操作系统版本号,内核版本,CPU型号,内存型号,硬盘大小。
代码:
[root@localhost shell]# cat systeminfo.sh
#!/bin/bash
echo "当前主机系统信息如下:"
echo "主机名:$(hostname)"
echo "ipv4地址:$(ifconfig | grep "inet" | awk -F " " '{print $2}' | head -1)"
echo "操作系统版本号:$(cat /etc/redhat-release)"
echo "内核版本:$(uname -r)"
echo "CPU型号:$(lscpu | grep "Model name" | awk -F " " '{print $4,$5,$6,$7,$8}')"
echo "内存型号:$(free -hm | grep "Mem" | awk -F " " '{print $2}')"
echo "硬盘大小:$(fdisk -l /dev/sda | head -2 | tail -1 | awk -F " " '{print $3,$4}' | tr -d ,)"
执行结果:
[root@localhost shell]# bash systeminfo.sh
当前主机系统信息如下:
主机名:localhost.localdomain
ipv4地址:192.168.1.132
操作系统版本号:CentOS Linux release 7.7.1908 (Core)
内核版本:3.10.0-1062.el7.x86_64
CPU型号:Core(TM) i5-7400 CPU @ 3.00GHz
内存型号:1.8G
硬盘大小:21.5 GB