Assuming that srvctl returns a useful exit status to the calling shell, we may use that in an if statement in the script to output messages depending on whether the database was successfully shut down or not.
#!/bin/bash export ORACLE_SID ORACLE_HOME for ORACLE_SID in TEST{1..4}1 do # The database name is the same as the SID, # but with the last character removed. db=${ORACLE_SID%?} # Get the ORACLE_HOME directory by parsing /etc/oratab. ORACLE_HOME=$( awk -F : '$1 == ENVIRON["ORACLE_SID"] { print $2 }' /etc/oratab ) # Try stopping the service. if "$ORACLE_HOME"/bin/srvctl stop instance -db "$db" -instance "$ORACLE_SID" then # Report success. printf 'DB "%s" on instance "%s" stopped\n' "$db" "$ORACLE_SID" else # Report failure, log to file. printf 'Failed stopping DB "%s" on instance "%s"\n' "$db" "$ORACLE_SID" | tee -a error.log >&2 fi done
The script above re-implements most of your own script in a portable manner, removes unnecessary calls to external utilities, does not use more variables than needed, and adds an if statement that either outputs a message about successfully stopping the service, or about failing to stopping the service. If the service is not stopped, the message about this is additionally added to a log file called error.log in the current directory.
A variant of the above script that avoids reading the whole of /etc/oratab in every iteration and instead parses the file in the loop, tests whether we've found an interesting ORACLE_SID, and otherwise skips to the next iteration. The if statement is identical to the first script.
#!/bin/bash export ORACLE_SID ORACLE_HOME while IFS=: read -r ORACLE_SID ORACLE_HOME do # Skip uninteresting SIDs. [[ $ORACLE_SID != TEST[1-4]1 ]] && continue db=${ORACLE_SID%?} if "$ORACLE_HOME"/bin/srvctl stop instance -db "$db" -instance "$ORACLE_SID" then printf 'DB "%s" on instance "%s" stopped\n' "$db" "$ORACLE_SID" else printf 'Failed stopping DB "%s" on instance "%s"\n' "$db" "$ORACLE_SID" | tee -a error.log >&2 fi done </etc/oratab
You could delete all "bash-isms" from the above script by changing the [[ $ORACLE_SID != TEST[1-4]1 ]] test to
case $ORACLE_SID in (TEST[1-4]1) : ;; (*) continue; esac