Bash is also known as GNU Bash, it is a simple command language.
What are conditionals in Bash?
Perhaps if you know a little about programming, this term is familiar to you, however, in case this is not the case, we leave you with a small definition with which you can understand more about this subject of programming. Bash and its conditionals.
The conditionals in Bash They refer to that action you perform in order to answer a question. That is, as a result of a problem you react coherently to solve it.
Bash It is made up of two conditionals, however, today it is appropriate to mention the details of »if then else», however, before knowing the information, it is important to make it clear that, to use this conditional, you must perform a comparison.
So to continue with the main topic, you must first understand what compare means in Bash.
Compare in Bash
To perform a comparison in Bash you must use the "test", or also »[», either one is totally valid. Now, it is important to take into account that, if you use the square bracket, you must place both, because otherwise there will be an error.
It is also valid to put two open brackets in each comparison, and in this way, you should not add the quotes.
In relation to the Boolean groupings, it is important to mention that you have the possibility of using the parentheses with the double brackets. This way, you avoid having to use parentheses but escapes.
It is important to mention that Boolean operators are also used, which refer to the entire set of mathematical symbols or numbers used to search within the databases of a system, in this case, Linux, for example.
After having the above clear, it is time to talk about the first comparison you make with this conditional.
- For this process you must use »if then else», taking into account the principle that according to a question there must necessarily be an answer.
- The next thing is to determine a variable, and assign it a name. It is important that, in case it ends in "to", it is understood that it is female, and if it is with any other letter the name is male.
- The arguments are understood as the indications that are placed right after a command, and in this case they are the $1 and $9.
- That way, the script should look like the following:
Options with the if-then-else conditional
Taking into account the above information, it is important to mention that there are different options with which you can use the conditional if-then-else in Bash, also depending on your goal, and here are some examples:
Principle »if one thing is true then you must do the following»
The first option is that if something is fulfilled you must respond correctly with an action, and in this case it is the following:
"In case it is not fulfilled, do something else"
Another of the options you have with the if-then-else conditional is to do something if what you wanted is true, but if it doesn't, you must carry out a different action.
»If something is fulfilled, do the following, if it is not fulfilled, do something else»
There is a last option with which you can use the conditional, it includes all the possibilities mentioned above, and it can be seen as follows:
Using nested if-then
Nested if-thes are big competitions where once you pass one test, you must move on to the next one, and so on. Many times they are compared with real life skills, to understand a little more the process that takes place as follows:
It is important that you keep in mind that many times you will need the combinations or conditionals to be completely fulfilled to decide between one action or another. For this reason, you must find a concatenation of operations, or better known as the cat command, and for this, it is necessary to use boolean operators, as follows.
- ''y' which is represented by &&, refers to the fact that both comparisons must be correct.
- »o» represented by | |, and establishes that since one of the comparisons is correct, there is no problem.
As you have read in this article, Bash is an interpreter for the most used programming languages and also for commands. For this reason, it is very important to fully understand the topic.