View
3
Download
0
Category
Preview:
Citation preview
1
Week 6 – Lesson 1:Control-Flow Statements (Continued)
2
Chapter Objectives
In this chapter, you will: Learn additional shell scripting tools including:
Logic
Case Statement
Loops
Purpose
Indeterminate Loops (while , until)
Determinant Loops (for)
3
Logic
case statement
The case control-flow statement is used to make a decision based upon multiple inputs that are constants.
For decision-making for ranges of values (such as letter grades based on percentage marks – eg. A for 80 – 89), then the if / elif / else control-flow statement would work better.
Refer to diagram above for flowcharting representation of a case statement….
4
Logic
case statement
SYNTAX:
case $var in pattern1) command(s) ;; pattern2) command(s);; pattern3) command(s);; .. default) command(s);;esac
Marks end of case statement
Patterns can consist of letters,numbers, character class [ .. ].
;; represents break to endof case statement (esac)
Default (use * symbol) representswhat to do if no patterns arematched…
5
Logic
Example #1
Based on the commands below, show the stdout:
door=2case $door in 1) echo “Door #1”;; 2) echo “Door #2”;; 3) echo “Door #3”;; *) echo “Door #?”;;esac
Question: What would happen to above case statement if value of door was set to -1?
6
Logic
Example #2
Based on the commands below, show the stdout:
read –p “Pick an odd number between 11 – 19: ” number case $number in
[1][13579]) echo “The odd number is $number”;; [1][02468]) echo “That number is even – try again”;; *) echo “Must be a positive two digit number”;;esac
Question: What would happen to above case statement if the user selected 11? 19? 12? 0? 12? -2?
7
Loops
Loops
A loop is a condition that causes a specific set of statements to be repeated.
Statements repeat until a condition terminates the loop.
An iteration refers to the number of complete passes the loop makes.
If a loop repeats 4 times, it has completed 4 iterations.
8
Loops
Advantage – allows you to enter data without knowing the exact amount of data that needs entering.
Used in menu-based systems.
Poor programming logic design can cause infinite loops.
An infinite loop executes without end – can be terminated with the kill command.
Disadvantage of infinite loop – consumes excessive processing time.
9
Loops
There are two types of loops:
Determinant
The number of repetitions (iterations) are known. For example, print “hello” six times. The for loop is a determinant loop
Indeterminate
The number of repetitions (iterations) are unknown. For example, continue prompting the user until they enter a positive number between 1 and 10. We do not know how many times or what type of data the user will enter until they get it correct! The while and until loops are indeterminant loops.
10
Loops while Control-Flow Statement
The theoretical while statement tests a condition for true or false.
Syntax:
while true-condition do
Perform activity for true-condition done
If condition is true do statements execute.
If condition is false loop terminates and program flow continues with subsequent statement.
11
Loops
Additional Information (while loop)
Can be used to test exit status of a command. Useful for determining if a command succeeded or
failed. Example to copy a file if it exists:
while ! cp fileX.txt fileY.txt do echo “Attempting to copy…” sleep 10echo “Unable to copy…”
done
12
Loops Additional Information (while loop)
Can use true in the while statement to loop until a condition is met, then use the break command to terminate the loop.
Example:
((count=1)) while true do
if [[ $count –gt 3 ]] then
break fi count=$((count+1))
done
13
Loops
Example
Based on the commands below, show the stdout:
number=23 while echo $number | grep -qv "^[1-9][0]*$" do read -p "Pick a number (1-10): " number done
echo "The number is: $number"
Question: What would happen if the user entered 1? 9? 10? 0? 11? -1? A? x? Nothing?...
14
Loops until Control-Flow Statement
The until control-flow statement is similar to the while statement, but works the opposite way – it loops if the condition is false, and terminates the loop if the condition is true.
Theoretical until Logic:
until true-condition do
Perform activity for true-condition done
If condition is false do statements execute.
If condition is true loop terminates and program flow continues with subsequent statement.
15
Loops Example
Based on the commands below, show the stdout:
number=5 echo "Count-Down:"
until [ $number -eq 0 ] do echo -n "$number " number=$((number - 1)) sleep 1 done echo "Lift-off!"
TASK: Describe step-by-step what how this program runs …
16
Loops
for Loop
Used to repeat commands for a specified number of repetitions (iterations).
There are several methods how to use the for loop…
17
Loops
for Loop (Method 1)
Word list form:
for variable in arg1 arg2 arg3 .. argN do
command(s) done
Each argument (arg1, arg2, arg3 .. argN) is used in a separate iteration…
When all arguments are used, then the for loop terminates.
18
Loops
Example
Based on the commands below, show the stdout:
counter=1 for item in pens pencils paper do echo "arg #$counter: $item" counter=$((counter + 1)) done
TASK: Describe step-by-step what how this program runs …
19
Loops for Loop (Method 2)
Read positional parameters:
for variable do
command(s) done
If for loop does not include reserved word “in” followed by the arugments, then any positional parameters will be used.
When all positional parameters are used, then the for loop terminates. The positional parameters stored prior to for loop still remain to be used later if necessary…
20
Loops Example
Below are the contents of the file called items.bash:
counter=1 for item do echo "arg #$counter: $item" counter=$((counter + 1)) done
TASK: Assuming items.bash is in your current directory and has execute permissions. What will be the stdout if the following commands are issued:
./items.bash pigs horses hats rats books cars umbrellas
./items.bash “pigs horses hats rats” books cars umbrellas
21
Loops
for Loop (Method 3)
The for loop can use arithmetic expression to increase or decrease a value for the number of iterations
Syntax:
for ((variable=start-value; variable condition; change variable value))
do command(s)
done
22
Loops Example
Below are the contents of the file called count.bash:
read -p "Enter # of repetitions: " number echo -n "Repeating: "
for ((x=1; x <= number; x++)) do echo -n "$x " sleep 1 done echo "times..."
TASK: Describe step-by-step what how this program runs …
23
Summary
The case control-flow statement is used to make a decision based upon multiple inputs that are constants.
A loop is a condition that causes a specific set of statements to be repeated. Statements repeat until a condition terminates the loop.
There are two types of loops:
Determinant (# of repetitions know – for ) Indeterminate (# of repetitions unknown – while, until)
24
Summary
With indeterminate loops, it is important to allow the user to change the value of a variable (if prompted) or change the value of the variable by some means (eg. Mathematics).
If the value remains the same, then the loop may repeat forever (which are referred to as “infinite loops”).
Shell scripts caught in “infinite-loops” can cause reduced productivity of the computer server's operations...
Recommended