No matter what computer language, looping is a topic that cannot be avoided, and Shell is certainly no exception. Here is a summary of some common Shell script loop related knowledge points, novice friends can refer to.
The for loop
The simplest loop in a Shell script is the for loop. The simplest for loop is like this, where you simply write the values of the variables after in:
#! /bin/bash for num in 1 2 3 4 do echo $num doneCopy the code
If the loop is a series of letters or numbers in the alphabet, the script can be written using the following syntax:
#! /bin/bash for x in {a.. z} do echo $x doneCopy the code
The while loop
In addition to the for loop, the Shell also provides a while loop. In other languages, if you have seen a for loop but not a while loop, you must be learning a fake language.
In a while loop, each time the loop is done, the condition is evaluated to determine whether the loop should continue. In fact, the for loop is almost as good as the while loop when the number of loops is small, but when the number of loops is large, say 100,000, the advantage of the while loop becomes apparent.
#! /bin/bash n=1 while [ $n -le 4 ] do echo $n ((n++)) doneCopy the code
Cycle jacket cycle
Like other high-level languages, loops can be nested with each other. For example, we put a for loop inside the while loop:
#! /bin/bash n=1 while [ $n -lt 6 ] do for l in {a.. d} do echo $n$l done ((n++)) doneCopy the code
The result of this script execution should be 1A, 1b, 1c, 1D, 2a, 2b… 5 d.
The content of the loop changes
In the for loop we mentioned above, the values assigned to the loop variables are listed after the in. But this is too flexible, because in many cases, the value of the loop variable is not fixed.
For example, there is a variable to get all the users on the current system, but because each computer user is different, there is no way to write this variable to death.
In this case, we can use the ls command to list all the users in the /home directory, and then use the loop variable to fetch them in turn. The complete code is as follows:
#! /bin/bash for user in `ls /home` do echo $user doneCopy the code
Of course, the Shell supports other commands besides ls. For example, we can use the date command to get the current system time and print it in sequence:
$ for word in `date`
> do
> echo $word
> done
Thu
Apr
9
08:12:09
CST
2020Copy the code
Variable value check
When we use a while loop, we often need to determine whether the value of a variable is greater than or less than a certain number. Sometimes that number is also represented by another variable, so we need to determine if the value of that variable is a number. There are three ways to judge:
#! /bin/bash echo -n "How many times should I say hello? " read ans if [ "$ans" -eq "$ans" ]; then echo ok1 fi if [[ $ans = *[[:digit:]]* ]]; then echo ok2 fi if [[ "$ans" =~ ^[0-9]+$ ]]; then echo ok3 fiCopy the code
The first method may seem like nonsense, but in fact, -eq can only be used to judge between values and fails if it is a string, so this ensures that ANS is a numeric variable.
The second approach is to use the Shell’s wildcards directly to determine variables.
The third method is more straightforward, using regular expressions to determine variables.
Let’s go straight to an example:
#! /bin/bash echo -n "How many times should I say hello? " read ans if [ "$ans" -eq "$ans" ]; then n=1 while [ $n -le $ans ] do echo hello ((n++)) done fiCopy the code
In this script, I pass in the ANS variable the number of times I’m going to loop, and then the script prints Hello exactly how many times. To ensure that what we pass is a number, we use the if [“$ans” -eq “$ans”] statement. If we pass in something other than a number, we don’t enter the while loop.
2020 selected Ali/Tencent and other front-line big factory interview, resume, advanced, e-book public number “Liang Xu Linux” background reply “information” free access
Looping out the text file content
If you want to loop through the contents of a text file line by line, do this:
#! /bin/bash echo -n "File> " read file n=0 while read line; do ((n++)) echo "$n: $line" done < $fileCopy the code
Here, we use the read command to read the contents of the text file into the file variable, and then use redirection (the last line of the script above) to pass the contents of the file in turn into a while loop for processing and printing.
Infinite loop
Sometimes we need to keep doing something forever, so we can use an infinite loop. Doing this is as simple as using while true.
#! /bin/bash while true do echo -n "Still running at " date sleep 1 doneCopy the code
In the above script, the exact time of Still running will be printed every second until you press Ctrl + C to terminate the script.
All you see is true love. Why don’t you like it before you leave? Your “sanlian” is the biggest motivation for liang Xu’s continuous creation!
- Pay attention to the original public number “good Xu Linux”, the first time to get the latest Linux dry goods!
- Public number background reply [information] [interview] [resume] to get selected first-line big factory interview, self-improvement, resume and other information.
- Follow my blog: lxLinux.net