lab10.docx
Document Details
Uploaded by StylishSpessartine
University of Science and Technology
2024
Tags
Full Transcript
**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Open Source Operating Systems** **Lab (10) Date 15-05-2024** **[Loops in Shell Scripts]** **Description:** The students take from this exercise the three types of loops. And functions. **Lab...
**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Open Source Operating Systems** **Lab (10) Date 15-05-2024** **[Loops in Shell Scripts]** **Description:** The students take from this exercise the three types of loops. And functions. **Lab Learning Outcomes:** - Conditional loops. - Counter-controlled loops. - Iterator loops. - Funcions **Lab Instructions:** The lab instructor has to follow the steps below to demonstrate to the students the following:. 1. **Conditional loops:** A conditional loop is a loop controlled by a condition. There are two versions of the conditional loop: a while loop and an until loop. The difference between the two is in the semantics of the condition. The while loop iterates while the condition is true. The until loop iterates until the condition becomes true. **The syntax of the two loops:** while \[ condition \]; do action(s); done \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* until \[ condition \]; do action(s); done **\* while conditional loops examples:** 1\. Write a shell script to generate odd numbers? \#ODD NUMBER GENERATION PROGRAM Lab \$ vi Godd press esc key press i key echo --n "Enter the boundary number:" read num x=1 while ( test \$x --lt \$num) do echo "odd no: " \$x x=\`expr \$x + 2\` done press esc key :wq **INPUT & OUTPUT:** Enter the boundary number: 10 odd no : 1 3 5 7 9 2\. Write a shell script to find the factorial of a number? \$ vi fact press esc key press i key echo "ENTER A NUMBER:" read a i=1 fact=1 while ( test \$i --le \$a) do fact=\`expr \$fact \\\* \$i\` i=\`expr \$i + 1\` done echo "FACTORIAL VALUE OF "\$a "IS " \$fact press esc key :wq **INPUT & OUTPUT:** ENTER A NUMBER: 5 FACTORIAL VALUE OF 5 IS 120 - **until conditional loops example:** SUM=0 read --p \"Enter the first number, negative to exit\" VALUE until \[ \$VALUE --lt 0 \]; do SUM=\$((SUM+VALUE)) read --p \"Enter next number, negative to exit\" VALUE done 2. **Counter-Controlled Loops:** Another type of loop in Bash is the counter-controlled loop. This loop is a variation of the C/Java for loop. In this loop, you specify three pieces of information: a variable initialization, a loop continuation condition, and a variable increment. **Syntax is:** for ((initialization; condition; increment)); do loop body done What follows are several examples of the for loop statement (without the loop body or do/done statements). The notation i++ is a shortcut for saying i=i+1, or incrementingiby 1. for ((i=0; i\< 100; i++)) -- iterate from 0 to 99 for((i=0; i\< 100; i=i+2)) -- iterate from 0 to 99 by 2s for ((i=100; i\> j; i----)) -- iterate from 100 down to j, whatever value j is storing The syntax for the initialization, condition, and increment are similar to the syntax that we saw earlier in conditions that used (()) instead of \[\] or {}. for I in {start..end} do repeat all statements between do and done **Example:** \#!/bin/bash \#Script to test for loop \# for I in {1..5} do echo \"Welcome \$i times\" done 3. **Iterator loops:** An iterator loop is one that executes the loop body one time per item in a list. The iterator loop uses the keyword for, but is more like a for each loop as found in C\# and perl. The Bash for loop has the form **for VAR in LIST do;** **action(s)** **done** The variable, VAR, takes on each element of the LIST one at a time. You can use the VAR in the action(s). The variable VAR is often referred to as the loop variable. The LIST can be an enumerated list such as (1 2 3 4 5), the result of a Linux commandwhich generates a list (e.g., ls, cat), a list generated through filename expansion (forinstance \*.txt would expand into all file names of.txt files in the current directory) or the list of parameters supplied to the script using \$@. **Example1:** \#!/bin/bash SUM=0 for VALUE in 1 2 3 4 5; do SUM=\$((SUM+VALUE)) done echo \$SUM **Example2:** \#!/bin/bash for filename in \* ; do if \[ -f \$filename \]; then wc \$filename fi done **Example3:** \#!/bin/bash for filename in \*.txt; do if \[ -w \$filename \]; then echo \$filename fi done **Example4:** \#!/bin/bash TOTAL = 0 for filename in \*; do if \[ -f \$filename \]; then./\$filename TOTAL=\$((TOTAL+1)) fi done echo \$TOTAL scripts started Here are loops examples with an array called a: **1)for value in \${a\[@\]}; do** **\...\$value\...** **done** **\$value stores each array value** 2)**for ((i = 0; i\< \${\#a\[@\]}; i++)); do** **\...\${a\[i\]}\... ; done** **\${a\[i\]} stores each array value** **3)** \#!/bin/bash list=(File1 File2 File3 File4) fori in \${list\[@\]}; do echo\$i if \[ ! --f \$i \]; then echo Warning, \$i not found! fi done **4. Functions:** A function is a stand-alone piece of code that can be called from different program units. The function is a type of subroutine. The use of functions in Bash provides a mechanism to support reusable code Bash shell allow us to define a block of code in memory and execute it by calling its name. **Syntax:** **Function\_name()** **{** **Function body** **}** **Example:** **Myfunction()** **{** **echo "the first function"** **}** Calling function by function name: **Myfunction()** Functions can be define also inside the bash file \#!/bin/bash hello() { echo "You are in function hello()" } Hello() In the above, we called the hello() function by name by using the line: hello. When this line is executed, bash searches the script for the line hello(). It finds it right at the top, and executes its contents We can pass arguments to a function. **Example**: multi() { echo \$((\$1 \* \$2)) } Calling from the command line: multi 5 10 50 End..................