In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
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 repeat a command in linux until it runs successfully. It is very detailed and has a certain reference value. Friends who are interested must read it!
To solve repetitive tasks, it is natural to think of cycles. In Shell, there are no more than three kinds of cycles: for, while, and until. In this article, we use the last two loops: while and until.
With regard to these two kinds of cycles, the partners who have the basis of programming should be more familiar with it, so review it again here.
For a while loop, it repeats the loop body (contained in do.) if the loop condition is true. In the done structure, until the loop condition becomes false.
For untile loops, in contrast to while loops, if the loop condition is false, it repeats the loop body (contained in the do. In the done structure, until the loop condition becomes true.
All right, after reviewing the basics, let's look at how to use these two loops to achieve our goal.
1. Use while Loop
Take repeatedly testing network connectivity as an example. We can use the following command to free our hands:
$while! Ping-c 3 baidu.com; do sleep 2; done; echo succeed
For this command, it will repeat the ping command, and if it does not succeed, sleep will continue to try after 2 seconds until it succeeds. After success, the word succeed will be printed on the terminal.
A brief introduction:
Let's unravel this command and then briefly introduce the role of each module.
$while! Ping-c 3 baidu.com
This is a loop condition, and our requirement is to "execute the loop body when the ping command fails," so we need to add a! before the ping.
$do sleep 2; done
This part is the circular body. When the cycle condition is established, it enters the loop body. In order not to let this program grab too many resources, we sleep in the loop for 2 seconds. Once the cycle condition is not established, jump out of the loop body.
$echo succeed
This part is the statement executed after jumping out of the loop body, just printing a succeed, of course, you can do other things as well.
two。 Use until Loop
We use the until loop to do the same thing, which is written as follows:
$until ping-c 3 baidu.com; do sleep 2; done; echo succeed
The effect is the same as above, except that when the loop condition is true, exit the loop.
Let's simply disassemble it.
$until ping-c 3 baidu.com
This part is the loop condition, it will execute the ping command, the result of the execution as the basis for judgment. Do not exit the loop until the ping command is executed successfully.
$do sleep 2
Circulatory body, sleep 2 seconds.
$echo succeed
What you do after success.
3. Repeat the previous command until successful
With a little extra expansion here, how to repeat the last command until it succeeds?
We know that to repeat the last command, you can use this command:
$!
So how to judge whether the last command was successful or not? We can judge by judging $? To determine whether the value of the
# while Loop Writing $!; while [$?-ne 0]; do!; done # until Loop Writing $until!; do:; done is all the contents of the article "how to repeat a command in linux until it runs successfully". Thank you for reading! Hope to share the content to help you, more related knowledge, 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: 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.