Yeah, I don't remember what the Gnome HOWTO said specifically. I haven't read it in a while (since I really really dislike Gnome). That's just usually the case.
df shows the percentage in the standard view, so you can kinda kludge something together if you parse its output with awk and sed (you can probably do everything with one or the other, but this is the easiest way that I know off the top of my head). For instance, if I wanted to return just the percentage free on /dev/sda1, I could use the following (since with df's output, the percentage field is the 5th one):
df /dev/sda1 | awk '{ getline; print $5 }' | sed 's/\%//'
Basically,
df /dev/sda1 returns the disk space free for only the specified drive, as follows:
spectere@obsidian:~$ df /dev/sda1
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 488369088 350973792 137395296 72% /storage
spectere@obsidian:~$
Now, we pipe that output to awk. Awk is a programming language that's designed around parsing text. We're basically feeding what df is giving us into awk's input. Let's look at the awk script, line-by-line:
getline
print $5
getline just returns the first line and drops it from the input. This is my way of getting rid of df's header.
print $5 simply returns the fifth field of the line. Since awk seperates fields by spaces (like df does) it returns the percentage, as follows:
spectere@obsidian:~$ df /dev/sda1 | awk '{ getline; print $5 }'
72%
spectere@obsidian:~$
Since we want just the number, we'll use sed to substitute the percentage for black space with the following script:
sed 's/\%//'. This takes what is given to sed (the output of awk, which is the processed output from df) and replaces any percentage sign that it sees with nothingness. If you were to have a string that said "hello%world", this script would give you "helloworld". The end result of this is pretty obvious:
spectere@obsidian:~$ df /dev/sda1 | awk '{ getline; print $5 }' | sed 's/\%//'
72
spectere@obsidian:~$
Looks like the shared storage drive on my server is 72% full. Oh snap. Now, let's cook up a quick bash script to use the output from our little one-liner and put it to use. I'll start off by simply showing what I came up with:
#!/bin/bash
export percent=`df /dev/sda1 | awk '{ getline; print $5 }' | sed 's/\%//'`
if [ "$percent" -gt "80" ]; then
echo "omg ur disks r filing up :o";
else
echo "kk looks liek ur gud 4 nao";
fi;
Save that to a file (I called it
zomgdisk.sh) and make it executable with
chmod u+x zomgdisk.sh.
The first line tells the interpreter what to use to parse the script. Since we definitely want bash to handle it, we tell it to use
/bin/bash. This is optional, but recommended. If this is not included, the active shell will interpret the script, which might not be what you want.
The next line should look mostly familiar. In short, it's setting the output of our one-liner to the
percent variable. The backticks (``) execute the command contained within and pastes its output directly onto the line. Essentially, in the context of the script, when it's run on my server, it's seeing this:
export percent=72
Next, we go down to a fairly straight-forward if statement. If our variable is greater than 80, it will display the "omg ur disks r filing up" message, otherwise it'll tell us that it "looks liek ur gud". Note the square brackets for the condition and the odd semicolon usage. I assure you, that will bite you on the ass repeatedly in the future until you've written several scripts from scratch (and after you've set aside the black arts of bash scripting for a few months and jump back to it, it'll trip you up again...grr).
Let's give it a whirl, shall we? Since my server's SATA drive is only 72% full, we should see the second message.
spectere@obsidian:~$ ./zomgdisk.sh
kk looks liek ur gud 4 nao
spectere@obsidian:~$
Perfect! Now, to make sure that our conditions are working right, let's set the warning percentage to 70%:
if [ "$percent" -gt "70" ]; then
Okay, let's give this another go:
spectere@obsidian:~$ ./zomgdisk.sh
omg ur disks r filing up :o
spectere@obsidian:~$
oh noes!!!!!1
So, there you have it. A script that will happily warn you when your drive is getting full.
As you can see, no complex regex tomfoolery was required.