Linux教程

3.linux正则表达式及文本三剑客及shell基础

本文主要是介绍3.linux正则表达式及文本三剑客及shell基础,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

[root@centos7-v1 ~]#grep -v  '/sbin/nologin' /etc/passwd |cut -d: -f1
root
sync
shutdown
halt
test
mage
www
xiaoming
mageia
slackware
user1
user2
user3
[root@centos7-v1 ~]#grep -v  '/sbin/nologin' /etc/passwd |cut -d: -f1 | wc -l
13

 

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

[root@centos7-v1 ~]#getent passwd|sort -n -t: -k3|cut -d: -f1,3,7|tail -1
nfsnobody:65534:/sbin/nologin
[root@centos7-v1 ~]#

 

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

[root@centos7-v1 ~]#netstat -t | grep ':ssh'|tr -s ' '|cut -d ' ' -f5|cut -d: -f1 | uniq -c|sort -rn
      1 192.168.188.1

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

[root@centos7-v1 ~]#df -h 
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               895M     0  895M   0% /dev
tmpfs                  910M     0  910M   0% /dev/shm
tmpfs                  910M   11M  900M   2% /run
tmpfs                  910M     0  910M   0% /sys/fs/cgroup
/dev/sda5               86G  5.8G   81G   7% /
/dev/sdb1              976M  143M  767M  16% /db1
/dev/sda2              100G   36M  100G   1% /data
/dev/mapper/vg0-log   1014M   33M  982M   4% /log
/dev/mapper/vg0-mysql  976M   63M  852M   7% /mysql
/dev/sda1               10G  222M  9.8G   3% /boot
tmpfs                  182M   12K  182M   1% /run/user/42
tmpfs                  182M     0  182M   0% /run/user/0
[root@centos7-v1 ~]#cat disk.sh 
#!/bin/bash
 
BEGIN="\e[1;32m"
 
END="\e[0m"
 
echo -e "${BEGIN}`df |grep '^/dev/sd' |grep -Eo '[0-9]+%'|grep -Eo '[0-9]+'|sort -nr|head -n1`${END}"
[root@centos7-v1 ~]#sh disk.sh 
16
[root@centos7-v1 ~]#

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

[root@centos7-v1 ~]#cat systeminfo.sh 
#!/bin/bash
#********************************************************************
BEGINCOLOR="\e[1;35m"
ENDCOLOR="\e[0m"
 
echo -e "My hostname is ${BEGINCOLOR}`hostname`$ENDCOLOR"
echo -e "IP address is ${BEGINCOLOR}`ifconfig eth0 |grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'|head -n1`$ENDCOLOR"
echo -e "OS version is ${BEGINCOLOR}`cat /etc/redhat-release`$ENDCOLOR"
echo -e "Kernel version is ${BEGINCOLOR}`uname -r`$ENDCOLOR"
echo -e "CPU type is ${BEGINCOLOR}`lscpu|grep "Model name" |cut -d: -f2 |tr -s " "`$ENDCOLOR"
echo -e "Memtotol is ${BEGINCOLOR}`cat /proc/meminfo |head -n1 |grep -Eo '[0-9]+.*'`$ENDCOLOR"
echo -e "Disk space is ${BEGINCOLOR}`lsblk |grep 'sda\>'|grep -Eo '[0-9]+[[:upper:]]'`$ENDCOLOR"
[root@centos7-v1 ~]#sh systeminfo.sh 
My hostname is centos7-v1
IP address is 192.168.188.101
OS version is CentOS Linux release 7.9.2009 (Core)
Kernel version is 3.10.0-1160.el7.x86_64
CPU type is  Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
Memtotol is 1863032 kB
Disk space is 200G
[root@centos7-v1 ~]#

 

6.20分钟内通关vimtutor(可参考https://yyqing.me/post/2017/2017-02-22-vimtutor-chinese-summary)

 

这篇关于3.linux正则表达式及文本三剑客及shell基础的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!