Considerations for using while loop ssh in the shell

Source: Internet
Author: User

Need to read a text, the second text each line contains an IP in the while loop using SSH, but after the first line of SSH exited, how to avoid automatically reading a line out of the while loop, this article will explain its reasons in detail.

Recently wrote an automatic update shell, but found that if you use the while loop to read an IP address from a file, then access can only read the first row of records. The code is as follows:








But if using for is not the problem, it is very strange, for the code is as follows:








Isn't it possible that the for and while have such a big difference? After some experiments, it is not the difference between the for of while, but the use of the redirection mechanism in the while, the information in the data file has been read into and redirected to the entire while statement. So when we call the Read statement again in the while loop, we read the next record. The problem here is that the SSH statement is just going to read everything in the input, and the shell below is a good illustration of the problem:








Executing the shell will find that the cat statement in SSH will print out the other records in the data file, which will cause the call to play the SSH statement, the input cache has been read, and when the read statement is read again of course will not read the record, the loop is exited. The improved method is to redirect input from SSH into:







In this way, the SSH statement can be executed smoothly in the while statement. The redirection of Linux is really complicated.


Reference post: Http://leeon.me/a/shell-while-ssh

Considerations for using while loop ssh in the shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.