Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


How do I customize a script from "Wicked Cool Shell Scripts"?

Regarding script #41 (calculating available disk space), out of your book Wicked Cool Shell Scripts, I am unclear how I can have this script calculate the free space on one specific drive, namely /Volumes/Documents HD in my case.

I'd also love to know how I can automatically email the results of this same script, say every 24 hours. If you could kindly point me in the right direction, I'm sure I could figure out the rest!


Dave's Answer:

This is a fairly straightforward script you've asked about, so this will be an easy explanation.

The most important line in the diskspace script is:

df -k | awk -f $tempfile

Let's just run a quick df -k so you can see the output in question:

$ df -k
Filesystem              1K-blocks      Used    Avail Capacity  Mounted on
/dev/disk0s9            156277884  63170972 92850912    40%    /
devfs                         101       101        0   100%    /dev
fdesc                           1         1        0   100%    /dev
<volfs>                       512       512        0   100%    /.vol
/dev/disk1s3            156159792 138235824 17923968    89%    /Volumes/X2
automount -nsl [206]            0         0        0   100%    /Network
automount -fstab [214]          0         0        0   100%    /automount/Servers
automount -static [214]         0         0        0   100%    /automount/static

The important thing to see here is that I have one non-root drive, /Volumes/X2, and that it shows up in the list exactly in that format.

This means that you can weed out other drives by using a call to grep. I'd tally up all drives on /Volume by doing this:

df -k | grep /Volumes | awk -f $tempfile

You could be even more specific, by using your hard disk name too:

df -k | grep /Volumes/Documents\ HD | awk -f $tempfile

Either way, changing that one line should accomplish what you seek.

In terms of automatically emailing the results every 24 hours, that's a job for crontab. Start with man crontab and you'll find that it's not too terribly hard to figure out.

If you would like to learn more about crontab and related administrative features of Unix, I suggest my brand new fourth edition of Teach Yourself Unix in 24 Hours.


More Useful Shell Script Programming Articles:
✔   Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔   Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  








Reader Comments To Date: 1

Howard Hong said, on October 30, 2009 3:28 PM:

Dave,

While it is certainly possible to alter the script to filter the output for a specific filesystem, isn't it better to leave the script as is and just pipe the output to a filter instead? That output can then be piped to a mailer program to send mail. Finally, the whole command can be scheduled using cron. Here's an example crontab that runs the full command at 12:00 every day.

* 12 * * * diskspace | grep -e 'Filesystem' -e '/Volumnes/Documents HD' | mailx -s '/Volumnes/Documents HD Diskspace' user@host

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.