简体   繁体   English

如何捕获 df -h --total - Bash 的 output

[英]How to capture output of df -h --total - Bash

I am writing a script which will be used to gather information on the available and used space on different partitions across servers.我正在编写一个脚本,该脚本将用于收集有关跨服务器不同分区上可用和已用空间的信息。 I need to be able to capture the output as a variable.我需要能够将 output 作为变量捕获。

For example, if the output looked like:例如,如果 output 看起来像:

Filesystem                          Size  Used Avail Use% Mounted on
devtmpfs                            2.9G     0  2.9G   0% /dev
tmpfs                               2.9G  4.0K  2.9G   1% /dev/shm
tmpfs                               2.9G  488K  2.9G   1% /run
tmpfs                               2.9G     0  2.9G   0% /sys/fs/cgroup
/dev/mapper/vg_os-lv_root           3.9G  1.6G  2.1G  44% /

How could I capture 2nd row Used, Avail and Mounted on as variables?我如何将第二行 Used、Avail 和 Mounted on 作为变量捕获?

I use a general way for things like that: for capturing a line, I use grep in case of a keyword, or a combination of head and tail in case of a line number.我对这样的事情使用一般的方法:为了捕获一行,我在关键字的情况下使用grep ,在行号的情况下使用headtail的组合。 Then, I use awk for getting a certain column.然后,我使用awk来获取某个列。

In this case, you get something like:在这种情况下,你会得到类似的东西:

var_used = $(df -hk | grep "/dev/shm" | awk '{print $3}')
var_avail = $(df -hk | grep "/dev/shm" | awk '{print $4}')
var_mounted = $(df -hk | grep "/dev/shm" | awk '{print $6}')

In case you're interested in the "/dev" one, you need to grep on /dev$ (the dollar sign stands for "end-of-line").如果您对“/dev”感兴趣,您需要在/dev$上输入 grep(美元符号代表“行尾”)。

For your information: I've mentioned the head and tail usage, but I don't show exactly how it works, for the simple reason: you seem to be interested in one particular line (like the "/dev" one), which currently is at the first line of your output.供您参考:我已经提到了headtail的用法,但我没有确切说明它是如何工作的,原因很简单:您似乎对某一特定行感兴趣(如“/dev”行),它目前在您的 output 的第一行。 If, for any reason, this line number changes, you might need to rework your script, but using the "grep" approach this problem won't occur.如果出于任何原因,此行号发生更改,您可能需要重新编写脚本,但使用“grep”方法不会出现此问题。

First i'd suggest to use special options for df to get only needed fields:首先,我建议使用df的特殊选项来仅获取需要的字段:

$ df -h --output=avail,used,target
Avail  Used Mounted on
 7.3G     0 /dev
 1.5G  3.2M /run
 7.0G   21G /
 7.2G  188M /dev/shm
 5.0M  4.0K /run/lock
 7.4G     0 /sys/fs/cgroup
  61G  189G /home
 1.5G   16K /run/user/125
 1.5G   60K /run/user/1000

And then use readarray ( mapfile ) to store data into an array:然后使用readarray ( mapfile ) 将数据存储到数组中:

readarray -t -s1 arr <<< $(df -h --output=avail,used,target)

Readarray options:读取数组选项:

 -s count Discard the first COUNT lines read -t Remove a trailing DELIM from each line read (default newline)

Which can be accessed like this:可以这样访问:

$ echo ${arr[0]}
7.3G 0 /dev

Split into vars:拆分为变量:

read avail used mount <<< ${arr[0]}
$ echo $avail $used $mount
7.3G 0 /dev

You can use sed or alternatively head and tail to get the second line, or if you know the mount point, grep .您可以使用sedheadtail来获得第二行,或者如果您知道安装点, grep

To parse the line, you can use awk and you split on one or more spaces and print the elements that you want out of this:要解析该行,您可以使用awk并拆分一个或多个空格并打印您想要的元素:

df -h --total | sed -n '2 p' | awk -F "[[:space:]]+" '{ print $3 " " $4 " " $6 }'

To assign this to a variable, do:要将其分配给变量,请执行以下操作:

stats=$(df -h --total | sed -n '2 p' | awk -F "[[:space:]]+" '{ print $3 " " $4 " " $6 }')

[EDIT]: Following the suggestion in the comments: [编辑]:按照评论中的建议:

df -h --total | awk -F "[[:space:]]+" 'NR==2{print $3 " " $4 " " $6 }'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM