df换行问题解决
df是linux下用来查磁盘空间的命令,而在使用了LVM分区或网络挂载的情况下,再用df取分区的使用率时,发现有些分区显示换行了,这样会导致通过脚本取的数据不对。
[root@ ]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 154G 6.1G 140G 5% / /dev/sda2 29G 1.1G 26G 5% /opt /dev/sda1 190M 12M 169M 7% /boot tmpfs 3.9G 0 3.9G 0% /dev/shm /dev/sdb1 2.5T 113G 2.3T 5% /mail /dev/mapper/vg_01-LogVol00 68G 180M 64G 1% /App 10.185.xxx.xxx:/opt/apacche/conf 59G 218M 56G 1% /opt/apacche/conf
而直接使用awk进行截取数据时,可能就出现下面的情况:
[root@web45 data1]# df -h|awk '{print $1,$5}' 文件系统 已用%% /dev/sda2 3% tmpfs 0% /dev/mapper/vg_01-LogVol00 68G /App /dev/sda1 29% /dev/mapper/vg_01-LogVol02 385G /data1 /dev/mapper/vg_01-LogVol03 401G /data2 /dev/mapper/vg_01-LogVol01 73G /logs
解决该问题的方法也十分简单,在使用df命令时加上参数:-P就可以解决。
-P –portability use the POSIX output format
[root@ ]# df -hP|awk ‘{print $1,$5}’ Filesystem Use% /dev/sda5 5% /dev/sda2 5% /dev/sda1 7% tmpfs 0% /dev/sdb1 5% 10.185.xxx.xxx:/opt/apacche/conf 1%
如果想让输出的结果而美观一些,可以再通column命令配合进行输出:
[root@web45 data1]# df -hlP|awk '{print $1,$5}'|column -t /dev/sda2 3% tmpfs 0% /dev/mapper/vg_01-LogVol00 1% /dev/sda1 29% /dev/mapper/vg_01-LogVol02 32% /dev/mapper/vg_01-LogVol03 1% /dev/mapper/vg_01-LogVol01 1%
参考资料:http://www.361way.com/df/58.html