Loops used in Bash Scripts.
All the loops can be exited with break command.
For Loops
Executed on each pass for one parameter in a list.
It has the syntax:
for <variable> in <list>
do
<do something>
donewhere a <list> consists of space separated values.
A list of consecutive values from a to b can be constructed with {a..b}.
For loops can also be written in a single line:
for <variable> in <list>;do <do something>;doneWhile Loops
Executes as long as <condition> is true.
It has the syntax:
while [ <condition> ]
do
<do something>
doneUntil Loops
Executes until <condition> is true.
It has the syntax:
until [ <condition> ]
do
<do something
doneExamples
For Loops
for variable in {1.4}
do
echo $variable
donefor variable in file1 file2 file3
do
echo $variable
donefor ip in "10.10.10.170 10.10.10.174 10.10.10.175"
do
ping -c 1 $ip
done