From 26b68d44561531e044d354078c61a3fa072dba37 Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Sat, 28 Dec 2013 20:36:34 +0100 Subject: [PATCH] Exercise 4 on lab 8 done --- Labb8/ovning4.sh | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 Labb8/ovning4.sh diff --git a/Labb8/ovning4.sh b/Labb8/ovning4.sh new file mode 100755 index 0000000..e88290e --- /dev/null +++ b/Labb8/ovning4.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +# Jack-Benny Persson +# LX13 +# Exercise 4 on lab 8 (Write a script that sends a report to the sysadmin +# about the 5 biggest homefolders on the system + +# Binaries +Sed="/bin/sed" +Awk="/usr/bin/awk" +Du="/usr/bin/du" +Mktemp="/bin/mktemp" +Mail="/usr/bin/mail" +Printf="/usr/bin/printf" +Rm="/bin/rm" + +# Variables +Admin="jake" + +# Sanity checks +for Bin in $Sed $Awk $Du $Mktemp $Mail $Printf $Rm; do + if [ ! -x $Bin ]; then + echo "Can't execute $Bin" + exit 1 + fi +done + +if [ ! -w /tmp/ ]; then + echo "Can't create temporary files, aborting" + exit 1 +fi + + +# Main + +# Create a temporary file +File=`$Mktemp -t homeusage.XXXXXX` + +# Redirect all output to the temporary file +exec 1> $File + +# Create the mail content +$Printf "Home folder usage report (the top 5 users)\n\n" +$Printf "Usage \tUser\n" +$Printf "----- \t----\n" +$Du -sm /home/* | sort -r | sed -n '1,5p' | awk '{ print $1 " Mbyte \t" $2 }' + +# And finaly send it to it's recipient +$Mail $Admin -s "User usage report" < $File + +# Clean up +$Rm $File + +exit 0