gpt4 book ai didi

linux - Perl - 在 Linux 上获取可用磁盘空间使用情况

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:19:58 28 4
gpt4 key购买 nike

我想知道如何从 df ("/") 获取第二行第四列的​​值。这是 df 的输出:

Filesystem      Size  Used Avail Use% Mounted on
rootfs 208G 120G 78G 61% /
fakefs 208G 120G 78G 61% /root
fakefs 1.8T 1.3T 552G 70% /home4/user
fakefs 4.0G 1.3G 2.8G 31% /ramdisk/bin
fakefs 4.0G 1.3G 2.8G 31% /ramdisk/etc
fakefs 4.0G 1.3G 2.8G 31% /ramdisk/php
fakefs 208G 120G 78G 61% /var/lib
fakefs 208G 120G 78G 61% /var/lib/mysql
fakefs 208G 120G 78G 61% /var/log
fakefs 208G 120G 78G 61% /var/spool
fakefs 208G 120G 78G 61% /var/run
fakefs 4.0G 361M 3.7G 9% /var/tmp
fakefs 208G 120G 78G 61% /var/cache/man

我正在尝试使用 perl 获取可用空间 (78GB),这对我来说还是个新手。我可以使用以下 linux 命令获取值,但我听说根本没有必要在 perl 中使用 awk,因为 perl 可以做 awk 本身可以做的事情。

df -h | tail -n +2 | sed -n '2p' | awk '{ print $4 }'

我被难住了。我尝试使用 Filesys::df 模块,但是当我打印出可用的使用百分比时,它会给我一个不同于从命令行运行 df 的值。感谢您的帮助。

最佳答案

更简洁一点:

df -h | perl -wlane 'print $F[3] if $. == 2;'


-w enable warnings
-l add newline to output(and chomps newline from input line)
-a splits the fields on whitespace into the @F array, which you access using the syntax $F[n] (first column is at index position 0)
-n puts the code inside the following loop:

LINE:
while (<>) {
... # code goes here
}
# <> reads lines from STDIN if no filenames are given on the command line

-e execute the string
$. current line number in the file (For the first line, $. is 1)

关于linux - Perl - 在 Linux 上获取可用磁盘空间使用情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28247661/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com