LINUX--正则表达式&grep&shell基础
1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来
lime@ubuntu1804:~$ grep -v '/sbin/nologin' /etc/passwd | cut -d: -f1
root
sync
lxd
pollinate
lime
test
mageia
slackware
user1
user2
user3
2、查出用户UID最大值的用户名、UID及shell类型
lime@ubuntu1804:~$ cat /etc/passwd |cut -d: -f1,3,7|sort -nt: -k2 |tac|head -1
nobody:65534:/usr/sbin/nologin
3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
lime@ubuntu1804:~$ ss -nt |grep "^ESTAB" | tr -s ' ' :|cut -d: -f6|uniq -c|sort -nr|head
2 10.0.0.1
4、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值
脚本内容:
echo '分区最大利用率'
df -h|grep '^/dev/sd' |tr -s ' ' %|cut -d% -f5|sort -nr |tail -l|head -1
lime@ubuntu1804:/data$ ./disk.sh
分区最大利用率
17
5、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
lime@ubuntu1804:/data$ cat systeminfo.sh
#!/bin/bash
#
#*********************************
#Aythor: lime
#Date: 2021-03-$d
#*********************************
RED="\E[1;31m"
GREEN="echo -e \E[1;32m"
END="\E[0m"
$GREEN----------------------Host systeminfo--------------------$END
echo -e "HOSTNAME: $RED`hostname`$END"
#echo -e "IPADDR: $RED` ifconfig eth0|grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' |head -n1`$END"
echo -e "IPADDR: $RED` hostname -I`$END"
echo -e "OSVERSION: $RED`cat /etc/redhat-release`$END"
echo -e "KERNEL: $RED`uname -r`$END"
echo -e "CPU: $RED`lscpu|grep 'Model name'|tr -s ' '|cut -d : -f2`$END"
echo -e "MEMORY: $RED`free -h|grep Mem|tr -s ' ' : |cut -d : -f2`$END"
echo -e "DISK: $RED`lsblk |grep '^sd' |tr -s ' ' |cut -d " " -f4`$END"
$GREEN---------------------------------------------------------$END

浙公网安备 33010602011771号