Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Loop in Shell script

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to achieve the cycle in the Shell script, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

For cycle

The simplest loop in a Shell script is the for loop. Anyone with a programming foundation should have used the for loop. The simplest for loop is shown below, where you simply write the values of the variables after the in:

#! / bin/bash for num in 1 2 3 4 do echo $num done

If the content you want to loop is consecutive letters or consecutive numbers in the alphabet, you can write the script in the following syntax:

#! / bin/bash for x in {a.. z} do echo $x done

While cycle

In addition to for loops, Shell also provides while loops. For other languages, if you've seen for loops but not while loops, you must have learned a fake language.

In a while loop, each time a loop is performed, the condition is judged to determine whether the loop should continue. In fact, in the case of a small number of cycles, the effect of the for loop is similar to that of the while loop, but if there are more cycles, such as 100,000, then the advantage of the while loop is reflected.

#! / bin/bash nasty 1 while [$n-le 4] do echo $n ((nasty +)) done

Cycle sleeve cycle

Like other high-level languages, loops can be nested within each other. For example, in the following example, we insert another for loop into the while loop:

#! / bin/bash nasty 1 while [$n-lt 6] do for l in {a.. d} do echo $naughl done ((nasty +)) done

The result of this script execution should be 1a, 1b, 1c, 1d, 2a, 2b. 5d.

The content of the loop is changing.

The for loop we mentioned above, the values to be assigned to the loop variables are listed in the list after in. But this is too inflexible, 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 loop variables to get them in turn. The complete code is as follows:

#! / bin/bash for user in `ls / Home` do echo $user done

Of course, Shell supports other commands besides ls. For example, we can use the date command to get the current system time, and then print it out in turn:

$for word in `date` > do > echo $word > done Thu Apr 9 08:12:09 CST 2020

Variable value check

When using while loops, we often need to determine whether the value of a variable is greater or less than a certain number. Sometimes this number is also represented by another variable, so we need to determine whether the value of this 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 fi

The first method may seem like nonsense, but in fact,-eq can only be used to judge between values, and if it is a string, the judgment will not pass, so this ensures that ans is a numeric variable.

The second method is to judge variables directly using Shell wildcards.

The third method is more straightforward, using regular expressions to judge variables.

Let's look directly at an example:

#! / bin/bash echo-n "How many times should I say hello?" Read ans if ["$ans"-eq "$ans"]; then nasty 1 while [$n-le $ans] do echo hello ((nasty +)) done fi

In this script, I pass the number of times I loop to the ans variable, and then the script prints the hello several times. To ensure that what we pass in is a number, we use the if ["$ans"-eq "$ans"] statement to determine. If we do not pass in a number, we will not enter the while loop.

Loop out the text file contents

If you want to cycle through the contents of the text file by line, you can do this:

#! / bin/bash echo-n "File >" read file name0 while read line; do ((nasty +)) echo "$n: $line" done < $file

Here, we use the read command to store the contents of the text file in the file variable, and then use redirection (the last line of the script above) to pass the file contents into the while loop and print them out.

Endless cycle

Sometimes we need to do something in a loop all the time, so we can use an endless loop. It's easy to do this, just use while true.

#! / bin/bash while true do echo-n "Still running at" date sleep 1 done

In the above script, the specific time of Still running at will be printed every 1 second until you press Ctrl + C to terminate the script.

Thank you for reading this article carefully. I hope the article "how to achieve a loop in a Shell script" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report