REPETITION
STRUCTURES
QUESTION
• What are some reasons to repeatedly execute commands?
• What are some tasks that we can/do perform on our machines regularly?
LOOPING IN SCRIPTS
• Loops are used to perform a set of commands repeatedly until a condition is met to
terminate them.
• Question: What are some programs you wrote in CIS120 that used loops?
• While we can use the loops the same in each language a distinction has to be made
for scripting.
Programming Scripting
FOR LOOP
• The For Loop is a looping
structure used to execute a set
of commands a specified
number of times.
– for var in <list>
do
command
command
done
• In programming you would specify a
range to execute your loops. Ex. 1 -
100, 2-10
• In scripting, for loops are much more
powerful than just executing ranges:
– Range: Run for a predefined set of
numbers
– List: A list of values or files
– List/Range: A list with a naming scheme
– Directory: A directory to manipulate
EXAMPLE: FOR LOOPS
• Exercise in Learning Materials
– Identify what the for loop is doing:
• for1.txt
• for2.txt
• for3.txt
• for4.txt
WHILE LOOP
• The While Loop is a looping
structure that executes as long
as a condition is true(exit status
of 0).
– while [ condition ]
do
command
command
done
• Question: What are some conditions
that we can execute a while loop over?
• There is another type of loop known as
an Until Loop that executes while a
condition is false.
• Question: What is the exit status being
tested for an Until loop?
• Question: How could we alter a while
loop to behave in the same way as an
Until loop?
EXAMPLE: WHILE LOOPS
• Exercise in Learning Materials
– Identify what the while loop is doing:
• while1.txt
• while2.txt
• while3.txt
I/O REDIRECTION ON A LOOP
REDIRECT OUTPUT
• for i in 1 2 3 4
do
echo $i
done > loopout.txt
• for file
do
echo “Process $file” > &1
done > output
• while [“$endofdata” –ne TRUE ]
do
…
done 2 > errors
REDIRECT INPUT
• while read n
do
run $n
done < installFile.txt
• You can override redirection of the entire loop’s input or output by explicitly using the
redirect commands.
– <, >
BREAK AND CONTINUE
BREAK
• The Break command is often used to
terminate the remainder of a loop
• Used to create a condition where if an
undesirable outcome was to happen
you can test for it and break the loop.
– Ex. If disk space is low stop processing
files immediately
• break
CONTINUE
• The Continue command is used to
terminate the current iteration of a
loop.
• Used to test if an undesirable outcome
happens and then to start a new
iteration of the loop.
– Ex. If you are updating files and one
doesn’t exist, proceed to the next one.
• continue

Repetition Structures

  • 1.
  • 2.
    QUESTION • What aresome reasons to repeatedly execute commands? • What are some tasks that we can/do perform on our machines regularly?
  • 3.
    LOOPING IN SCRIPTS •Loops are used to perform a set of commands repeatedly until a condition is met to terminate them. • Question: What are some programs you wrote in CIS120 that used loops? • While we can use the loops the same in each language a distinction has to be made for scripting. Programming Scripting
  • 4.
    FOR LOOP • TheFor Loop is a looping structure used to execute a set of commands a specified number of times. – for var in <list> do command command done • In programming you would specify a range to execute your loops. Ex. 1 - 100, 2-10 • In scripting, for loops are much more powerful than just executing ranges: – Range: Run for a predefined set of numbers – List: A list of values or files – List/Range: A list with a naming scheme – Directory: A directory to manipulate
  • 5.
    EXAMPLE: FOR LOOPS •Exercise in Learning Materials – Identify what the for loop is doing: • for1.txt • for2.txt • for3.txt • for4.txt
  • 6.
    WHILE LOOP • TheWhile Loop is a looping structure that executes as long as a condition is true(exit status of 0). – while [ condition ] do command command done • Question: What are some conditions that we can execute a while loop over? • There is another type of loop known as an Until Loop that executes while a condition is false. • Question: What is the exit status being tested for an Until loop? • Question: How could we alter a while loop to behave in the same way as an Until loop?
  • 7.
    EXAMPLE: WHILE LOOPS •Exercise in Learning Materials – Identify what the while loop is doing: • while1.txt • while2.txt • while3.txt
  • 8.
    I/O REDIRECTION ONA LOOP REDIRECT OUTPUT • for i in 1 2 3 4 do echo $i done > loopout.txt • for file do echo “Process $file” > &1 done > output • while [“$endofdata” –ne TRUE ] do … done 2 > errors REDIRECT INPUT • while read n do run $n done < installFile.txt • You can override redirection of the entire loop’s input or output by explicitly using the redirect commands. – <, >
  • 9.
    BREAK AND CONTINUE BREAK •The Break command is often used to terminate the remainder of a loop • Used to create a condition where if an undesirable outcome was to happen you can test for it and break the loop. – Ex. If disk space is low stop processing files immediately • break CONTINUE • The Continue command is used to terminate the current iteration of a loop. • Used to test if an undesirable outcome happens and then to start a new iteration of the loop. – Ex. If you are updating files and one doesn’t exist, proceed to the next one. • continue

Editor's Notes

  • #4 Difference ex. Black jack game until user gets 21 points vs. starting up a set of machines Summing the numbers from x – 100 vs. executing all the files in a folder Replaying a game vs. setting env variables in profiles
  • #5 List Range: go voer with examples for i in memo[1-4]
  • #6 For3 – no spaces between curly braces or will be identified as a list and not a range, {val..val2..step}
  • #7 Note the while loop does not need the [] if it is a command not a condition(read) End of file is a condition that has an exit status
  • #8 How ca we modify while 3 for any duplicated file not just 1 we specify
  • #9 Note than when direction is overridden it is globally known. That way any additions like to the terminal are only for the specific line designated