OK, now to the conditionals... What do they do? They let you decide whether to perform an action or not, this decision is taken by evaluating an expression.
Conditionals have many forms. The most basic form is: if expression then statement where 'statement' is only executed if 'expression' evaluates to true. For example, '2<1' is an expresion that evaluates to false, while '2>1' evaluates to true.
Conditionals have other forms such as: if expression then statement1 else statement2. Here 'statement1' is executed if 'expression' is true,otherwise 'statement2' is executed.
Yet another form of conditionals is: if expression1 then statement1 else if expression2 then statement2 else statement3. In this form there's added only the "ELSE IF 'expression2' THEN 'statement2'" which makes statement2 being executed if expression2 evaluates to true.
OK, now to the syntax used in Bash:
... if [ expression ]; then #code goes here to be executed if expression is true fi ...The syntax is quite easy to understand, except the "="... if you worked with other languages, you use "==" to check equality, but this is BASH! it's syntax is a little different.
OK, here's a very simple script to practice an "if...else" condition:
#!/bin/bash STR1="astring" STR2="anotherstring" #don't forget the space after '[' and also before ']' !!! if [ $STR1 = $STR2 ]; then echo "$STR1 is equal to $STR2" else echo "$STR1 is not equal to $STR2" fiIt's a very stupid example but just for practice... the output will be "astring is not equal to anotherstring" as you know
Now let me tell you something! all that "[ expression ]" thing is a command in Linux! did you know? it is called "test". Check out it's man page to learn more about it and the conditions you can use inside it.
The for loop is really simple, come on! have a look:
#!/bin/bash #remember about the note we learned in previous tutorial about $(command) ? for i in $(seq 1 10); do echo $i donedon't panic! here's the explanation: in the first line of the loop, it tells that i is looped through 1 to 10, then we used a simple echo command inside the loop to print the value of i. It's quite simple
while loops are another type of conditional statements that might come handy. Here's an example of the usage:
#!/bin/bash counter=0 while [ $counter -le 10 ]; do echo $counter #*Note* let counter+=1 done*Note* this line is simple too! don't panic. it just lets us change the value of counter variable, we have to update it in order to continue the loop as we wanted. It just increases the value of counter by one.
here comes the last part:
until loops are another type of conditional loops to perform in bash scripts. The syntax is simply as follows:
#!/bin/bash counter=0 until [ $counter -gt 10 ]; do echo $counter let counter+=1 doneBasically, the loop continues until counter gets bigger than 10.
Now it's time to fulfill my oath to you! In previous tutorial, I promised to teach a simple way to know if a user has root privileges or not. Here's the script which uses an if statement and an environment variable (a variable controlled by OS and available in all scripts):
#!/bin/bash if [ $EUID -ne 0 ]; then echo "You don't have root privileges..." exit 1 fi*Note: we check the environmental variable "EUID" which is set by system to see if it is equal to 0, if false, then the user doesn't have root privileges. And the "exit 1" line tells the script is completed without success... use 0 for success.
This was the tutorial about using conditional statements and loops in Bash Scripts. Hope it was helpful for you