Little Girl's Mostly Linux Blog

ExitOnError

Exit on error

This page was last updated on September 28, 2013.

Here are a couple of ways to get a script to exit on error if a command fails to run.

Integer test

  • This is an integer test, which means it looks for a specific integer. In this example, the zero integer is used. This example will attempt to run COMMAND. Since COMMAND should exit with a value of zero if it succeeds and non-zero if it fails, you can check what the exit value is and use the value to determine what happens next in your script:
  • #!/bin/bash
    COMMAND
    if
    	[ "$?" -ne "0" ];
    then
    	echo "FAILURE MESSAGE";
    	exit 1;
    else
    	echo "SUCCESS MESSAGE";
    	exit 0;
    fi
    
    • Explanation: Let the shell know which interpreter to use #!/bin/bash. Run this command (COMMAND). If the exit value of the command ($?) does not equal (-ne) zero (0) then tell me about it (echo "FAILURE MESSAGE") and exit the script with a status of 1 (exit 1). Otherwise (else), tell me it worked (echo "SUCCESS MESSAGE") and exit the script with a status of 0 (exit 0).
    • Example using ls as the command:
    • #!/bin/bash
      ls
      if
      	[ "$?" -ne "0" ];
      then
      	echo "FAILURE!";
      	exit 1;
      else
      	echo "SUCCESS!";
      	exit 0;
      fi
      

String expression test

  • This is a string expression test, which means it looks for a specific string. In this example, the zero string is used. This example will attempt to run COMMAND. Since COMMAND should exit with a value of zero if it succeeds and non-zero if it fails, you can check what the exit value is and use the value to determine what happens next in your script:
  • #!/bin/bash
    COMMAND
    if
    	[ $? != "0" ];
    then
    	echo "FAILURE MESSAGE";
    	exit 1;
    else
    	echo "SUCCESS MESSAGE";
    	exit 0;
    fi
    
    • Explanation: Let the shell know which interpreter to use #!/bin/bash. Run this command (COMMAND). If the exit value of the command ($?) does not (!) equal (=) zero (0) then tell me about it (echo "FAILURE MESSAGE") and exit the script with a status of 1 (exit 1). Otherwise (else), tell me it worked (echo "SUCCESS MESSAGE") and exit the script with a status of 0 (exit 0).
    • Example using ls as the command:
    • #!/bin/bash
      ls
      if
      	[ $? != "0" ];
      then
      	echo "FAILURE!";
      	exit 1;
      else
      	echo "SUCCESS!";
      	exit 0;
      fi
      

Obligatory Happy Ending

And they all lived happily ever after. The end.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: