Answer
Piping is really just a degenerate form of bending, we’re told. In this case, it’s just a matter of thinking about “Where can I find this information in human-readable form, and how do I make it machine-readable?”
The answer to the first part is good ol’ friendly df. Ostensibly (why do I use that word so much? Or better yet, why do I have to when talking about the technical aspects of the Mac?), df tells you how much free space is left on your disks. Depending on who you ask, df stands for either “Display Free space” or “Disk Free”. Either way, that’s what it’s for. It has the bonus of providing, line by line, a list of disks that includes their device file and their mountpoint.
$ df
Filesystem 512-blocks Used Avail Capacity Mounted on
/dev/disk0s2 155629664 86278648 68839016 56% /
/dev/disk1s2 155629664 155610132 19532 99% /Volumes/Backup
Everything you need is right there, eh?
The universal UNIX tool to narrow output down on the basis of a line is grep. We want to do that here because we can never be sure what line your information will be on, nor how many lines there will be. If we grep on your known information – the volume name – we get a single line with that volume listed.
$ df | grep Backup
/dev/disk1s2 155629664 155610132 19532 99% /Volumes/Backup
Lastly, we want to grab just that first term in the output. This is an example of tasks where there’s more than one way to skin a cat or, as they say in PERL, “TMTOWTDI”. Tim-tawdy! Yay! I haven’t used that in conversation in years…
Despite my PERL-whorish reputation, We’re actually going to use AWK here.
$ df | grep Backup | awk '{print $1}'
/dev/disk1s2
Greatly simplified, one of AWK’s functions is to break up any input into variables $1, $2, etc. If we wanted the free space instead, we could use $3 to get it (in 512b blocks, by default; read man df for other optional units).
I’m not going to delve into how to put that data into a variable in your script because it’s quite different in every scripting environment, and you didn’t say what you’re using. If you can’t figure it out in your script, post a followup comment.
You’ll probably want to do something with $1 other than print it. Just a thought.
I believe that instead of : df | grep volname | awk ‘{print $1}’
you can do: df | awk ‘/volname/ {print $1}’
I believe that both do the same thing. The second form is better, IMHO, because it saves a command invocation: less work for the computer to do, and less failure points.
YMMV
JP
excellent. i’ll admit to not being an awk pro (which should be obvious).
Post new comment