SlideShare a Scribd company logo
1 of 34
Lesson 12
• Decision with Loops
• Statement Iteration
• While
• Until
• For
• Select
• Nesting loops
• Loop Control
• Infinite
• Break
• Continue
Decision Making with loops
Iteration Statements
Loops
Powerful programming tool that enables execution of a set of commands
repeatedly.
• The while loop
• The for loop
• The until loop
• The select loop
Nesting Loops
All loops support nesting
– You can put one loop inside another similar or different loops.
This nesting can go up to unlimited number of times based on your requirement.
while loop
while loop iterates “while” the expression is true
enables to execute a set of commands repeatedly until some condition occurs.
It is usually used to manipulate the value of a variable repeatedly.
• It’s a looping structure. Executes a set of commands while a specified condition is true.
• The loop terminates as soon as the condition becomes false.
• If condition never becomes false, loop will never exit.
Syntax:
while command
do
Statement(s) to be executed if command is true
done
• If the resulting value is true, given statement(s) are executed.
• If command is false then no statement would be not executed and program would jump
to the next line after done statement.
while loop
#!/bin/sh
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
example display the numbers zero to nine
0
1
2
3
4
5
6
7
8
9
example result
• Each time this loop executes, the variable a is checked to see
whether it has a value that is less than 10.
• If the value of a is less than 10, this test condition has an exit
status of 0.
• In this case, current value of a is displayed, then a is incremented
by 1.
while loop
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
let COUNTER+=1
done
example
#!/bin/bash
while read line; do
echo $line
done < /etc/passwd
example
while loop
$ vi while.sh
#!/bin/bash
echo –n “Enter a number: ”; read x
sum=0
i=1
while [ $i –le $x ]; do
sum=$[sum+i]
i=$[i+1]
done
echo “the sum of the first $x numbers is: $sum”
example
while loop
$ vi menu.sh
#!/bin/bash
clear ; loop=y
while [ “$loop” = y ] ;
do
echo “Menu”; echo “====”
echo “D: print the date”
echo “W: print the users who are currently log on.”
echo “P: print the working directory”
echo “Q: quit.”
echo
read –s choice # silent mode: no echo to terminal
case $choice in
D | d) date ;;
W | w) who ;;
P | p) pwd ;;
Q | q) loop=n ;;
*) echo “Illegal choice.” ;;
esac
echo
done
example
until loop
until loop will execute the loop while the expression evaluates to false
while loop is perfect to execute a set of commands while some condition is true.
until loop is when u need to execute a set of cmds until a condition is true.
• Similar to the while structure.
• until structure loops until the condition is true. “until this condition is true, do this”.
Syntax:
until command
do
Statement(s) to be executed until command is true
done
• If the resulting value is false, given statement(s) are executed.
• If command is true then no statement would be not executed and program would jump
to the next line after done statement.
until loop
#!/bin/sh
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
example
0
1
2
3
4
5
6
7
8
9
example result
until loop
#!/bin/bash
COUNTER=10
until [ $COUNTER -lt 1 ]; do
let COUNTER-=1
done
example
until loop
$ vi countdown.sh
#!/bin/bash
echo “Enter a number: ”; read x
echo ; echo Count Down
until [ “$x” -le 0 ]; do
echo $x
x=$(($x –1))
sleep 1
done
echo ; echo GO !
example
for loop
for loop
Operates on lists of items. used when you are looping through a range of
variables. It repeats a set of commands for every item in a list.
statements are executed with var set to each value in the list
Syntax:
for var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done
• var is the name of a variable and word1 to wordN are sequences of characters
separated by spaces (words).
• Each time the for loop executes, value of the variable var is set to the next word in
the list of words, word1 to wordN.
for loop
#!/bin/sh
for var in 0 1 2 3 4 5 6 7 8 9
do
echo $var
done
example span list numbers
0
1
2
3
4
5
6
7
8
9
example result
for loop
#!/bin/sh
for FILE in $HOME/.bash*
do
echo $FILE
done
example display all the files starting with .bash and available in your home. executing from my root
/root/.bash_history
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
example result
for loop
#!/bin/bash
let sum=0
for num in 1 2 3 4 5
do
let “sum = $sum + $num”
done
echo $sum
example
for loop
#!/bin/bash
for i in $(ls); do
echo $i
done
example
#!/bin/bash
for i in $(seq 10); do
echo $i
done
example
for loop
#!/bin/bash
for x in paper pencil pen
do
echo “The value of variable x is: $x”
sleep 1
done
$vi for1.sh
#!/bin/bash
for x
do
echo “The value of variable x is: $x”
sleep 1
done
$ for1.sh arg1 arg2
The value of variable x is: arg1
The value of variable x is: arg2
if the list part is left off, var is set to each parameter passed to the script ( $1, $2, $3,…)
example
for loop
$ vi old.sh
#!/bin/bash
# Move the command line arg files to old directory.
if [ $# -eq 0 ] #check for command line arguments
then
echo “Usage: $0 file …”
exit 1
fi
if [ ! –d “$HOME/old” ]
then
mkdir “$HOME/old”
fi
echo The following files will be saved in the old directory:
echo $*
for file in $* #loop through all command line arguments
do
mv $file “$HOME/old/”
chmod 400 “$HOME/old/$file”
done
ls -l “$HOME/old”
example
for loop
$ vi args.sh
#!/bin/bash
# Invoke this script with several arguments: “one two three“
if [ ! -n “$1” ]; then
echo “Usage: $0 arg1 arg2 ..." ; exit 1
fi
echo ; index=1 ;
echo “Listing args with ”$*”:”
for arg in “$*” ;
do
echo “Arg $index = $arg”
let “index+=1” # increase variable index by one
done
echo “Entire arg list seen as single word.”
echo ; index=1 ;
echo “Listing args with ”$@”:”
for arg in “$@” ; do
echo “Arg $index = $arg”
let “index+=1”
done
echo “Arg list seen as separate words.” ; exit 0
example
C-like for loop
An alternative form of for structure is
for (( EXPR1 ; EXPR2 ; EXPR3 ))
do
statements
done
• First, the arithmetic expression EXPR1 is evaluated.
• EXPR2 is then evaluated repeatedly until it evaluates to 0.
• Each time EXPR2 is evaluates to a non-zero value, statements are executed and EXPR3
is evaluated.
$ vi for2.sh
#!/bin/bash
echo –n “Enter a number: ”; read x
let sum=0
for (( i=1 ; $i<$x ; i=$i+1 )) ; do
let “sum = $sum + $i”
done
echo “the sum of the first $x numbers is: $sum”
select loop
select loop (fuction is like a for loop with menu selection)
Easy way to create a numbered menu from which users can select options.
Useful for asking the user to choose one or more items from a list of choices.
Syntax:
select var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done
• var is the name of a variable and word1 to wordN are sequences of characters
separated by spaces (words).
• Each time the for loop executes, the value of the variable var is set to the next word
in the list of words, word1 to wordN.
• For every selection a set of commands would be executed with-in the loop.
loop was introduced in ksh and has been adapted into bash. It is not available in sh.
select loop
#!/bin/bash
select DRINK in tea cofee water juice appe all none
do
case $DRINK in
tea|cofee|water|all)
echo "Go to canteen"
;;
juice|appe)
echo "Available at home"
;;
none)
break
;;
*) echo "ERROR: Invalid selection"
;;
esac
done
example let the user select a drink of choice (A)
select loop
$./test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
#? juice
Available at home
#? none
$
example result
select loop
$PS3="Please make a selection => " ; export PS3
$./test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
Please make a selection => juice
Available at home
Please make a selection => none
$
example alternate let the user select a drink of choice (B)
change the prompt displayed by the select loop by altering the variable PS3
Nesting while Loops
while loop as part of the body of another while loop
Syntax:
while command1 ; # this is loop1, the outer loop
do
Statement(s) to be executed if command1 is true
while command2 ; # this is loop2, the inner loop
do
Statement(s) to be executed if command2 is true
done
Statement(s) to be executed if command1 is true
done
Nesting while Loops
#!/bin/sh
a=0
while [ "$a" -lt 10 ] # this is loop1
do
b="$a"
while [ "$b" -ge 0 ] # this is loop2
do
echo -n "$b "
b=`expr $b - 1`
done
echo
a=`expr $a + 1`
done
example add another countdown loop inside the loop used to count to nine
Nesting while Loops
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
5 4 3 2 1 0
6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1 0
example result
echo -n option let echo to avoid printing a new line character
Loop Control
Loop control is needed to stop a loop or skip iterations of the loop
two statements used to control shell loops
break statement
continue statement
The infinite Loop
• loops have a limited life and they execute once the condition is false or true
• loop may continue forever due to required condition is not met.
A loop that executes forever without terminating executes an infinite number of times.
Is called infinite loops.
#!/bin/sh
a=10
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
Break statement
Break statement
Used to terminate the execution of the entire loop, after completing the execution
of all of the lines of code up to the break statement. It then goes to the code
following the end of the loop.
Syntax:
break
break command can also be used to exit from a nested loop using this format:
break n #n specifies the nth enclosing loop to exit from
.
Break statement
#!/bin/sh
a=0
while [ $a -lt 10 ]
do
echo $a
if [ $a -eq 5 ]
then
break
fi
a=`expr $a + 1`
done
example loop would terminate as soon as a becomes 5
0
1
2
3
4
5
example result
Break statement
$ vi break.sh
#!/bin/bash
LIMIT=19
echo
echo “Printing Numbers 1 through 20, but something happens after 2 … ”
a=0
while [ $a -le “$LIMIT” ]
do
a=$(($a+1))
if [ “$a” -gt 2 ]
then
break
fi
echo -n “$a ”
done
echo; echo; echo
exit 0
example
continue statement
continue statement
Similar to break command
except that causes the current iteration of the loop to exit, rather than entire loop.
That is It causes a jump to the next iteration of the loop, skipping all the remaining
commands in that particular loop cycle
This statement is useful when an error has occurred but you want to try to execute the next
iteration of the loop.
Syntax:
continue
an integer argument can be given to the continue command to skip commands from nested loops:
continue n #n specifies the nth enclosing loop to continue from
.
continue statement
#!/bin/sh
NUMS="1 2 3 4 5 6 7"
for NUM in $NUMS
do
Q=`expr $NUM % 2`
if [ $Q -eq 0 ]
then
echo "Number is an even number!!"
continue
fi
echo "Found odd number"
done
example
Found odd number
Number is an even number!!
Found odd number
Number is an even number!!
Found odd number
Number is an even number!!
Found odd number
example result
continue statement
$ vi continue.sh
#!/bin/bash
LIMIT=19
echo
echo “Printing Numbers 1 through 20 (but not 3 and 11)”
a=0
while [ $a -le “$LIMIT” ]; do
a=$(($a+1))
if [ “$a” -eq 3 ] || [ “$a” -eq 11 ]
then
continue
fi
echo -n “$a ”
done
example

More Related Content

What's hot

Linux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell scriptLinux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell scriptKenny (netman)
 
Bash shell
Bash shellBash shell
Bash shellxylas121
 
Mastering unix
Mastering unixMastering unix
Mastering unixRaghu nath
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scriptingerbipulkumar
 
Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!Aaron Zauner
 
Bioinformatica: Leggere file con Perl, e introduzione alle espressioni regola...
Bioinformatica: Leggere file con Perl, e introduzione alle espressioni regola...Bioinformatica: Leggere file con Perl, e introduzione alle espressioni regola...
Bioinformatica: Leggere file con Perl, e introduzione alle espressioni regola...Andrea Telatin
 
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...Zyxware Technologies
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting BasicsSudharsan S
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide SummaryOhgyun Ahn
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scriptingEasiest way to start with Shell scripting
Easiest way to start with Shell scriptingAkshay Siwal
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scriptingCorrado Santoro
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.pptKalkey
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell ScriptDr.Ravi
 

What's hot (20)

Shell script-sec
Shell script-secShell script-sec
Shell script-sec
 
Shell Script Tutorial
Shell Script TutorialShell Script Tutorial
Shell Script Tutorial
 
Linux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell scriptLinux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell script
 
Chap06
Chap06Chap06
Chap06
 
Bash shell
Bash shellBash shell
Bash shell
 
Mastering unix
Mastering unixMastering unix
Mastering unix
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
 
Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!Beautiful Bash: Let's make reading and writing bash scripts fun again!
Beautiful Bash: Let's make reading and writing bash scripts fun again!
 
Bioinformatica: Leggere file con Perl, e introduzione alle espressioni regola...
Bioinformatica: Leggere file con Perl, e introduzione alle espressioni regola...Bioinformatica: Leggere file con Perl, e introduzione alle espressioni regola...
Bioinformatica: Leggere file con Perl, e introduzione alle espressioni regola...
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scriptingEasiest way to start with Shell scripting
Easiest way to start with Shell scripting
 
Unix - Shell Scripts
Unix - Shell ScriptsUnix - Shell Scripts
Unix - Shell Scripts
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scripting
 
Memory Manglement in Raku
Memory Manglement in RakuMemory Manglement in Raku
Memory Manglement in Raku
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.ppt
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
 

Viewers also liked

Viewers also liked (9)

Open lpi 101 preparation guide v3
 Open lpi 101 preparation guide v3 Open lpi 101 preparation guide v3
Open lpi 101 preparation guide v3
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 
Como predicar de la biblia
Como predicar de la biblia  Como predicar de la biblia
Como predicar de la biblia
 
Licão 03 vi editor
Licão 03 vi editorLicão 03 vi editor
Licão 03 vi editor
 
Licão 01 introduction
Licão 01 introductionLicão 01 introduction
Licão 01 introduction
 
Licão 02 shell basics bash intro
Licão 02 shell basics bash introLicão 02 shell basics bash intro
Licão 02 shell basics bash intro
 
Append 03 bash beginners guide
Append 03 bash beginners guideAppend 03 bash beginners guide
Append 03 bash beginners guide
 
Licão 14 debug script
Licão 14 debug scriptLicão 14 debug script
Licão 14 debug script
 
Append 00 advanced bash scripting gnu guide
Append 00 advanced bash scripting gnu guideAppend 00 advanced bash scripting gnu guide
Append 00 advanced bash scripting gnu guide
 

Similar to Licão 12 decision loops - statement iteration

Similar to Licão 12 decision loops - statement iteration (20)

34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
 
Shell programming
Shell programmingShell programming
Shell programming
 
Bash production guide
Bash production guideBash production guide
Bash production guide
 
Syntax
SyntaxSyntax
Syntax
 
Case, Loop & Command line args un Unix
Case, Loop & Command line args un UnixCase, Loop & Command line args un Unix
Case, Loop & Command line args un Unix
 
What is a shell script
What is a shell scriptWhat is a shell script
What is a shell script
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
 
shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptx
 
OS.pdf
OS.pdfOS.pdf
OS.pdf
 
Bash
BashBash
Bash
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
 
Shell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfShell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdf
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
Raspberry pi Part 25
Raspberry pi Part 25Raspberry pi Part 25
Raspberry pi Part 25
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Best training-in-mumbai-shell scripting
Best training-in-mumbai-shell scriptingBest training-in-mumbai-shell scripting
Best training-in-mumbai-shell scripting
 
Best training-in-mumbai-shell scripting
Best training-in-mumbai-shell scriptingBest training-in-mumbai-shell scripting
Best training-in-mumbai-shell scripting
 
Scripting ppt
Scripting pptScripting ppt
Scripting ppt
 
Licão 11 decision making - statement
Licão 11 decision making - statementLicão 11 decision making - statement
Licão 11 decision making - statement
 

More from Acácio Oliveira

Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptxSecurity+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptxSecurity+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptxSecurity+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptxSecurity+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptxSecurity+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptxSecurity+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptxSecurity+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptxSecurity+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptxSecurity+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptxSecurity+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptxSecurity+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....Acácio Oliveira
 
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptxSecurity+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptxSecurity+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...Acácio Oliveira
 
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptxSecurity+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptx
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptxSecurity+ Lesson 01 Topic 05 - Common Network Protocols.pptx
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptxSecurity+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptxSecurity+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptxAcácio Oliveira
 
Security+ Lesson 01 Topic 17 - Types of Malware.pptx
Security+ Lesson 01 Topic 17 - Types of Malware.pptxSecurity+ Lesson 01 Topic 17 - Types of Malware.pptx
Security+ Lesson 01 Topic 17 - Types of Malware.pptxAcácio Oliveira
 

More from Acácio Oliveira (20)

Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptxSecurity+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
Security+ Lesson 01 Topic 24 - Vulnerability Scanning vs Pen Testing.pptx
 
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptxSecurity+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
Security+ Lesson 01 Topic 25 - Application Security Controls and Techniques.pptx
 
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptxSecurity+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
Security+ Lesson 01 Topic 21 - Types of Application Attacks.pptx
 
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptxSecurity+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
Security+ Lesson 01 Topic 19 - Summary of Social Engineering Attacks.pptx
 
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptxSecurity+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
Security+ Lesson 01 Topic 23 - Overview of Security Assessment Tools.pptx
 
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptxSecurity+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
Security+ Lesson 01 Topic 20 - Summary of Wireless Attacks.pptx
 
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptxSecurity+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
Security+ Lesson 01 Topic 22 - Security Enhancement Techniques.pptx
 
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptxSecurity+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
Security+ Lesson 01 Topic 15 - Risk Management Best Practices.pptx
 
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptxSecurity+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
Security+ Lesson 01 Topic 13 - Physical Security and Environmental Controls.pptx
 
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptxSecurity+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
Security+ Lesson 01 Topic 14 - Disaster Recovery Concepts.pptx
 
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptxSecurity+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
Security+ Lesson 01 Topic 06 - Wireless Security Considerations.pptx
 
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
Security+ Lesson 01 Topic 04 - Secure Network Design Elements and Components....
 
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptxSecurity+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
Security+ Lesson 01 Topic 02 - Secure Network Administration Concepts.pptx
 
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptxSecurity+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
Security+ Lesson 01 Topic 01 - Intro to Network Devices.pptx
 
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
Security+ Lesson 01 Topic 08 - Integrating Data and Systems with Third Partie...
 
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptxSecurity+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
Security+ Lesson 01 Topic 07 - Risk Related Concepts.pptx
 
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptx
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptxSecurity+ Lesson 01 Topic 05 - Common Network Protocols.pptx
Security+ Lesson 01 Topic 05 - Common Network Protocols.pptx
 
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptxSecurity+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
Security+ Lesson 01 Topic 11 - Incident Response Concepts.pptx
 
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptxSecurity+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
Security+ Lesson 01 Topic 12 - Security Related Awareness and Training.pptx
 
Security+ Lesson 01 Topic 17 - Types of Malware.pptx
Security+ Lesson 01 Topic 17 - Types of Malware.pptxSecurity+ Lesson 01 Topic 17 - Types of Malware.pptx
Security+ Lesson 01 Topic 17 - Types of Malware.pptx
 

Recently uploaded

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 

Recently uploaded (20)

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 

Licão 12 decision loops - statement iteration

  • 1. Lesson 12 • Decision with Loops • Statement Iteration • While • Until • For • Select • Nesting loops • Loop Control • Infinite • Break • Continue
  • 2. Decision Making with loops Iteration Statements Loops Powerful programming tool that enables execution of a set of commands repeatedly. • The while loop • The for loop • The until loop • The select loop Nesting Loops All loops support nesting – You can put one loop inside another similar or different loops. This nesting can go up to unlimited number of times based on your requirement.
  • 3. while loop while loop iterates “while” the expression is true enables to execute a set of commands repeatedly until some condition occurs. It is usually used to manipulate the value of a variable repeatedly. • It’s a looping structure. Executes a set of commands while a specified condition is true. • The loop terminates as soon as the condition becomes false. • If condition never becomes false, loop will never exit. Syntax: while command do Statement(s) to be executed if command is true done • If the resulting value is true, given statement(s) are executed. • If command is false then no statement would be not executed and program would jump to the next line after done statement.
  • 4. while loop #!/bin/sh a=0 while [ $a -lt 10 ] do echo $a a=`expr $a + 1` done example display the numbers zero to nine 0 1 2 3 4 5 6 7 8 9 example result • Each time this loop executes, the variable a is checked to see whether it has a value that is less than 10. • If the value of a is less than 10, this test condition has an exit status of 0. • In this case, current value of a is displayed, then a is incremented by 1.
  • 5. while loop #!/bin/bash COUNTER=0 while [ $COUNTER -lt 10 ]; do let COUNTER+=1 done example #!/bin/bash while read line; do echo $line done < /etc/passwd example
  • 6. while loop $ vi while.sh #!/bin/bash echo –n “Enter a number: ”; read x sum=0 i=1 while [ $i –le $x ]; do sum=$[sum+i] i=$[i+1] done echo “the sum of the first $x numbers is: $sum” example
  • 7. while loop $ vi menu.sh #!/bin/bash clear ; loop=y while [ “$loop” = y ] ; do echo “Menu”; echo “====” echo “D: print the date” echo “W: print the users who are currently log on.” echo “P: print the working directory” echo “Q: quit.” echo read –s choice # silent mode: no echo to terminal case $choice in D | d) date ;; W | w) who ;; P | p) pwd ;; Q | q) loop=n ;; *) echo “Illegal choice.” ;; esac echo done example
  • 8. until loop until loop will execute the loop while the expression evaluates to false while loop is perfect to execute a set of commands while some condition is true. until loop is when u need to execute a set of cmds until a condition is true. • Similar to the while structure. • until structure loops until the condition is true. “until this condition is true, do this”. Syntax: until command do Statement(s) to be executed until command is true done • If the resulting value is false, given statement(s) are executed. • If command is true then no statement would be not executed and program would jump to the next line after done statement.
  • 9. until loop #!/bin/sh a=0 until [ ! $a -lt 10 ] do echo $a a=`expr $a + 1` done example 0 1 2 3 4 5 6 7 8 9 example result
  • 10. until loop #!/bin/bash COUNTER=10 until [ $COUNTER -lt 1 ]; do let COUNTER-=1 done example
  • 11. until loop $ vi countdown.sh #!/bin/bash echo “Enter a number: ”; read x echo ; echo Count Down until [ “$x” -le 0 ]; do echo $x x=$(($x –1)) sleep 1 done echo ; echo GO ! example
  • 12. for loop for loop Operates on lists of items. used when you are looping through a range of variables. It repeats a set of commands for every item in a list. statements are executed with var set to each value in the list Syntax: for var in word1 word2 ... wordN do Statement(s) to be executed for every word. done • var is the name of a variable and word1 to wordN are sequences of characters separated by spaces (words). • Each time the for loop executes, value of the variable var is set to the next word in the list of words, word1 to wordN.
  • 13. for loop #!/bin/sh for var in 0 1 2 3 4 5 6 7 8 9 do echo $var done example span list numbers 0 1 2 3 4 5 6 7 8 9 example result
  • 14. for loop #!/bin/sh for FILE in $HOME/.bash* do echo $FILE done example display all the files starting with .bash and available in your home. executing from my root /root/.bash_history /root/.bash_logout /root/.bash_profile /root/.bashrc example result
  • 15. for loop #!/bin/bash let sum=0 for num in 1 2 3 4 5 do let “sum = $sum + $num” done echo $sum example
  • 16. for loop #!/bin/bash for i in $(ls); do echo $i done example #!/bin/bash for i in $(seq 10); do echo $i done example
  • 17. for loop #!/bin/bash for x in paper pencil pen do echo “The value of variable x is: $x” sleep 1 done $vi for1.sh #!/bin/bash for x do echo “The value of variable x is: $x” sleep 1 done $ for1.sh arg1 arg2 The value of variable x is: arg1 The value of variable x is: arg2 if the list part is left off, var is set to each parameter passed to the script ( $1, $2, $3,…) example
  • 18. for loop $ vi old.sh #!/bin/bash # Move the command line arg files to old directory. if [ $# -eq 0 ] #check for command line arguments then echo “Usage: $0 file …” exit 1 fi if [ ! –d “$HOME/old” ] then mkdir “$HOME/old” fi echo The following files will be saved in the old directory: echo $* for file in $* #loop through all command line arguments do mv $file “$HOME/old/” chmod 400 “$HOME/old/$file” done ls -l “$HOME/old” example
  • 19. for loop $ vi args.sh #!/bin/bash # Invoke this script with several arguments: “one two three“ if [ ! -n “$1” ]; then echo “Usage: $0 arg1 arg2 ..." ; exit 1 fi echo ; index=1 ; echo “Listing args with ”$*”:” for arg in “$*” ; do echo “Arg $index = $arg” let “index+=1” # increase variable index by one done echo “Entire arg list seen as single word.” echo ; index=1 ; echo “Listing args with ”$@”:” for arg in “$@” ; do echo “Arg $index = $arg” let “index+=1” done echo “Arg list seen as separate words.” ; exit 0 example
  • 20. C-like for loop An alternative form of for structure is for (( EXPR1 ; EXPR2 ; EXPR3 )) do statements done • First, the arithmetic expression EXPR1 is evaluated. • EXPR2 is then evaluated repeatedly until it evaluates to 0. • Each time EXPR2 is evaluates to a non-zero value, statements are executed and EXPR3 is evaluated. $ vi for2.sh #!/bin/bash echo –n “Enter a number: ”; read x let sum=0 for (( i=1 ; $i<$x ; i=$i+1 )) ; do let “sum = $sum + $i” done echo “the sum of the first $x numbers is: $sum”
  • 21. select loop select loop (fuction is like a for loop with menu selection) Easy way to create a numbered menu from which users can select options. Useful for asking the user to choose one or more items from a list of choices. Syntax: select var in word1 word2 ... wordN do Statement(s) to be executed for every word. done • var is the name of a variable and word1 to wordN are sequences of characters separated by spaces (words). • Each time the for loop executes, the value of the variable var is set to the next word in the list of words, word1 to wordN. • For every selection a set of commands would be executed with-in the loop. loop was introduced in ksh and has been adapted into bash. It is not available in sh.
  • 22. select loop #!/bin/bash select DRINK in tea cofee water juice appe all none do case $DRINK in tea|cofee|water|all) echo "Go to canteen" ;; juice|appe) echo "Available at home" ;; none) break ;; *) echo "ERROR: Invalid selection" ;; esac done example let the user select a drink of choice (A)
  • 23. select loop $./test.sh 1) tea 2) cofee 3) water 4) juice 5) appe 6) all 7) none #? juice Available at home #? none $ example result
  • 24. select loop $PS3="Please make a selection => " ; export PS3 $./test.sh 1) tea 2) cofee 3) water 4) juice 5) appe 6) all 7) none Please make a selection => juice Available at home Please make a selection => none $ example alternate let the user select a drink of choice (B) change the prompt displayed by the select loop by altering the variable PS3
  • 25. Nesting while Loops while loop as part of the body of another while loop Syntax: while command1 ; # this is loop1, the outer loop do Statement(s) to be executed if command1 is true while command2 ; # this is loop2, the inner loop do Statement(s) to be executed if command2 is true done Statement(s) to be executed if command1 is true done
  • 26. Nesting while Loops #!/bin/sh a=0 while [ "$a" -lt 10 ] # this is loop1 do b="$a" while [ "$b" -ge 0 ] # this is loop2 do echo -n "$b " b=`expr $b - 1` done echo a=`expr $a + 1` done example add another countdown loop inside the loop used to count to nine
  • 27. Nesting while Loops 0 1 0 2 1 0 3 2 1 0 4 3 2 1 0 5 4 3 2 1 0 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 example result echo -n option let echo to avoid printing a new line character
  • 28. Loop Control Loop control is needed to stop a loop or skip iterations of the loop two statements used to control shell loops break statement continue statement The infinite Loop • loops have a limited life and they execute once the condition is false or true • loop may continue forever due to required condition is not met. A loop that executes forever without terminating executes an infinite number of times. Is called infinite loops. #!/bin/sh a=10 while [ $a -lt 10 ] do echo $a a=`expr $a + 1` done
  • 29. Break statement Break statement Used to terminate the execution of the entire loop, after completing the execution of all of the lines of code up to the break statement. It then goes to the code following the end of the loop. Syntax: break break command can also be used to exit from a nested loop using this format: break n #n specifies the nth enclosing loop to exit from .
  • 30. Break statement #!/bin/sh a=0 while [ $a -lt 10 ] do echo $a if [ $a -eq 5 ] then break fi a=`expr $a + 1` done example loop would terminate as soon as a becomes 5 0 1 2 3 4 5 example result
  • 31. Break statement $ vi break.sh #!/bin/bash LIMIT=19 echo echo “Printing Numbers 1 through 20, but something happens after 2 … ” a=0 while [ $a -le “$LIMIT” ] do a=$(($a+1)) if [ “$a” -gt 2 ] then break fi echo -n “$a ” done echo; echo; echo exit 0 example
  • 32. continue statement continue statement Similar to break command except that causes the current iteration of the loop to exit, rather than entire loop. That is It causes a jump to the next iteration of the loop, skipping all the remaining commands in that particular loop cycle This statement is useful when an error has occurred but you want to try to execute the next iteration of the loop. Syntax: continue an integer argument can be given to the continue command to skip commands from nested loops: continue n #n specifies the nth enclosing loop to continue from .
  • 33. continue statement #!/bin/sh NUMS="1 2 3 4 5 6 7" for NUM in $NUMS do Q=`expr $NUM % 2` if [ $Q -eq 0 ] then echo "Number is an even number!!" continue fi echo "Found odd number" done example Found odd number Number is an even number!! Found odd number Number is an even number!! Found odd number Number is an even number!! Found odd number example result
  • 34. continue statement $ vi continue.sh #!/bin/bash LIMIT=19 echo echo “Printing Numbers 1 through 20 (but not 3 and 11)” a=0 while [ $a -le “$LIMIT” ]; do a=$(($a+1)) if [ “$a” -eq 3 ] || [ “$a” -eq 11 ] then continue fi echo -n “$a ” done example