Linux Shell scripting loops :

syntax:

if TEST_COMMAND

then STATEMENT(S)

else STATEMENT(S)

fi

The if then else statements will execute the statements based on the single condition. The condition is given beside the “if” statement, if the given condition is satisfied then the statements inside the “if” block will be executed. If the given condition is not satisfied or not true then the statements inside the “else” block will be executed. For the loop termination we will use “fi”.

2. Shell scripting if then elif:

Syntax:

if TEST_COMMAND

then STATEMENT(S)

elif TEST_COMMAND

then STATEMENT(S)

else STATEMENT(S)

fi

The above block is executed based on multiple conditions. The first condition is given beside “if” statement and if the condition is satisfied then the statements inside the “if” block will be executed. The second condition is given beside “elif” statement and if the first condition is not satisfied then the condition beside “elif” will be checked. If that condition is satisfied then statements inside “elif” block will be executed. If none of the conditions are satisfied then the statements inside “else” block will be executed.

3. Shell scripting for loop:

The for loop moves through a specified list of values until the list is exhausted.

1)Syntax:

Syntax of for loop using in and list of values is shown below. This for loop contains a number of variables in the list and will execute for each item in the list.

for varname in list

do

echo “statement”

done

Example:

for table in {2..20..2}

do

echo “table for 2: $table”

done

Here, varname is table, list is specified under curly braces. Within the curly braces, first two will initialize the table from 2, 20 represents maximum value of $table and last 2 shows the increment by value 2.

2) Syntax:

Syntax of for like C programming language.

for (( cond1; cond2; cond3 ))

do

echo “statement”

done

Example:

for (( i=10; i>=1; i--))

do

echo “$i”

done

4. Shell Scripting while loop:

Linux scripting while loop is similar to C language while loop. There is a condition in while. And commands are executed till the condition is valid. Once condition becomes false, loop terminates.

Syntax:

while [condition]

do

commands

done

Example:

i=10

while [ $i –ge 0 ]

do

echo “$i”

let i--;

done

4. Shell Scripting until loop

It is similar to while loop. The only difference is that until statement executes its code block while its conditional expression is false, and while statement executes its code block while its conditional expression is true.

Syntax:

until [ condition ]

do

commands

done

Example:

i=5

until [ $i –gt 15 ]

do

echo “$i”

i=$(( $i+1))

done

Linux Shell scripting Functions:

Functions enables you to break down the overall functionality of a script into smaller, logical subsections. Functions can be called upon to perform individual tasks. Using of functions is to create code reusability.

Creating function:

function_name ()

{

list of commands

}

Example:

#! /bin/bash

Hello ()

{

echo “Hello world!…”

}

Hello

Hello

Passing parameters:

One or more parameters can be passed. parameters are defined as $1, $2, $3, and so on.

Example:

#! /bin/bash

# Defining function

Sum()

{

sum=$(($1+$2))

}

echo “enter two numbers: ”

read a b

#invoking Sum() function

Sum $a $b

echo “The sum is $sum”

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store