In this tutorial we will look at Bash-based conditional statements. We’ll look at practical examples and discuss when a Bash case … esac statement is best used over a more traditional one if .. then .. fi
statement.
Bash case .. esac Statements in Bash
Originally available from the Bash shell, case conditional statements formulated using the case
and esac
idiom (an idiom is a word or group of words, or a keyword, with established usage patterns; a form of expressing a computer language), one can construct a complex, conditional case-by-case (hence the term case
) look up and run on a case-by-case basis.
The lookup portion of a case-based statement is sometimes called a select, leading to the term select / case statement (or vice versa), used in some other coding languages such as VBA, to refer to the same type of programming paradigm. Yet another set of languages (such as C #, C ++, Java and JavaScript) call this one switch or switch / case statement. All of these have similar meanings and operations.
Let̵
case .. esac
statement using a simple script.A simple one case .. esac script
We define our test script test.sh
as follows:
#!/bin/bash case "${1}" in 1) echo "Option was 1!";; 2) echo "Option was 2!";; *) echo "Option was something else: '${1}'";; esac
Then we make our script executable by executing chmod +x test.sh
and then we run the script itself, passing a different option as input each time.
In our opening sentence, we indicate that we use the first input option ($1
) as the variable to evaluate. Our case .. esac
statement will verify each option (as indicated by the text preceding a )
clause closing idiom) and then process the code block (after the )
idiom) for the correct result.
Note that there are two ;
terminators at the end of each line. The reason for this is simple; normal Bash syntax requires you to have at least one terminating idiom ;
(unless you are already at the end of a line), but within the case .. esac
statement, we have an extra ;
to indicate that we are ending a code block for a specific code execution block.
We can see how we defined code for both cases where 1
and 2
are respectively passed to the script. But what happens if we pass something else? In that case we want to go to our Others clause, denoted by a universal *
idiom.
We see how we pass 1
or 2
the appropriate action is taken to the script; Option was 1/2!
. We also see how when we do something else, the *
code block executes correctly, and nicely also displays our first variable passed to the script. What would happen if we didn’t give an option to our script? The case .. esac
code would still be evaluated, and since nothing matches except our catch-all *
clause, which is executed:
So what would happen if we assumed, something that makes sense, that we can leave a single ;
statement from the end of the line of each case .. esac
clause simply because an EOL (end of line) is reached at the same time?
Note that only single terminating ;
was used in this script. Bash does not allow this and an error is made; the last terminating syntax of ;;
is required, and something to keep in mind when writing case .. esac
explanations
Why case .. esac And not as?
We could also write the previous code with a if
statement:
#!/bin/bash if [ "${1}" == "1" ]; then echo "Option was 1!" elif [ "${1}" == "2" ]; then echo "Option was 2!" else echo "Option was something else: '${1}'"; fi
This seems similar in code quality and brevity compared to ours case .. esac
code, and in the given situations and specific use cases like these, often a if .. then .. elif .. else .. fi
based solution will do fine.
That said, if and when the complexity of the selection becomes more complex or greater, or if there are multi-layered case selections to be performed (note that case .. esac
can be nested), it can be rewarding to use case .. esac
explanations.
Another nice feature of it case .. esac
explanations is that one can mention two possible options for a single code execution block, as we will see in our next example, but this can also be done with standard Bash if
statements, using for example the -a
(and and -o
(or) options from our test clause and specifying a secondary test clause. For example, if [ "${1}" -eq 1 -o "${1}" -eq 3 ]; then ...
Note that we’ve also used numeric equations here instead of text-based equations like in our example above.
For more information about if .. then .. else .. elif .. fi
conditionals, you can also read our article Conditional Testing in Bash: if, then, else, elif.
Expand our business
Let’s expand our previous script and make our conditions a bit more complicated:
#!/bin/bash case "${1}" in 1|3) echo "Option was 1!" echo "Or, option was 3!";; 2|'a') case "${1}" in 2) echo "Option was 2!";; 'a') echo "Option was 'a'!";; esac;; *) echo "Option was something else: '${1}'";; esac
In this case we have listed two possible options for each block of code and separated them by one |
separator. Notice how everything works fine and the inputs 1
and 3
be parsed correctly. In the second code block, we created a nested case and used every possible input for the second code block (namely 2
and a
) as new options in the nested / secondary esac.
This works flawlessly again. The flow through the code is that in the first case
statement, the 2|'a'
branch is taken 2
or a
is passed, and then the second case
makes sure that too 2
or a
is selected individually. Also interesting is the use of ;;
every time we want to terminate a block of code for a specific option, including terminating the nested case .. esac
, again with ;;
ie esac;;
.
Shut down
In this tutorial, we have bash-based practical examples case .. esac
explanations. We also saw where case statements are a better choice than more traditional ones if .. then .. fi
based statements.
If Bash interests you, you’ll find Primer: Bash Loops: for, while and until, Bash Process Termination Hacks and How to Correctly Parse File Names in Bash interesting.
To enjoy!
Source link