繁体   English   中英

Perl-在Linux上获得免费的磁盘空间使用情况

[英]Perl - get free disk space usage on linux

我想知道如何从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) 

如果您希望在perl完成所有操作,则:

df -h | perl -e 'while (<stdin>) { if ($. == 2) { @x = split; print $x[3] }}'

这仅使用perl来读取df -h的输出,对于第二条记录( $. == 2 ),它根据空白将记录分为多个字段,并输出字段3(从0开始计数)。

这似乎也可以:

df -h | awk 'NR==2 {print $4}'

获取第二行,品脱第四场。

暂无
暂无

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

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