文本处理、正则表达式及shell脚本之练习

2021/9/27 7:11:18

本文主要是介绍文本处理、正则表达式及shell脚本之练习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来

# 方式一
[22:02:57 32 root@CentOS8 data]# cat /etc/passwd |grep -v  '/sbin/nologin$'
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
admin1:x:1000:1000:admin1:/home/admin1:/bin/bash
apache:x:80:80:apache:/var/www:/bin/bash

# 显示默认shell为非/sbin/nologin的用户个数
[22:03:10 33 root@CentOS8 data]# cat /etc/passwd |grep -vc  '/sbin/nologin$'
6

# 显示默认shell为非/sbin/nologin的用户
[22:03:23 34 root@CentOS8 data]# cat /etc/passwd |grep -v  '/sbin/nologin$'|cut -d: -f 1
root
sync
shutdown
halt
admin1
apache
# 方式二
# 显示默认shell为非/sbin/nologin的用户个数
[22:14:38 35 root@CentOS8 data]# cat /etc/passwd |grep -v  '/sbin/nologin$'|wc -l
6

# 显示默认shell为非/sbin/nologin的用户
[22:17:05 36 root@CentOS8 data]# cat /etc/passwd |grep -v  '/sbin/nologin$'|cut -d: -f 1
root
sync
shutdown
halt
admin1
apache

2、查出用户UID最大值的用户名、UID及shell类型

[22:20:16 38 root@CentOS8 data]# cat /etc/passwd|sort -rnt: -k3
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
admin1:x:1000:1000:admin1:/home/admin1:/bin/bash
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
sssd:x:996:993:User for sssd:/:/sbin/nologin
rngd:x:995:992:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
apache:x:80:80:apache:/var/www:/bin/bash
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash

# 显示用户UID最大值的用户名、UID及shell类型
[22:20:48 39 root@CentOS8 data]# cat /etc/passwd|sort -rnt: -k3|head -1 |cut -d: -f 1,3,7
nobody:65534:/sbin/nologin

3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

[22:32:25 56 root@CentOS8 data]# ss -nt|tail -n +2|tr -s " " :|cut -d: -f 6|sort |uniq -c|sort -rn
      3 10.0.0.1
      1 10.0.0.167
      1 10.0.0.162
[22:37:43 68 root@CentOS8 data]# ss -nt|grep -v "^State"|sed -rn "s/.*[[:space:]](.*)\:.*/\1/p"|sort|uniq -c|sort -rn
      3 10.0.0.1
      1 10.0.0.167
      1 10.0.0.162

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

[root@CentOS8 data]# cat disk.sh 
#! /bin/bash

RED="\e[1;31m"
BLUE="\e[1;34m"
END="\e[0m"
#MAX_SPACE=`df |tail -n +2|tr -s " "|cut -d" " -f 5|sort -rn |head -1`
MAX_SPACE=`df |tail -n +2|grep -Eo '(100|[0-9]{1,2})%'|sort -rn |head -1`


echo -e "$BLUE********************Show Info*********************$END"
printf "\e[1;30m%10s:$RED%s\n$END" "Maximum partition utilization" "$MAX_SPACE"

[root@CentOS8 data]# bash disk.sh 
********************Show Info*********************
Maximum partition utilization:17%

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

[root@CentOS8 data]# cat systeminfo.sh 
#! /bin/bash


BLUE="\e[1;34m"
END="\e[0m"
HOST_NAME=`hostname`
#IP=`ifconfig ens33|tail -n +2|head -1|tr -s " "|cut -d " " -f 3`
IP=`ifconfig ens33 |egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}'|head -1`
OS=`lsb_release -a|grep '^Description'|cut -d: -f 2|sed -rn 's/[[:space:]]+(.*)/\1/p'`
KERNEL_INFO=`uname -r`
CPU_INFO=`lscpu|grep '^Model name'|tr -s " " |cut -d: -f 2|sed -rn 's/[[:space:]](.*)/\1/p'`
MEM_SIZE=`free -h|grep 'Mem'|tr -s " "|cut -d " " -f 2`
DISK_SIZE=`lsblk|grep '^sd'|tr -s " " |cut -d " " -f 4`


echo -e "$BLUE*******************System Info Show***************$END"
printf "%-21s:%s\n" "The host name is" "$HOST_NAME"
printf "%-21s:%s\n" "The Host IP is" "$IP"
printf "%-21s:%s\n" "The Host OS is" "$OS"
printf "%-21s:%s\n" "The Host Kernel is" "$KERNEL_INFO"
printf "%-21s:%s\n" "The Host CPU is" "$CPU_INFO"
printf "%-21s:%s\n" "The Host Memory is" "$MEM_SIZE"
printf "%-21s:%s\n" "The Host Disk Size is" "$DISK_SIZE"
[root@CentOS8 data]# bash systeminfo.sh 
*******************System Info Show***************
The host name is     :CentOS8.db-node1-test
The Host IP is       :10.0.0.152
The Host OS is       :CentOS Linux release 8.3.2011
The Host Kernel is   :4.18.0-240.el8.x86_64
The Host CPU is      :Intel(R) Core(TM) i5-3320M CPU @ 2.60GHz
The Host Memory is   :941Mi
The Host Disk Size is:200G



这篇关于文本处理、正则表达式及shell脚本之练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程