In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "the difference between while read line and for cycle in shell". In daily operation, I believe that many people have doubts about the difference between while read line and for cycle in shell. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the difference between while read line and for cycle in shell". Next, please follow the editor to study!
[background]
There are situations where you want to get the ip port from an ip list and then ssh ip to the target machine for specific operations, but the script process uses while read line to read the ip list and exits the script after only the first ip is read in the while loop.
[introduction]
Before explaining the problems encountered above, you can see the test comparison between for and while, and the ip in this article has been modified.
Click (here) to collapse or open
# / bin/bash
IPS= "10.1.1.10 3001
10.1.1.10 3003
10.1.1.11 3001
10.1.1.11 3002
10.1.1.11 3004
10.1.1.11 3005
10.1.1.13 3002
10.1.1.13 3003
10.1.1.13 3004
10.1.1.14 3002 "
Echo "= = while test ="
ITunes 0
Echo $IPS | while read line
Do
Echo $(($iTun1))
Echo $line
Done
Echo "= = for test ="
Nasty 0
For ip in $IPS
Do
Nasty $(($nasty 1))
Echo $ip
Echo $n
Done
The output is as follows:
= while test =
one
10.1.1.10 3001 10.1.1.10 3003 10.1.1.11 3001 10.1.1.11 3002 10.1.1.11 3004 10.1.1.11 3005 10.1.1.13 3002 10.1.1.13 3003 10.1.1.13 3004 10.1.1.14 3002
= for test =
10.1.1.10
one
3001
two
10.1.1.10
three
3003
four
10.1.1.11
five
3001
six
10.1.1.11
....
As can be seen from the example, while read line reads information and assigns values to line at a time, while for reads a string with a space as a separator at a time.
[reason]
Using the redirect mechanism in while, all information in IPS is read in and redirected to the line variable in the entire while statement. So when we call the read statement again in the while loop, we read the next record. The problem is that the last line in $line has been read, and the next line cannot be obtained, thus exiting the while loop.
[solution]
1 use ssh-n "command"
2 ssh "cmd" < / dev/null redirects the input of ssh.
At this point, the study of "the difference between while read line and for cycle in shell" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.