Bash snippets & functions : Testing a command return code

Introduction

One more post in the “Bash snippets and functions” series.

Here is something that you should be using quiet often not to say more ! Testing the return code of a command is the basis of any shell script.

Here is a recommended way of doing that.

 

More “bash snippets & functions” posts

 

 

1 The code & examples

1.1 Basics

Here is the “not very good” way to do it (i used to do it like this) :

test=$(grep "blabla" /tmp/test.txt)  # Keep in mind that storing a command inside a variable is NOT good:
                                     #+you should use a function for that.
if [[ "$?" = "0 " ]] ; then
  # command list
fi

Here is how it is supposed to be done :

if grep "blabla" /tmp/test.txt ; then
  # command list
fi

Why should it be done this way ?

  1. It is clearer, therefore easier to debug
  2. It is shorter and more efficient
  3. It is more robust, because in the first form you can’t always escape from a command to come between the ${test} definition and the if statement.
  4. It is classer, therefore you will shine among other scriptwriters (!)

 

1.2 Advanced

1.2.1 Negate the test

It is possible to negate the test in order to run the command list if the tested command return a “non-zero” code.

To achieve that one can use the negation operator : !

Here is an example of negating a test :

if ! grep "blabla" /tmp/test.txt ; then
  # command list
fi

In this example the command list will be executed only if grep return anything but a zero.

1.2.2 Command grouping

It is possible to group different command(s) to get their return code as a whole, in this case you can use 2 different syntax :

  1. Using brackets : { command list ; }
  2. Using parenthesis : ( command list )

The former (brackets) will not fork to a sub-shell, when the later (parenthesis) will, you choose according to your needs.

I personally use the command grouping also to make everything clearer :

if { grep "blabla" /tmp/test.txt ; } ; then
  # command list
fi

It is, of course, possible to combine a negation to a command grouping :

if ! { grep "blabla" /tmp/test.txt ; } ; then
  # command list
fi

You may also want to test more than one command return code, in this case use this construct :

if { grep "blabla" /tmp/test.txt && grep "blabla2" /var/log/log.log ; } &>/dev/null  ; then 
  # command list
fi

In this example, the command list will be executed only if the first grep AND the second grep return an error code equal to “0” (which means grep actually found something). One can adapt this construct using the OR operator (||)…

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site supports SyntaxHighlighter via WP SyntaxHighlighter. It can highlight your code.
How to highlight your code: Paste your code in the comment form, select it and then click the language link button below. This will wrap your code in a <pre> tag and format it when submitted.