Issue
I have made a script in order to get the free space on partitions like root, var, tmp, opt and usr. But I have 3200 systems which are working fine, and another 1186 systems, mostly RHEL (Tikanga and Santiago), and some CentOS derivations which are working weird. But systems like RHEL (Maipo) are working fine.
Also, I cant connect personally to those servers in order to troubleshoot. I just can run the script remotely.
#!/bin/sh
df -h / /var /tmp /opt /usr > /freespace.txt
rootSpace=$(awk 'NR==2 { print $4 }' /freespace.txt)
varSpace=$(awk 'NR==3 { print $4 }' /freespace.txt)
tmpSpace=$(awk 'NR==4 { print $4 }' /freespace.txt)
optSpace=$(awk 'NR==5 { print $4 }' /freespace.txt)
usrSpace=$(awk 'NR==6 { print $4 }' /freespace.txt)
customSpace="root=$rootSpace,var=$varSpace,tmp=$tmpSpace,opt=$optSpace,usr=$usrSpace"
- I expect output like root=890M,var=3.1G,tmp=5.0G,opt=842M,usr=1.4G
- But I receive outputs like root=,var=,tmp=28%,opt=,usr=5%
Why DF its not giving the same outputs in order to sort them with AWK?
Solution
I suggest to use df -hP
if you want to parse df's output.
-P
: use the POSIX output format
This keeps all informations in one row, no matter how long the device name is.
Answered By - Cyrus