The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #6 (permalink)  
Old 07-16-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
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:


Code:
#!/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.