Using mail command to notify the status of script

Can someone please help me with this script, I'm trying to create system backup on AIX, for this I want to first mount the filesystem if it is not mounted, then create the backup and unmount the filesystem but I'm having problem while using the mail command to notify the status of filesystem whether it can be mounted or not, it doesn't send an email and doesn't append anything to the log files, also how do I calculate the time the script is taking to complete, is there a command I can use within the script ?

#!/usr/bin/ksh
set -x
HOSTNAME=$(hostname)
fs=$(df|grep testfs | awk '{print $7}')
if [ -z "$fs" ]; then
echo "Mounting filesystem..."
mount /testfs
mail -s "Filesystem mounted with success" -c "xxx@email1 yyy@email2"</var/log/succes.err >/dev/null
else
mail -s "No such filesystem exists on $HOSTNAME" -c "xxx@email1 yyy@email2"</var/log/miss.err >/dev/null

exit

fi

## If cannot mount then run,
if [ $? -ne 0 ]; then
mail -s "Cannot mount filesystem for $HOSTNAME" -c "xxx@email1 yyy@email2"</var/log/fail.err >/dev/null
exit
fi

Does the mail command work on its own outside the script?

You could include the date command at the beginning and end of the script, or else use time scriptname to run it.

Incidentally, no need for awk and grep, this will do:

fs=$(df | awk '/testfs/ { print $7}')

Thanks for your quick response, mail command worked from the command line after removing the -c flag but does the script look OK bcos it still doesn't append the log files.
changed the mail command as below,
mail -s "Filesystem mounted with success" "xxx@email1 yyy@email2"</var/log/succes.err >/dev/null

Maybe it should be success.err?

Also, another thing you will need to do is save the value of $? after the mount command, because it will change back to 0 when any subsequent command is run, such as mail for example. e.g. retcode=$? and if [ $retcode -ne 0 ]; then.

Should I save the value of $? after every command like mount, umount and use the code u suggested before each of the mail commands? Can u plz. be more descriptive ? Thanks again.

I would actually rearrange the code so that the test immediately follows the command. Note how you can just include a command in an if statement to test its success or failure:

#!/usr/bin/ksh
set -x
HOSTNAME=$(hostname)
fs=$(df| awk '/testfs/{print $7}')
if [ -z "$fs" ]; then
    echo "Mounting filesystem..."
    if mount /testfs; then
        mail -s "Filesystem mounted with success" "xxx@email1 yyy@email2"</var/log/succes.err >/dev/null
    else
        mail -s "Cannot mount filesystem for $HOSTNAME" "xxx@email1 yyy@email2"</var/log/fail.err >/dev/null
        exit 1
    fi
else
    mail -s "No such filesystem exists on $HOSTNAME" "xxx@email1 yyy@email2"</var/log/miss.err >/dev/null
    exit 2
fi

Also it is a good habit to exit n with an error code when your script is reporting an error.

I still have problem using the mail command within a script, I just want to send an email with the subject only.
On the command line for eg. :
# cat /etc/hosts |mail -s "Succesfully creatd mksysb for $HOSTNAME" "xxx@email1 yyy@email2" -->works

# mail -s "Succesfully creatd mksysb for $HOSTNAME" "xxx@email1 yyy@email2" -->doesn't work

It doesn't work because mail expects to read message contents from stdin.

Just use this to send a blank message:

echo | mail -s "Succesfully creatd mksysb for $HOSTNAME" "xxx@email1 yyy@email2" 

Thanks, u have been of great help to me, hopefully one last question though, what if I want to mount the nfs exported filesystem, can I still use it in an IF statement as suggested above,
for eg.:
if mount <Ip.addr.of nfs server>:/testfs /<mount pt>; then
echo "Success"
else
echo "Failure"
exit 1
fi

No reason why not.