Decision Loops

Linux Scripting

Last Update Unknown

If Statements

If else statements are useful decision-making statements which can be used to select an option from a given set of options.


Case...esac Statements

 You can use multiple if...elif statements to perform a multiway branch.

However, this is not always the best solution, especially when all of the branches depend on the value of a single variable


While Loops

The while loop is perfect for a situation where you need to execute a set of commands while some condition is true. It is usually used when you need to manipulate the value of a variable repeatedly.


For Loops

The for loop operates on lists of items. It repeats a set of commands for every item in a list.


Until Loops

The until loop enables you to execute a set of commands repeatedly until some condition occurs. 


The break Statement

The break statement is used to terminate the execution of the entire loop, after completing the execution of all of the lines of code up to the break statement. It then steps down to the code following the end of the loop.


The following break statement is used to come out of a loop:

break


The break command can also be used to exit from a nested loop using this format where n specifies the nth enclosing loop to exit from.

break n


The continue Statement

The continue statement is similar to the break command, except that it causes the current iteration of the loop to exit, rather than the entire loop.


This statement is useful when an error has occurred but you want to try to execute the next iteration of the loop.

continue


Like with the break statement, an integer argument can be given to the continue command to skip commands from nested loops where n specifies the nth enclosing loop to continue from.

continue n