SHELL脚本编程----ps分析-统计VSZ,RSS各自总和

描述

假设命令运行的结果我们存储在nowcoder.txt里,格式如下:

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

root 1 0.0 0.1 37344 4604 ? Ss 2020 2:13 /sbin/init

root 231 0.0 1.5 166576 62740 ? Ss 2020 15:15 /lib/systemd/systemd-journald

root 237 0.0 0.0 0 0 ? S< 2020 2:06 kworker/0:1H

root 259 0.0 0.0 45004 3416 ? Ss 2020 0:25 /lib/systemd/systemd-udevd

root 476 0.0 0.0 0 0 ? S< 2020 0:00 edac-poller

root 588 0.0 0.0 276244 2072 ? Ssl 2020 9:49 /usr/lib/accountsservice/accounts-daemon

message+ 592 0.0 0.0 42904 3032 ? Ss 2020 0:01 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation

root 636 0.0 0.0 65532 3200 ? Ss 2020 1:51 /usr/sbin/sshd -D

daemon 637 0.0 0.0 26044 2076 ? Ss 2020 0:00 /usr/sbin/atd -f

root 639 0.0 0.0 29476 2696 ? Ss 2020 3:29 /usr/sbin/cron -f

root 643 0.0 0.0 20748 1992 ? Ss 2020 0:26 /lib/systemd/systemd-logind

syslog 645 0.0 0.0 260636 3024 ? Ssl 2020 3:17 /usr/sbin/rsyslogd -n

root 686 0.0 0.0 773124 2836 ? Ssl 2020 26:45 /usr/sbin/nscd

root 690 0.0 0.0 19472 252 ? Ss 2020 14:39 /usr/sbin/irqbalance --pid=/var/run/irqbalance.pid

ntp 692 0.0 0.0 98204 776 ? Ss 2020 25:18 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 108:114

uuidd 767 0.0 0.0 28624 192 ? Ss 2020 0:00 /usr/sbin/uuidd --socket-activation

root 793 0.0 0.0 128812 3148 ? Ss 2020 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;

www-data 794 0.0 0.2 133376 9120 ? S 2020 630:57 nginx: worker process

www-data 795 0.0 0.2 133208 8968 ? S 2020 633:02 nginx: worker process

www-data 796 0.0 0.2 133216 9120 ? S 2020 634:24 nginx: worker process

www-data 797 0.0 0.2 133228 9148 ? S 2020 632:56 nginx: worker process

web 955 0.0 0.0 36856 2112 ? Ss 2020 0:00 /lib/systemd/systemd --user

web 956 0.0 0.0 67456 1684 ? S 2020 0:00 (sd-pam)

root 1354 0.0 0.0 8172 440 tty1 Ss+ 2020 0:00 /sbin/agetty --noclear tty1 linux

root 1355 0.0 0.0 7988 344 ttyS0 Ss+ 2020 0:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220

root 2513 0.0 0.0 0 0 ? S 13:07 0:00 kworker/u4:1

root 2587 0.0 0.0 0 0 ? S 13:13 0:00 kworker/u4:2

root 2642 0.0 0.0 0 0 ? S 13:17 0:00 kworker/1:0

root 2679 0.0 0.0 0 0 ? S 13:19 0:00 kworker/u4:0

root 2735 0.0 0.1 102256 7252 ? Ss 13:24 0:00 sshd: web priv

web 2752 0.0 0.0 102256 3452 ? R 13:24 0:00 sshd: web@pts/0

web 2753 0.5 0.1 14716 4708 pts/0 Ss 13:24 0:00 -bash

web 2767 0.0 0.0 29596 1456 pts/0 R+ 13:24 0:00 ps aux

root 10634 0.0 0.0 0 0 ? S Nov16 0:00 kworker/0:0

root 16585 0.0 0.0 0 0 ? S< 2020 0:00 bioset

root 19526 0.0 0.0 0 0 ? S Nov16 0:00 kworker/1:1

root 28460 0.0 0.0 0 0 ? S Nov15 0:03 kworker/0:2

root 30685 0.0 0.0 36644 2760 ? Ss 2020 0:00 /lib/systemd/systemd --user

root 30692 0.0 0.0 67224 1664 ? S 2020 0:00 (sd-pam)

root 32689 0.0 0.0 47740 2100 ? Ss 2020 0:00 /usr/local/ilogtail/ilogtail

root 32691 0.2 0.5 256144 23708 ? Sl 2020 1151:31 /usr/local/ilogtail/ilogtail

现在需要你统计VSZ,RSS各自的总和(以M兆为统计),输出格式如下

MEM TOTAL

VSZ_SUM:3250.8M,RSS_SUM:179.777M

具体代码实现:

bash 复制代码
#!/bin/bash

awk '{vsz+=$5;rss+=$6} END{print("MEM TOTAL\nVSZ_SUM:" vsz/1024 "M,RSS_SUM:" rss/1024 "M")}' nowcoder.txt

解题思路:

1.使用awk取出第五个域和第六个域的值,做累加

2.按题目要求输出累加的结果(题目要求以M兆为统计,记得累加结果除1024)

相关推荐
大树889 小时前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠9 小时前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质10 小时前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush410 小时前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行52010 小时前
Linux 11 动态监控指令top
linux
小宇宙Zz10 小时前
Maven依赖冲突
java·服务器·maven
Inhand陈工11 小时前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
网络研究院11 小时前
2026年网络安全
网络·安全·法律·法规·趋势·发展
酣大智11 小时前
ARP代理--工作原理
运维·网络·arp·arp代理
treesforest11 小时前
AI安全系统如何识别异常访问?IP风险识别正在成为关键能力
网络·人工智能·tcp/ip·安全·web安全