Open Source OS Lab 10: Shell Script Loops
27 Questions
0 Views

Open Source OS Lab 10: Shell Script Loops

Created by
@StylishSpessartine

Questions and Answers

Match the type of loop with its description:

Conditional loop = Loop controlled by a condition Counter-controlled loop = Loop controlled by a counter Iterator loop = Loop iterating over a collection Function = Reusable code block that performs an action

Match the loop syntax with its type:

while [ condition ]; do = Conditional loop until [ condition ]; do = Conditional loop for variable in list; do = Iterator loop count=0; while [ $count -lt num ]; do = Counter-controlled loop

Match the type of loop with its characteristic:

While loop = Iterates while the condition is true Until loop = Iterates until the condition becomes true Counter-controlled loops = Often uses a counter variable Conditional loops = Based on boolean conditions

Match the programming task with the appropriate loop type:

<p>Generate odd numbers = Conditional loop Calculate factorial = Counter-controlled loop Iterate through a list = Iterator loop Repeat an action until a condition is met = Until loop</p> Signup and view all the answers

Match the shell script component with its purpose:

<p>read num = Takes input from the user echo = Displays output to the user expr $x + 2 = Increments the counter done = Ends the loop</p> Signup and view all the answers

Match the script command with its action:

<p>echo --n 'Enter the boundary number:' = Prompts user for input while ( test $x -lt $num) = Checks if x is less than num x=<code>expr $x + 2</code> = Calculates the next odd number read num = Stores user input in variable num</p> Signup and view all the answers

Match the following concepts with their descriptions in Bash scripting:

<p>Loop Variable = Variable used to iterate over a list Function = Reusable block of code Array = Ordered collection of variables Enumerated List = Fixed set of items specified in order</p> Signup and view all the answers

Match the following Bash constructs with their functionality:

<p>for loop = Iterates over a list of items if statement = Checks a condition wc command = Counts lines, words, and characters in a file echo command = Displays output to the terminal</p> Signup and view all the answers

Match the following Bash script components with their roles:

<p>SUM = Variable aggregating values in a loop filename = Stores current file name during iteration TOTAL = Counts the number of scripts started VALUE = Holds the current iteration value in a loop</p> Signup and view all the answers

Match the following usage to their corresponding examples in Bash:

<p>for ((i = 0; i &lt; ${#a[@]}; i++)) = Iterates through an array using an index for filename in * = Processes each file in the current directory for value in ${a[@]} = Iterates through each value in an array if [ -f $filename ] = Checks if the item is a file</p> Signup and view all the answers

Match the following types of lists with their methods of creation:

<p>Enumerated List = Manually specified items like (1, 2, 3) Generated List = Output of commands like ls Filename Expansion = Pattern like *.txt matching file names Parameter List = Supplied to the script using $@</p> Signup and view all the answers

Match the following terminologies with their definitions:

<p>Bash = A command interpreter for Unix-like systems Script = A file containing a set of commands Shell = The interface for interacting with the operating system Command = An instruction for the shell to execute</p> Signup and view all the answers

Match the following loop types with their descriptions:

<p>Counter-Controlled Loop = Uses a variable initialization, condition, and increment Until Loop = Executes until a specified condition is met Iterator Loop = Executes for each item in a list While Loop = Continues as long as a specified condition is true</p> Signup and view all the answers

Match the following constructs to their purposes in Bash:

<p>if [ -w $filename ] = Checks if the file is writable echo $SUM = Displays the total sum value wc $filename = Counts lines/words/characters in a file for i in ${list[@]} = Iterates over the items in the list</p> Signup and view all the answers

Match the following Bash commands to their purpose:

<p>echo = Outputs text to the terminal read = Accepts user input expr = Evaluates expressions for = Initiates a loop over a set of items</p> Signup and view all the answers

Match the following array variables with their purposes:

<p>a = An array variable storing multiple values list = An example array containing file names VALUE = Holds the current item during iteration TOTAL = Variable used to count executed scripts</p> Signup and view all the answers

Match the following loop syntax with the correct type of loop:

<p>while (condition) = While Loop until [ condition ] = Until Loop for ((initialization; condition; increment)) = Counter-Controlled Loop for VAR in LIST = Iterator Loop</p> Signup and view all the answers

Match the following examples to the correct type of loop:

<p>for ((i=0; i&lt;100; i++)) = Counter-Controlled Loop until [ $VALUE --lt 0 ] = Until Loop for I in {1..5} = Iterator Loop while (test $i --le $a) = While Loop</p> Signup and view all the answers

Match the following script functionality with their descriptions:

<p>Factorial Calculation = Calculates the factorial of a given number Sum of Inputs = Sums user-entered numbers until a negative number is entered Counting Loop = Iterates through a specified range of numbers Welcome Echo = Prints a welcome message multiple times</p> Signup and view all the answers

Match the following terms with their definitions:

<p>Loop Body = The instructions executed within a loop Variable Increment = Increasing the value of a variable Loop Continuation Condition = Condition that determines if a loop should continue Input Prompt = Message displayed to ask for user input</p> Signup and view all the answers

Match the numbers with their descriptions in the factorial example:

<p>5 = The input number for factorial calculation 1 = The starting value for the counter in the loop 120 = The calculated factorial value of 5</p> Signup and view all the answers

Match the following Bash loop examples to their type:

<p>for((i=0; i&lt;100; i++)) = Counter-Controlled Loop for I in {start..end} = Iterator Loop while (test $i --le $a) = While Loop until [ $VALUE --lt 0 ] = Until Loop</p> Signup and view all the answers

What is the purpose of the loop variable VAR in a Bash loop?

<p>To hold the current item in the loop</p> Signup and view all the answers

In the example provided, what is the sum total after executing the loop with the values 1, 2, 3, 4, and 5?

<p>15</p> Signup and view all the answers

What does the command 'wc $filename' do in the second example?

<p>Counts the number of lines in a file</p> Signup and view all the answers

Which statement regarding functions in Bash scripting is true?

<p>Functions allow for reusable code</p> Signup and view all the answers

What will happen if the statement 'if [ -w $filename ]; then' evaluates to false?

<p>No action will be taken</p> Signup and view all the answers

Study Notes

Loops in Shell Scripts

  • Conditional Loops: Controlled by a condition, with two types:

    • While Loop: Executes as long as the condition is true.
      • Syntax:
        while [ condition ]; do
            action(s);
        done
        
    • Until Loop: Executes until the condition is true.
      • Syntax:
        until [ condition ]; do
            action(s);
        done
        
  • Examples of While Loops:

    • Odd Number Generation:
      • Prompts for a boundary number and generates odd numbers up to that limit.
    • Factorial Calculation:
      • Computes the factorial of a given number using a while loop.
  • Until Loop Example:

    • Sums user-inputted numbers until a negative number is entered.

Counter-Controlled Loops

  • Definition: Similar to C/Java for loops, defined by three elements: initialization, condition, increment.
    • Syntax:
      for ((initialization; condition; increment)); do
          loop body
      done
      
  • Examples of Counter-Controlled Loops:
    • Iterates over a range defined by initialization, condition, and increment.
    • Uses {start..end} to loop through a specified range.

Iterator Loops

  • Definition: Executes the loop body once for each item in a list.
    • Syntax:
      for VAR in LIST; do
          action(s);
      done
      
  • Examples:
    • Accumulates a sum of defined values.
    • Processes files in the current directory and applies actions like word count.
    • Iterates through text files and checks their permissions.

Using Functions in Shell Scripts

  • Definition: Stand-alone code blocks that can be reused, serving as subroutines.
  • Advantage: Promotes code reusability and organization within scripts, allowing efficient management of tasks.

Additional Notes

  • A variable in a loop can reference elements from lists and arrays.
  • Arrays can be iterated using their indices or through direct value reference.

Loops in Shell Scripts

  • Understanding three types of loops: conditional loops, counter-controlled loops, and iterator loops, is essential for scripting.

Conditional Loops

  • Controlled by a condition; includes while and until loops.

  • while loop iterates as long as the condition is true:

    • Syntax: while [ condition ]; do action(s); done
    • Example: Generating odd numbers until a specified boundary.
  • until loop iterates until the condition becomes true:

    • Syntax: until [ condition ]; do action(s); done
    • Example: Summing numbers until a negative value is entered.

Counter-Controlled Loops

  • Similar to the C/Java for loop; requires initialization, condition, and increment.
  • Syntax: for ((initialization; condition; increment)); do loop body; done
  • Examples of counting iterations:
    • for ((i=0; i<100; i++)) iterates from 0 to 99.
    • for ((i=100; i>j; i--)) decrements from 100 to an arbitrary value of j.

Iterator Loops

  • Executes the loop once for each item in a list; uses for keyword.
  • Syntax: for VAR in LIST; do action(s); done
  • VAR is the loop variable that takes values from LIST:
    • LIST can be static (e.g., 1 2 3 4 5), command output (e.g., ls), or files matching patterns (e.g., *.txt).

Examples of Iterator Loops

  • Summing a predefined list of numbers.
  • Counting words in each file within a directory using wc.
  • Checking for writable text files in the current directory.

Array Iteration

  • Arrays can be iterated using for value in ${array[@]}; do.
  • Accessing elements through index: for ((i = 0; i < ${#array[@]}; i++)); do.

Functions

  • Functions are reusable blocks of code that can be called by their name.
  • They promote code maintenance and efficiency within Bash scripts, allowing for modular programming.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Description

In this lab, students will explore the three types of loops in shell scripts: conditional, counter-controlled, and iterator loops. Additionally, they will learn about the use of functions within these loops to enhance their scripting skills. Prepare for hands-on practice and demonstration of these concepts.

More Quizzes Like This

Environment Variables in Shell Scripting
10 questions
Linux Essentials - Loop Script Exercise
77 questions
LPIC-1 Topic 105: Shells and Shell Scripting
5 questions
Use Quizgecko on...
Browser
Browser