Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling In Shell Scripts

Status
Not open for further replies.

gharabed

Programmer
Sep 7, 2001
251
US
Is there any way to do general error handling in shell scripts. I know if you use the $? variable it returns the exit code of the last executed command. However, I don't want to have to check this variable after every line of my script. Can I generally trap errors and send them to error handling portion of the shell script. I'm using bash.
Thanks,
Greg
 
In ksh you can play with set -e and trap 'some stuff' ERR

Hope This Help
PH.
 
Why not try something like

ERR=$!

while $ERR != [123456789]
do
your stuff
ERR=$i
done


--
| Mike Nixon
| Unix Admin
|
----------------------------
 
mrn, because some commands return 126,127,128 ...
 
well do [123456789] -o [123456789][123456789] -o [123456789][123456789][123456789]

Mike

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
mrn, I can't figure out which shell you use that admits such syntax.
IMHO your post may be amended like this:
Code:
ERR=$?
while [ $ERR -eq 0 ]
do
  your stuff
  ERR=$?
done
But if 'your stuff' is a list of many commands, you have to test the return value ($?) after each one, like this:
Code:
[ $? -ne 0 ] && break
and this is'nt that gharabed wants.
This is why I suggested the set -e and trap ERR approach.

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top