Help with function - is placed in script correctly?

Hi Folks -

I hope everyone is well.

I just need some assistance with a script I've put together. I haven't used functions before so I was wondering if my script is built properly in terms of architecture?

The first command of the script shuts down services. Then, the second part finds a service called ESSSVR and kills that if it's still up.

Is how I have it set up correct?

Thank you!

#:: Begin Script Processing --::#
echo ---------------------------------------------------------
echo "${_SN} beginning at ${_TIME}"                           
echo                                                                                                     
echo "Stop Essbase Services"                                         
echo ---------------------------------------------------------

. /home/essadmin/scripts/shutdown.sh

if [ $? -eq 0 ]
then
  echo ---------------------------------------------------------
  echo "Essbase Services Stopped Successfully!"                 
  echo ---------------------------------------------------------
 
else
  echo -------------------------------------------------------
  echo "Essbase Services Stopped Unsuccessfully!"           
  echo -------------------------------------------------------
  exit 1
  fi
  
echo ---------------------------------------------------------
echo "Terminate all hung ESSSVR Processes"                               
echo ---------------------------------------------------------

function trim {

        arg="$*"
        shopt -s extglob
        arg="${arg#*( )}"
        arg="${arg%*( )}"
        echo "$arg"

}

function kill_soft_then_hard {

        PIDS="$*"

        PID_REGEX_PATTERN="^\s*("
        for pid in $PIDS;do
                PID_REGEX_PATTERN="$PID_REGEX_PATTERN|$pid"
        done
        PID_REGEX_PATTERN="$PID_REGEX_PATTERN)"

        # Here's the chain of signals being sent, when the processes refuse to terminate
        for SIGNAL in 15 1 3 7 9 ;do 
                kill -$SIGNAL $PIDS &>/dev/null
                for((w=1;$w<=30;w++)) ; do
                  if ps ax | grep -qE $PID_REGEX_PATTERN ; then
                          continue
                  else
                          break 2
                  fi
                  sleep 1
                done
        done

}

function kill_by_pattern {

        pattern="$1"
        PIDS="$(ps ax | grep "$pattern" | grep -v grep | awk '{print $1}')"
        PIDS="$(trim $PIDS)"

        [ -n "$PIDS" ] && kill_soft_then_hard $PIDS

}

kill_by_pattern ESSSVR

if [ $? -eq 0 ]
then
  echo ---------------------------------------------------------
  echo "Terminated all hung ESSSVR Processes Successfully!"                           
  echo ---------------------------------------------------------
  #::-- If empty, delete YYYY_MMDD error file subdirectory --::#
  trap "[ -s ${_EF} ] || rm -f ${_EF} ] && rmdir ${_ARC_EP}" EXIT 0
  
else
  echo -------------------------------------------------------
  echo "Terminated all hung ESSSVR Processes Unsuccessfully!"                       
  echo ------------------------------------------------------
  fi
  exit 1

Place seems reasonable to me. Yes.

A question - as a sort of rule of thumb, you usually want to source scripts that define thing like functions, variables, environment variables - you seem to have sourced something which affects the system. Kind of the opposite. Or at least it does important things inside your current code.

A reason to consider: the more external dependencies you have, the more difficult maintenance and debugging become. The sourced script can have all sorts of effects on your code.