I have a script whose content is mostly like this:

#!/bin/bash
......
.......
(
sqlplus -s <<EOF
$USER/$PASS
@sql_script1.sql
EOF
) 2>&1 | tee -a $LOGFILE | awk '/^ORA-[0-9]/ || /^SP2-[0-9]/ || /^TNS-[0-9]/ { match ($0, "^...-[0-9]*") ; print substr ($0, RSTART, RLENGTH) ; }' | while read oraerr
do
case $oraerr in
ORA-01017)
echo "\n I cannot connet: " 2>&1 |tee -a $LOGFILE
exit 1
;;
*)
echo "\n There are some errors: ($oraerr) " 2>&1 |tee -a $LOGFILE
exit 1
;;
esac
done

........................
........................

(
sqlplus -s <<EOF
$USER/$PASS
@${PATHSQL}/sql_script2
EOF
) 2>&1 | tee -a $LOGFILE | awk '/^ORA-[0-9]/ || /^SP2-[0-9]/ || /^TNS-[0-9]/ { match ($0, "^...-[0-9]*") ; print substr ($0, RSTART, RLENGTH) ; }' | while read oraerr
do
case $oraerr in
ORA-01017)
echo "\n I cannot connet: " 2>&1 |tee -a $LOGFILE
exit 1
;;
*)
echo "\n There are some errors: ($oraerr) " 2>&1 |tee -a $LOGFILE
exit 1
;;
esac
done


How can I put the sqlerror manipulation in function so i need to write it several times inside the script but I just call the function that manage the error.


Thank you in advance,
ducasio