Considerations for using while loop ssh in shell 2

Source: Internet
Author: User
In shell, when using the while loop ssh, note that you need to read a text. each line of the text contains an IP address and ssh is used in the while loop, however, ssh exits after the first line. This article will explain in detail how to avoid the while loop from automatically reading a row. Www.2cto.com...
In shell, when using the while loop ssh, note that you need to read a text. each line of the text contains an IP address and ssh is used in the while loop, however, ssh exits after the first line. This article will explain in detail how to avoid the while loop from automatically reading a row. Www.2cto.com recently writes an automatically updated shell. However, if the while loop is used to read the IP address from a file, then the access can only read the first row of records. The code is as follows: java code while read LINE do echo ********************************* * ********* $ LINE ssh 192.168.10.233 ls done <data, but if you use, the for code is as follows: java code for LINE in 'cat data' do echo ***************************** * ************* $ LINE ssh 192.168.10.233 ls done is so different between for and while, impossible. After some experiments, we found that it was not the difference between the while for, but the redirection mechanism used in the while. The information in the data file has been read and redirected to the entire while statement. So when we call the read statement again in the while loop, the next record will be read. The problem is that the ssh statement exactly reads everything in the input. the shell below can explain this problem very well: java code while read LINE do echo ********************************* * ********* $ LINE ssh 192.168.10.233 cat done <data to execute this shell, you will find that, the cat statement in ssh prints other records in the data file, which causes the input cache to be read after the ssh statement is called, when the read statement is read again, the record cannot be read, and the loop exits. The improved method is to redirect the ssh input: java code while read LINE do echo ****************************** * ************ $ LINE ssh 192.168.10.233 ls </dev/null done <data, you can run the ssh statement smoothly in the while statement. Linux redirection is really complicated.

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.