Why did this command suddently stop working?

Red Squirrel

No Lifer
May 24, 2003
71,313
14,085
126
www.anyf.ca
I got this alert for one of my servers that disk space is -1. Confused, I decided to type the command directly on the server to see why it's returning such as weird value, and now I'm even more confused, because it's returning -869. This is the command:

Code:
df -kh / | tail -1 | awk '{ print 100-$4 }'

df -kh returns this:
Code:
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root  8.5G  7.1G  971M  89% /
tmpfs                         119M     0  119M   0% /dev/shm
/dev/sda1                     485M   33M  427M   8% /boot

The command is suppose to do 100-89 and return the disk space left. It's been working for years, except tonight it randomly stopped. Huh?

Why would it have done this? What's the proper command to get what I want? I don't get why it decided to stop working just now though. Replacing $4 with $5 seems to work... but why? It worked fine before. I can see why it should be 5 because the % is the 5th element... but I'm still confused why it worked before and now it stopped.

OS is CentOS 6.5.
 
Last edited:

Red Squirrel

No Lifer
May 24, 2003
71,313
14,085
126
www.anyf.ca
Well it seems to work now that I changed it to 5, but I don't get why it worked for so long with 4. Is there something that can cause the output of df to add an extra column?
 

KillerBee

Golden Member
Jul 2, 2010
1,750
82
91
Check yum.log for any recent coreutils update?

or maybe you just need to update coreutils to the latest
if not up2date already


This change was from Oct 2014
https://rhn.redhat.com/errata/RHBA-2014-1457.html

* A recent update of the coreutils packages changed the format of the output from the "df" and "df -k" commands to one line per entry, which is required for POSIX mode. As a consequence, scripts relying on the previous two lines per entry format started to fail. To fix this bug, two-line entries have been reintroduced to the output for modes other than POSIX. As a result, scripts relying on the two-line format no longer fail. (BZ#1057026)
 

Red Squirrel

No Lifer
May 24, 2003
71,313
14,085
126
www.anyf.ca
Hmm that explains why it's not consistent between different distros. I don't know why it would have changed on it's own though, does CentOS update automatically? I occasionally go on all my machines and run yum update but have not done it in a while. I probably should now that I think about it. These are not public facing servers so not really that worried either way.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,817
75
This should work more consistently:
Code:
df -kh / | tail -1 | sed -e 's/^.* \([0-9]*\)%.*$/\1/' | awk '{ print 100-$1 }'
There's probably an easier way but I never learned awk.