Bash Scripting & Conditionals After we nailed down the part with variables, let’s move on to bash scripting conditionals. Conditional statements (branching) help our script execute commands based on tests….

Bash Scripting & Conditionals

After we nailed down the part with variables, let’s move on to bash scripting conditionals. Conditional statements (branching) help our script execute commands based on tests. With conditional statements, we determine which part of the code (branch) will execute. Basic statement used for branching would be the IF statement.

Branching in bash differs from that used in other programming and scripting languages. Bash uses status codes (see Variables post) to decide whether an IF statement will execute. Only if a previous command returns a status code of 0, will the IF statement run. Other languages use Boolean values (True/False) for the same logic.

Bash Scripting IF & Else Statements

An basic IF statement in Bash looks like:

This example will run all the code between if and fi, only when the someCommand returns a status code of “0”. For other cases we want to cover, when the someCommand returns a status code other than “0”, we use the ELSE statement. To show this works, let’s use a GREP command that returns a status code “0”after finding a particular string in a file:

Using our knowledge of GREP, let’s create a script, within the same directory as the luke.txt file, using:

Running the above will print “Nooo!” if it finds the string “father” in the file luke.txt, otherwise it will print “?!”.

IF and ELSE statements are very useful for comparison, like comparing:

  • a variable to a number
  • a variable to a string
  • if a file or directory exists

Using the TEST command in Bash

As the IF statement executes only when the returned status code is “0”, a good command to run comparisons is TEST. With TEST you get easy access to comparison and file test operations like:

TEST command comes with other kinds of comparison options like greater than / lower than… More examples can are available on the TEST command manual pages.

When working with the TEST command, two things are worth noting:

Integers vs. Strings

The TEST looks different at integers and strings. For example, 007 and 7 are the same by numeric but different by string comparison:

TEST command alias

The TEST command is often used, so you can use a “[” (left square bracket), as a shortened alias of the command. So you can write the following in two ways:

or

Note: When using “[” command, make sure to have a closing bracket “]” too.

Multi-comparisons using IF statements

IF statements can consist of multi-comparison, handled by the ELIF statement:

In the above example, a variable is passed to the script, then gets assigned to internal script variable QUESTION. Then, based on the variables value, the script echoes out one of the outputs.

Since IF/ELIF/ELSE complicate things, you can use switch-case statements when handling multi-comparisons, for example:

So, a variable gets pattern-matched trough all the cases, where each case terminates with parenthesis “)”. The first case matches the Chef or Puppet strings. Note how multiple operations are separated with the vertical bar “|”, representing the OR operator in many programming languages. Following are the commands to execute in case the variable does match a given value, and the whole case terminates with two semicolons “;;”.

The sequence ends with a “*)”, where “*” matches anything, so ensuring that, if all other cases fail, the last action gets executed.

Statements are important in scripting, only IF you have a solid knowledge, you can have your scripts execute commands IF necessary.

Stay with us, as there is more to com

Website: