How to handle shell output in php

I am trying to write a PHP script that performs some shell functions, such as reporting. So I start with a discretization report

I want the following format

drive path ------------total-size --------free-space

Nothing else

My script is

$output = shell_exec('df -h -T');
echo "<pre>$output</pre>";

and its output is lower

Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/sda6     ext3     92G  6.6G   81G   8% /
none      devtmpfs    3.9G  216K  3.9G   1% /dev
none         tmpfs    4.0G  176K  4.0G   1% /dev/shm
none         tmpfs    4.0G  1.1M  4.0G   1% /var/run
none         tmpfs    4.0G     0  4.0G   0% /var/lock
none         tmpfs    4.0G     0  4.0G   0% /lib/init/rw
/dev/sdb1     ext3    459G  232G  204G  54% /media/Server
/dev/sdb2  fuseblk    466G  254G  212G  55% /media/BACKUPS
/dev/sda5  fuseblk    738G  243G  495G  33% /media/virtual_machines

How can I convert this output to my forn \ matted output file

+3
source share
2 answers

Something like that:

// Use awk to pull out the columns you actually want
$output = shell_exec('df -h -T | awk \'{print $1 " " $3 " " $5}\'');
// Split the result into an array by lines (removing the final linefeed)
$drives = split("[\r|\n]", trim($output));
// Chuck away the unused first line
array_shift($drives);

echo "<pre>drive path\ttotal-size\tfree-space\n";

foreach($drives as $drive) {
    // Explode the individual lines to get the values
    $values = explode(" ", $drive);
    echo $values[0], "\t", $values[1], "\t", $values[2], "\n";
}

echo "</pre>";

Should you go anyway

+3
source

PHP disk_total_space() disk_free_space() ? $output , preg_, "g", "k", "m" . , , explode() .

+3

Source: https://habr.com/ru/post/1747290/


All Articles