Click to See Complete Forum and Search --> : bash - getting portions of a program output


KarrottoP
06-15-2005, 09:44 AM
I will try to explain this as best as possible, but given that google and my trusty O'Reilly bash book aren't turning up answers for me I though I would lean on the forum a bit.

I need to write a script that checks the percentage of disk space used and reports it back to be worked with in the script. here is what I think I need to happen:


df /dev/sdb1 returns:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sdb1 139817392 36 132715024 1% /mnt/vault

Somehow I need to grab the percentage, however this could be either 1 digit or 2 (if it is three then my script is too late anyway)

I then want that number (in this case 1) to be workable in the script so I can evaluate it further.... What I am going to do is have the script email me when the disk capacity is at 85% and then email me and stop samba when it reaches 95%

I am fairly certain that I can do all of the evaluation stuff, I just though I would write my intentions in case that I am off the mark somehow.

Thanks,
Jason Self

ph34r
06-15-2005, 09:49 AM
df /dev/sdb1 | tail -1 | cut -f 5 -d " "

KarrottoP
06-15-2005, 01:09 PM
Interestingly enough it ended up being something like 22 fields over from that output, but what you said worked perfectly, thanks

One other question the result is lets say "1%" is there a way I can strip away the % and make it equal just "1" I know I can do something simular with a language I do know (lisp) but I have no clue in bash.

ph34r
06-15-2005, 01:22 PM
df /dev/sdb1 | tail -1 | cut -f 5 -d " " | cut -f 1 -d "%"


Sorry about hte 5th field vs. 20somethingth field thing... I'm guessing it had extra spaces that the web doesn't display.

KarrottoP
06-15-2005, 01:36 PM
Ok, I see what you did, I didn't even think about usinig the cut command in that way... And you are exactly right, The spaces were because of that but it was easy to fix...thanks.

cybertron
06-15-2005, 05:29 PM
Just as a sidenote: newer versions of tail might be complaining about -1 being a deprecated option and telling you to use -n 1 instead. Not sure whether that would account for the discrepancy or not though. If it's working I guess it doesn't matter.:)

bwkaz
06-15-2005, 07:51 PM
Or, your distro may have patched tail so that it doesn't fail when you give it an argument of -1. However, even if this is the case, tail has supported the -n 1 syntax for approximately 12 years now (POSIX from 1993 said it was an option, and POSIX from 2001 said -1 was illegal), so there's no excuse to use the option that's technically illegal.

We're already saddled with 20-some years of scripts that use the now-illegal option; don't add to them! ;)

Anyway, on the actual issue. cut interprets each instance of the field-separator character as an individual field separator. It won't condense multiple spaces (or tabs + spaces) into a single field separator; if you have 10 spaces, you'll get 11 fields. If you have 10 spaces in a row, you'll get 9 empty fields, plus whatever's before the first space, plus whatever's after the last space.

To fix this, you might consider using awk:

df /dev/sdb1 | tail -n 1 | awk '{print $5;}' | cut -d'%' -f1

joodas
06-17-2005, 09:32 AM
You can equally well use sed and make it replace the whole line with the digits which were followed by % character:
df /dev/sdb1 |sed -e 's/.* \([[:digit:]]\+\)% .*/\1/' |tail -n 1

Please, note that df may sometimes give you a multiline record for each mountpoint:
Filesystem Size Used Avail Use% Mounted on
/dev/ide/host0/bus0/target0/lun0/part5
34G 8.7G 24G 28% /
/dev/ide/host0/bus0/target0/lun0/part1
79G 24G 55G 31% /mnt/windows
...so I suggest using -P command line option to enable POSIX single-line output format. However, the given sed pipeline works in either case.

Icarus
06-17-2005, 11:31 AM
I always get a kick out of how overly complicated sed can get for simple things like this :D

If you are looking to grab the data on a certain drive, grep makes it easier and awk for the feild you want
df -h|grep /dev/hda6| awk -F' ' '{print $5}'
and if the device file causes a problem for whatever reason you can also grep for the mount point ;)