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 use while Loop in Shell

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article Xiaobian for you to introduce in detail "how to use the while loop in Shell", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use the while loop in Shell" can help you solve your doubts.

The while loop is the simplest loop in a Shell script. While repeatedly executes a set of statements when the condition is met, and exits the while loop when the condition is not met.

The while loop format is: while conditiondo commanddone

The following is a basic while loop, and the test condition is: if int is less than or equal to 5, then the condition returns true. Int starts at 0, and int is added by 1 each time the cycle is processed. Run the above script, return the numbers 1 to 5, and then terminate.

#! / bin/bashint=1while (($intdo echo $int let "int++" done

Run the script and output:

12345

The above example uses the Bash let command, which is used to execute one or more expressions. There is no need to add $to represent the variable in the variable calculation. For more information, please see: Bash let command

. The while loop can be used to read keyboard information. In the following example, the input information is set to the variable FILM and press to end the loop.

Echo 'Press exit' echo-n 'enter the name of your favorite website:' while read FILMdo echo 'Yes! $FILM is a good website "done"

Run the script with output similar to the following:

Press exit to enter the name of your favorite website: rookie tutorial yes! The rookie tutorial is an infinite loop of a good website.

Infinite loop syntax format:

While: do commanddone

Or

While truedo commanddone

Or

For ((;;)) read this, this article "how to use while loops in Shell" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

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: 230

*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

Development

Wechat

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

12
Report