我正在尝试编写一些PHP报告function的shell脚本。 所以我开始使用diskusage报告
我想要以下格式
drive path ------------total-size --------free-space
没有其他的
我的脚本是
$output = shell_exec('df -h -T'); echo "<pre>$output</pre>";
其输出如下所示
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
我怎样才能将输出转换成我的forn \ matted输出
像这样的东西:
// 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>";
无论如何都应该让你去
为什么不使用PHP的disk_total_space()
和disk_free_space()
函数呢? 否则,一种方法是使用preg_函数将$输出解析为多维数组,将'g','k','m'字符转换为数字,然后将列总和。 或者,如果列空间是单个制表符或一组空格,则可以将每行分解为多维数组。