用shell做記錄
#!/bin/bash
#########################################################################
# File Name: 3_demo.sh
# Author: Ammon
# Created Time: Fri 03 Dec 2021 08:33:49 PM CST
# DesC: This is a demo!
#########################################################################
#1.統(tǒng)計(jì)出/etc/passwd文件中其默認(rèn)shell為非/sbin/nologin的用戶個(gè)數(shù),并將用戶都顯示出來
# -v 匹配相反的,-d: 指明分隔符是:,默認(rèn)是tab报辱,-f1 匹配第n個(gè)字段
cat /etc/passwd | grep -v "/sbin/nologin" | cut -d: -f1
#2.查出用戶UID最大值的用戶名吗铐、UID及shell類型
#/etc/passwd格式: 用戶名:口令:用戶標(biāo)識(shí)號(hào):組標(biāo)識(shí)號(hào):注釋性描述:主目錄:登錄Shell
#-f1,3,7 離散的獲取1,3,7的字段 -t: 指明:是分隔符息楔,-k3 第3列, -n 按數(shù)字排序
#tail 查看文件或標(biāo)準(zhǔn)輸入的倒數(shù)行 -1 指定倒數(shù)第一行
#getent passwd | cut -d: -f1,3,7 | sort -t: -k3 -n | tail -1
getent passwd | sort -t: -k3 | tail -1 | cut -d: -f1,3,7
#3.統(tǒng)計(jì)當(dāng)前連接本機(jī)的每個(gè)遠(yuǎn)程主機(jī)IP的連接數(shù),并按從大到小排序
#w -h 顯示奴前登陸系統(tǒng)的用戶信息重斑,tr -s 縮減連續(xù)重復(fù)的字符成指定的單個(gè)字符
w -h | tr -s " " | cut -d" " -f3 | sort -rn
#4.編寫腳本disk.sh兵睛,顯示當(dāng)前硬盤分區(qū)中空間利用率最大的值
df | tr -s " " | cut -d" " -f5 | sort -rn | head -1
#5.編寫腳本 systeminfo.sh,顯示當(dāng)前主機(jī)系統(tǒng)信息窥浪,包括:主機(jī)名祖很,
#IPv4地址,操作系統(tǒng)版本漾脂,內(nèi)核版本假颇,CPU型號(hào),內(nèi)存大小骨稿,硬盤大小
COLOR_BEGIN="\E[1;35m"
COLOR_END="\E[0m"
echo -e "${COLOR_BEGIN}---------------------------------------${COLOR_END}"
echo -e "${COLOR_BEGIN}"主機(jī)名:"`hostname` ${COLOR_END}"
echo -e "${COLOR_BEGIN}"IPv4地址:"`` ${COLOR_END}"
echo -e "${COLOR_BEGIN}"操作系統(tǒng)版本:"`cat /etc/redhat-release` ${COLOR_END}"
echo -e "${COLOR_BEGIN}"內(nèi)核版本:"`uname -r` ${COLOR_END}"
#uniq 檢查文本文件中重復(fù)出現(xiàn)的行列 -c 顯示重復(fù)的次數(shù)
echo -e "${COLOR_BEGIN}"CPU型號(hào):"`lscpu | grep "Model name" | tr -s " " | cut -d: -f2 | uniq -c` ${COLOR_END}"
echo -e "${COLOR_BEGIN}"內(nèi)存大小:"`free -h | grep "Mem" | tr -s " " | cut -d" " -f2` ${COLOR_END}"
echo -e "${COLOR_BEGIN}"硬盤大小:"`lsblk | grep -w "sda" | tr -s " " | cut -d" " -f4` ${COLOR_END}" #-w 精確匹配
echo -e "${COLOR_BEGIN}---------------------------------------${COLOR_END}"
執(zhí)行結(jié)果如下
image.png