If if-else if-elif-else. In this chapter you will learn about loop logic in Bash, including:
for
while
until
- use
continue
break
Control loop
cycle
In the common logic interaction, naturally can not lose the logic of the cycle, clever use of the cycle, can save a lot of development time
for
For allows a class to execute a judgment command each time it loops, and execute that loop if the result passes.
format
for NAME [ in LIST ]; do
[COMMAND]
done
Copy the code
Example 1:
#! /bin/bash
# Echo list with space deviced
users='mengj guom tj'
for user in ${users}; do
echo ${user}
done
Copy the code
Using a for loop defaults to using a separator for a LIST, and in the example above you can print each item in the LIST in the terminal.
mengj
guom
tj
Copy the code
Example 2:
The default delimiter is, so we can change the LIST delimiter by declaring IFS (Internal FIeld Seprator) to print different LIST child elements.
list='mengj,guom,tj'
IFS=,
for item in ${list}; do
echo ${item}
done
Copy the code
In the example above we declared the delimiter IFS to be, so the output is the same as in example 1
mengj
guom
tj
Copy the code
Example 3:
We can also control the number of times we iterate through the for loop by declaring the scope of the list. The return is required to use {}. Inside the braces is the UNIx-compliant parameter range convention, i.e. {1.. 10} is 1-10.
# Echo range numbers
for num in{1.. 10};do
echo ${num}
done
Copy the code
Example 3 outputs results from 1 to 10. Note that echo on Bash is followed by the \n carriage return character by default
1, 2, 3, 4, 5, 6, 7, 8, 9, 10Copy the code
Example 4:
The list of for loops can also be an array of strings wrapped in ().
# Echo array
array=('mengj' 'guom' 'tj')
for item in ${array[@]}; do
echo ${item}
done
for item in ${array[*]}; do
echo ${item}
done
Copy the code
In example 4, we see the use of ${array[@]} and ${array[*]}, both of which represent arrays themselves
The content of the sample can be viewed from GitHub source code, portal.
while
The code format of while is similar to that of if, except that while can repeatedly execute logical judgments.
format
while [CONDITION]
do
[COMMANDS]
done
Copy the code
Example:
Execute the specified loop 10 times.
#! /bin/bash
counter=1
while [[ ${counter} -lt 10 ]]; do
echo ${counter}
((counter++))
done
Copy the code
View the source code in GitHub, portal.
until
The loop judgment of until is much simpler than that of while, which requires the test command to be executed before the command is executed.
Example:
#! /bin/bash
counter=1
until [[ ${counter} -gt 10 ]]; do
echo ${counter}
((counter++))
done
Copy the code
We can compare the “until” loop with the “while” loop, which uses a judgment condition, and the “until” loop, which uses a test command, with the output showing that the execution results of both commands are the same.
1, 2, 3, 4, 5, 6, 7, 8, 9, 10Copy the code
The sample source code for Until is available on GitHub, Portal.
Cycle control
The continue command skips the loop and starts the next loop. The continue command skips the loop and starts the next loop. Break literally means to break, so the command to break is to break the loop and execute what follows.
Example 1:
#! /bin/bash
Loop control by using a while loop and case with break
while true; do
echo "On which topic do you want advice?"
echo "1. politics"
echo "2. startrek"
echo "3. kernelnewbies"
echo "4. sports"
echo "5. bofh-excuses"
echo "6. magic"
echo "7. love"
echo "8. literature"
echo "9. drugs"
echo "10. education"
echo
echo -n "Enter your choice, or 0 for exit: "
read choice
echo
case $choice in
1)
echo 'politics'
;;
2)
echo 'startrek'
;;
3)
echo 'kernelnewbies'
;;
4)
echo 'sports'
;;
5)
echo 'bofh-excuses'
;;
6)
echo 'magic'
;;
7)
echo 'love'
;;
8)
echo 'literature'
;;
9)
echo 'drugs'
;;
10)
echo 'education'
;;
0)
echo "OK, see you!"
break
;;
*)
echo "Please choose whichever item you like from 0-10."
;;
esac
done
Copy the code
In this example, while and case are used to print the result of the user’s selection, and if 0 is entered, exit the loop by using break to view the source’s portal.
Example 2:
Use the continue command to print 0-10, skip the loop if it is divisible by 3 and print only numbers that are not divisible by 3.
Talk is easy, show my your code
#! /bin/bash
for i in{1.. 10};do
if[$((${i} % 3 )) -eq 0 ]; then
continue
fi
echo ${i}
done
Copy the code
The example code prints numbers 1-10 by using for, and skips the output with continue if it is a multiple of 3. The output looks like this:
1, 2, 4, 5, 7, 8, 10Copy the code
There is no 3, 6, 9 in the output result, you can check and download the source code in my GitHub to test oh, portal.