how to handle error in bash scripting in unix

hi,

How do i handle an error if for example, i cannot connect to database in bash scripting in unix.

pls help!
megabyte78Asked:
Who is Participating?
 
ahoffmannConnect With a Mentor Commented:
your-command-producing-error-condition
if [ $? != 0 ]; then
  echo error occured
fi
0
 
megabyte78Author Commented:
just to confirm, can i do something like this?

if [ sqlplus -s /nolog != 0]; then
   echo error occured
fi
0
 
yuzhCommented:
what ahoffmann mean is:

sqlplus -s /nolog
if [ $? -ne 0 ] ; then
  echo error occured
fi
# $? is the return value of your last command

you can do something like the following:

if sqlplus -s /nolog ; then
   echo "ok !"
else
   echo "error occured"
fi


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.