Example shell script

In the following example, the shell script is designed to process a UNIX tar file. The shell script is written so that it:
  1. Explodes the tar file in response to a COPY action by SMP/E
  2. Deletes all files from the directory in response to a DELETE action by SMP/E.
# This script will either explode a tar file file or delete all files in
# the directory.  The script uses the following environment variables
# for input:
#
#   SMP_Directory - directory in which the tar file resides
#   SMP_File      - name of the tar file
#   SMP_Phase     - indicates whether the shell script is being called
#                   before or after SMP/E has processed the file
#   SMP_Action    - the action that SMP/E is performing: COPY or DELETE
#
echo "Starting script processing…"
status=0                 # initialize script status
#
# Verify that the required input was received by the shell script.
#
if test ! "$SMP_Directory"
then
  echo "** No SMP_Directory parameter specified."
  status=-1
elif test ! "$SMP_File"
then
  echo "** No SMP_File parameter specified."
  status=-1
elif test ! "$SMP_Phase"
then
  echo "** No SMP_Phase parameter specified."
  status=-1
elif test ! "$SMP_Action"
then
  echo "** No SMP_Action parameter specified."
  status=-1
fi
#
# If a parameter error was detected, then exit now.
#
if test $status -ne 0               # If status is not 0, an error was detected
then
  echo "   Ensure the input environment variables for this script have been provided." 
  echo "   If SMP/E was not used to invoke the script, correct the caller to specify "
  echo "   the input environment variables.  If SMP/E invoked this script, contact "
  echo "   the IBM support center."
  echo "Parameter error.  Exiting script with status $status"
  exit $status
fi
#
# If the script is being invoked after the tar file has been copied
# to the directory (SMP/E phase is post-copy), and the desired action
# is COPY, explode the tar file.
#
if test $SMP_Phase = POST
then
  if test $SMP_Action = COPY
  then
    cd $SMP_Directory                # Set the working directory for pax
    echo "Exploding all components of $SMP_Directory$SMP_File using the pax command"
    pax -rvf $SMP_Directory$SMP_File # Explode the tar file
    status=$?                        # Get the status of the pax command
    if test $status -ne 0            # If pax failed, indicate so to the user
    then
      echo "** pax command failure: pax ended with status $status"
    else
      echo "pax command completed successfully."
    fi
    ls -l $SMP_Directory            # List the contents of the directory
  fi
fi
#
# If the action is DELETE, delete all component files.
#
if test $SMP_Action = DELETE
then
  echo "Deleting the following from $SMP_Directory using the rm command:"
  ls -l $SMP_Directory              # List the contents of the directory
  cd $SMP_Directory                 # Set the working directory for rm
  rm *                              # Delete the directory and all files
  status=$?                         # Get the status of the delete
  if test $status -ne 0             # If rm failed, indicate so to the user
  then
    echo "** rm command failure: rm ended with status $status"
  else
    echo "rm command completed successfully."
  fi
  if test $status = 0               # If rm was OK, then…
  then
    mkdir $SMP_Directory            # …recreate the directory, albeit empty
    status=$?                       # Get the status of the mkdir
    if test $status -ne 0           # If mkdir failed, indicate so to the user
   then
     echo "** mkdir command failure: mkdir ended with status $status"
    fi
  fi
fi
echo "Exiting script with status $status"
exit $status