Recently I have been doing quite a few more system administrator tasks around different office. I wrote this bash script (I realise it is probably not perfect to bash officianados, however, it does solve that problem). I have attached the completed file below if you want to download it.

Bash Code

#!/bin/bash

# get all users whose uid is greater than 1000 and less than 60000

for user in `awk -F':' '{if ($3 > 1000 && $3 < 60000) print $1}' /etc/passwd`
do

# create array as output from quota command

array=( $(quota -v $user))

# retrieve amount of space user is using

block=${array[17]}

#retrieve quota that user is allowed

quota=${array[18]}

# result = quota - amount used

result=$(($quota-$block))

# if result is less than 20000 then do something. The extra part is in as users with unlimited quota are given a quot with 0 therefore will have a negative number.

if [ $result -lt 20000 ] && [ $result -gt 0 ]
then

# email the user and administrator to make them aware

echo "Sending email regard $user"
(echo "From: [email protected]"
echo "To: $user@server.com"
echo "Subject: Your email box is nearly full"
echo "Hello,
This is your email server speaking. You are running low on email space on the server. Please can you remove any unwanted emails including emails in your Sent and Deleted box.
Thank you,
Your Server"
)| sendmail -t
(echo "From: [email protected]"
echo "To: [email protected]"
echo "Subject: $user is running low on space"
echo "This is Captain Kirk. $user is running low on space. Do something captain."
)| sendmail -t

# finish off the if statement and script

fi

Done. :-)