Ch16-Advanced Examples of Iterations (Loops) in Bash Script(1)
Ch16-Advanced Examples of Iterations (Loops) in Bash Script(1)
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
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.
do
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
var=10
until [ $var -lt 5 ]
do
echo "The current value of the variable = $var"
((var--))
done
SUMMARY