Perl - 在Linux上获得可用的磁盘空间使用量

th3*_*0id 1 linux perl awk

我想知道如何从df("/")获得第二行第4列的值.这是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
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用perl获得可用的可用空间(78GB),这是我相当新的.我可以使用以下linux命令获取值,但我听说没有必要在perl中使用awk,因为perl可以做本机的awk.

df -h | tail -n +2 | sed -n '2p' | awk '{ print $4 }'
Run Code Online (Sandbox Code Playgroud)

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

7st*_*tud 6

更简洁一点:

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) 
Run Code Online (Sandbox Code Playgroud)