Tille - I can see you, read those man pages!   Tille's Site

Solutions for the disk usage report exercise

Commands needed: date, echo, df, touch.

The script:

#!/bin/bash
# This is a simple script reporting disk usage on /home
touch /var/tmp/du-report
REPORT="/var/tmp/du-report"
date > $REPORT
echo "Disk usage on /home:" >> $REPORT
df -h /home >> $REPORT
cat $REPORT | mail -s "Report for `date`" boss@work.com

First we create a file for later reference, after that we assign the name of this file to a variable. We put the date in it, then a line of comment using an echo command. Starting from this second command we want to add to the file, we use the >> sign in order to append to this file instead of overwriting it. Last, the report is send to the recepient. The mail gets an appropriate subject, demonstrating that single quotes retain their meaning when inside double quotes: date will be replaced with the actual time and date.

Our work is not finished yet. Now the script has to be moved to the correct place and permissions have to be set:

myprompt> mv du-report.sh ~/bin
myprompt> chmod u+x du-report.sh

Now, set the PATH variable so that you don't have to type the entire path in order to execute the script:

myprompt> export PATH=$PATH:~/bin

Preferable, add this line to your ~/.bashrc file.

Test your result entering the command du-report.sh.

Home
© 1995-2010 Machtelt Garrels - tille - Powered by vIm - Best viewed with your eyes - Validated by W3C - Last update 20100511