0% found this document useful (0 votes)
3 views18 pages

Ch16-Advanced Examples of Iterations (Loops) in Bash Script(1)

The document provides advanced examples of iterations in Bash scripting, focusing on the FOR, WHILE, and UNTIL loops. Each example includes detailed explanations, pseudocode, flowcharts, scripts, and outputs to enhance understanding. The content aims to build students' confidence in writing their own Bash scripts.

Uploaded by

ngaantraanf0711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

Ch16-Advanced Examples of Iterations (Loops) in Bash Script(1)

The document provides advanced examples of iterations in Bash scripting, focusing on the FOR, WHILE, and UNTIL loops. Each example includes detailed explanations, pseudocode, flowcharts, scripts, and outputs to enhance understanding. The content aims to build students' confidence in writing their own Bash scripts.

Uploaded by

ngaantraanf0711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

ITERATIONS IN

BASH
Advanced Examples of Iterations (Loops) In
Bash Script
PROBLEM A
■ Use the FOR loop in Bash to write a program without a list and read
command-line arguments.
– We can use the FOR loop without any list/command output/array.
– We use the $stp variable for finding the step of the loop.
– Each step will increment the value of $stp by 1.
– When the $stp is 1, the script will print the message “I like chocolates”
– When the $stp is 2, the script will print the message “My favorite color is

– When the $stp is 3, the script will print the message “I love Git Bash”
– do indicates the start of the loop
– echo $stp is used to display the value which is stored in the stp variable.
– ; will terminate the echo statement.
– done will indicate the end of the loop.
– The command-line arguments to run the script are $ bash
problem1.sh Chocolates red Git Bash
PROBLEM A
PSEUDOCODE
The following code will use the FOR command to create a program without a list

The following code will use the FOR command to create a program without a list

Define a variable stp=1


for text
do
if stp is equal to 2
then
disp="My favorite color is "
elif stp is equal to 3
then
disp="I love "
else
disp="I like "
fi
Print the result "$disp $text"
Increment the variable stp by 1 using the + operator
done
PROBLEM A
FLOWCHART AND SCRIPT

stp=1
for text
do
if [ $stp -eq 2 ]
then
disp="My favorite color is "
elif [ $stp -eq 3 ]
then
disp="I love "
else
disp="I like "
fi
echo "$disp $text"
((stp=$stp+1))
done
PROBLEM A
OUTPUT
PROBLEM B
■ Use the infinite FOR loop to display a five lists menu and display a specific
message for each number (5 to exit).
– We use the infinite FOR loop to perform repetitive tasks multiple
times.
– The loop terminates when a particular condition appears.
– We will not define a termination condition in this loop.
– The loop will display a menu for 5 lists until the user presses the
number 5.
– For other numbers, the program will display a specific message
(from 1 to 4).
– After that, it will display the menu again.
– The empty for expression (( ; ; )) is used to define an infinite loop.
– Since we are using the case command inside an infinite loop in
this example, we will use the exit command to terminate the loop.
PROBLEM B
PSEUDOCODE
The following code will use the FOR command to create a program to allow the user to choose from 1 to 5 and display
different results.

define the infinite FOR loop

do

Print the message "1. Print the success message."

Print the message "2. Print the information message."

Print the message "3. Print the Warning message."

Print the message "4. print the error message."

Print the message "5. To Exit"

Take the input from the user

define the case expression, followed by the “in” keyword

Print the result "The Task is Successfully completed.";;

2) Print the result "It is an Invalid input";;


3) Print the result "Your computer is running on low battery";;
4) Print the result "You submitted a Wrong number of arguments ";;
5) exit 0;;
*) print the result “Wrong selection";;
terminate the case statement using “esac”
done
PROBLEM B
FLOWCHART AND SCRIPT

for (( ; ; ))
do
echo "1. Print the success message"
echo "2. Print the information message"
echo "3. Print the Warning message"
echo "4. print the error message"
echo "5. To Exit"
echo -n "Select a number from [1-5]:"
read ans

case "$ans" in
1) echo "The Task is Successfully completed."
;;
2) echo "It is an Invalid input";;
3) echo "Your computer is running on low
battery";;
4) echo "You submitted a Wrong number of
arguments ";;
5) exit 0;;
*) echo "Wrong selection";;
esac
done
PROBLEM B
OUTPUT
PROBLEM C
■ Use the CONTINUE statement in the WHILE loop to display a set of 5
numbers and omit the third step.
– In this program we will use the CONTINUE statement in the WHILE loop.
– The program will iterate 5 times without printing all of the 5 positions.
– We will define the condition [ $i -le 5 ] inside the WHILE loop.
– The program will check the condition [ $i -le 5 ] before executing the
commands.
– The program will execute the commands between do and done for as
long as the condition [ $i -le 5 ] is true.
– We will use the CONTINUE statement in the third position. Thus, the
loop will skip the text of third position and continue to the next iteration.
– We will define the expression inside the if..then..fi statement within the
WHILE loop.
– If the expression is true; [ $i == 3 ], the loop will execute the CONTINUE
statement and omit the third step.
– If the expression is false then the program will continue to the next
iteration.
PROBLEM C
PSEUDOCODE

The following program will use the CONTINUE statement in the


WHILE loop to display a set of 5 numbers and omit the third step.

Assign the value to the variable i


while i is less than equal to 5
do
increment the value of i using the ++ operator
if i is equal to 3
then
continue
fi
Print the result "Value of i: $i"
done
PROBLEM C
FLOWCHART
PROBLEM C
SCRIPT AND OUTPUT
i=0
while [ $i -le 5 ]
do
(( i++ ))
if [ $i == 3 ]
then
continue
fi
echo "Value of i : $i"
done
PROBLEM D
■ Use a variable in the UNTIL loop to decrement a number
initialized at ten until it reaches the value 5.
– In this program, we will use the $var variable for controlling the
iteration of the loop.
– We will initialize the loop at 10.
– The loop will carry on the iteration six times.
– We use the $echo command for printing the value of $var after
each step.
– To reach the UNTIL loop's termination condition, the value of $var
will decrement by 1 in each step.
– We will define the condition [ $var – lt 5 ] inside the UNTIL loop.
– The loop will check this condition before executing the commands.
– It will only execute the commands if the condition evaluates to
false.
– The loop will get terminated when the condition evaluates to true
PROBLEM D
PSEUDOCODE

The following program will use counter in the UNTIL loop to


decrement the value of a number 6 times.
Initialize the value of the variable var=10

until var is less than 5


do
Print the result "The current value of the variable = $var"
decrement the value of var by using the -- operator
done
PROBLEM D
FLOWCHART
PROBLEM D
SCRIPT AND OUTPUT

var=10
until [ $var -lt 5 ]
do
echo "The current value of the variable = $var"
((var--))
done
SUMMARY

■ In this chapter, we looked at four advanced examples of iterations in


Bash which will help students get a better understanding of the way
the iterations function in Bash.
■ Each example contains detailed explanation, along with flowcharts,
pseudocodes, scripts and outputs.
■ The examples offer an in-depth analysis of the FOR, WHILE and UNTIL
loops used in Bash scripting.
■ Students can gain confidence in writing their first code by going
through these examples.

You might also like