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 Shell script while, until Loop statement

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use Shell script while, until loop sentence", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "how to use Shell script while, until loop sentence" bar!

I. while cycle

The while loop is used to execute a series of commands continuously, as well as to read data from an input file; commands are usually test conditions. Its format is:

The code is as follows:

While command

Do

Command1

Command2

...

CommandN

Done

After the command is executed, the control returns to the top of the loop, starting from scratch until the test condition is false.

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

The code is as follows:

COUNTER=0

While [$COUNTER-lt 5]

Do

COUNTER='expr $COUNTER+1'

Echo $COUNTER

Done

Run the script and output:

one

two

three

four

five

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.

The code is as follows:

Echo 'type to terminate'

Echo-n 'enter your most liked film:'

While read FILM

Do

Echo "Yeah! great film the $FILM"

Done

Run the script with output similar to the following:

Type to terminate

Enter your most liked film: Sound of Music

Yeah! Great film the Sound of Music

2. Until cycle

The until loop executes a series of commands until the condition is true. The until loop and the while loop are handled in the opposite way. While loops are generally better than until loops, but until loops are more useful at some times-and only in rare cases.

The until loop format is:

The code is as follows:

Until condition

Command1

Command2

...

CommandN

Done

The condition can be any test condition, and the test occurs at the end of the loop, so the loop is executed at least once-note this.

Thank you for your reading, the above is "how to use Shell script while, until loop sentence" content, after the study of this article, I believe you on how to use Shell script while, until loop statement this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Development

Wechat

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

12
Report