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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exiting a shell script on error?

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
0
0
US
Hi,
After looking through the Bash man pages, I couldn't see any sort of goto statement like Windows Batch files have. Is there a way of doing:
Code:
if [ something ]; then goto Error; fi

Currently, I'm just doing this on each line of my script, which is kind of annoying:
Code:
./Make.sh clean clobber all  | grep "\*\*\*" ; [COLOR=blue]if [ $? != 0 ]; then echo "Error!  $0 halted."; exit 1 ; fi[/color]

Also, I noticed that the block in blue is useless for exiting when make fails unless I don't pipe to grep. How can I fix that problem and still filter the output through grep?
 


cpjust,

You have all you need right in the code you posted.

Code:
if [ something ]; then
   echo "Error! $0 halted."
   exit 1
else
   Do what you want here.
fi
 
Ways to skin this cat, assuming that by "dos style" you are talking about checking the errorlevel (return status in Linux):

1) if you just want a cleaner script, put your grep and if into a function, in which case you can then say in your script:

checkstars("make etc etc etc")

2) reverse your logic. Use the "&&" operator after your make command, so:

{ make foo && make bar && make baz && ./foobin } || echo "Build failed"

I will post more permutations, if anyone wants, as I think of them.

 
Maby something like this?
Code:
#!/bin/bash
# Test error-handling
# Date: 07.11.07

function errorhandler()
{
echo "The $0 script crashed!"
exit 1
}

if ( ls -l $1 ) 2> /dev/null   # if the command succedes:
then
  echo "This is what's inside the $1 directory."

else               # if the command errors out:
  errorhandler     # goto the error handler
fi

exit 0
 
Well, I meant more like this:

Code:
function errorhandler()
{

if ( $1 )  >  /dev/null 2>&1   # if the command succedes:
then
  echo "Command ran okay"

else               # if the command errors out:
  echo "Ahhhh! time to panic!"
  return 1 # or exit 1 if you want to bomb right out to the shell
fi
}

echo "Making lovenotwar to get an error"
errorhandler "make love not war"

echo "asking make for help to get a good run"
errorhandler "make -h"

exit 0


The point is, as we have all aptly demonstrated, is that there are lots of ways to do this, right?
 
I finally got some time to try out using the function method, and it works great, except for one thing. Here is my function:
Code:
function RunCmd()
{
	echo "=== Running: $1"
	if !( $1 ) | grep "\*\*\*"
	then
		echo "Error!  $SCRIPT_NAME halted."
		popd
		exit 1
	fi
}
If the command I run doesn't have anything with "***" output, the grep fails and causes my script to exit.
Is there a switch or something that tells grep to filter only lines with "***", but if there aren't any of those lines, return 0 instead of an error?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top