Conditional Statements
Conditional statements allows you to run certain commands based on a certain condition. This is just the same as if and switch statements in languages like C, PHP and others.
The input and output of conditional statements are connected to the input and output of all commands within the statement. This means that any redirections applied on conditional statements are automatically applied on all commands within the statement. The same rule applies when you use conditional statement within pipelines.
Note that in any of the examples below, whenever a semiclon is used. can be replaced by one or more newlines as mentioned before
if statement
The most basic syntax of an if statement looks like:
if test-commands; then
consequent-commands
fiThe test-commands are invoked first. Then if (and only if) the exit status is zero, consequent-commands are then invoked.
For example:
if true; then
echo Horrey
fi
if false; then
echo Booo
fiOutputs:
Horreyelif branches
The format is so simple:
if test-commands; then
consequent-commands
elif alt-test-commands; then
alt-consequent-commands
fielif act as an alternative branch. if test-commands exited with zero. consequent-commands are invoked and the statement returns. Otherwise, if test-commands exited with non-zero, then alt-test-commands are executed. and if the exit status is zero, alt-consequent-commands are then executed.
You can have as many elif branches as you want. For example:
if cmd1; then
echo foo
elif cmd2; then
echo bar
elif cmd3; then
echo baz
fielse branch
The else branch is the last branch in the if statement. looks like:
if test-commands; then
consequent-commands
else
alt-consequent-commands
fiThe else branch does not have a test commands. because it is the default branch that gets executed when (and only when) none of the other branches get pass the test.
if test-commands exited with zero. consequent-commands are invoked and the statement returns. Otherwise,alt-consequent-commands are then executed.
For example:
if false; then
echo foo
elif false; then
echo bar
else
echo baz
ficase statement
The case statement is similar to the switch statement in languages like C and Go.
The most basic format is:
case WORD in
(PATTERN) consequent-commands ;;
esacWORDis the value to match against, can be any string.PATTERNis a string that represents a glob pattern.consequent-commandsis a list of one or commands.(PATTERN) consequent-commands ;;is known as a CLAUSE.- the left parenthese
(in the clause is optional
The case statement can have one or more clauses, and each clause can have or or more patterns separated by |.
The case statement will go through each clause, and match WORD against its patterns. if one of the patterns matches, then the consequent-commands corresponding to that clause will be executed.
For example:
CWD=$(pwd)
case $CWD in
/root ) echo "we are in the 'root' home directory";;
/home/* | /Users/*) echo "we are in home directory";;
/var/www/html/*) echo "we are in nginx default directory";;
esacDefault clause
It's common to use the * as the final pattern to define the default clause, since that pattern will always match.
for example:
OS=$(uname)
case $OS in
Linux | Darwin ) echo "Unix system, cool";;
*) echo "the operating system '$OS' is unknown";;
esacControl the match flow
Each clause must be terminated by ;;, ;& or ;;&.
If the
;;operator is used, no subsequent matches are attempted after the first pattern match.shcase foo in f*) echo "it's foo";; *oo) echo "it's foo as well";; esacOutputs:
txtit's fooThe
;&causes execution to continue with theconsequent-commandsassociated with the next clause, if any. You can think of as thefallthroughkeyword inGo.shcase foo in f*) echo "it's foo";& bar) echo "it's bar";; esacOutputs:
txtit's foo it's barThe
;;&causes the case statement to test the patterns in the next clause, if any, and execute any associatedconsequent-commandson a successful match, continuing the execution as if the pattern list had not matched.shcase foo in f*) echo "it's foo";;& *oo) echo "it's foo as well";; esacOutputs:
txtit's foo it's foo as well
