Problem solving in shell that only loops the first row when using while loop ssh

Source: Internet
Author: User

Recently writing a shell script, I would like to use a For loop to read the contents of the file, but found that every line in the file has a space, obviously with a for loop does not work, my intention is to read a row of the file content, so decisive use while loop to achieve, but the problem comes, The reason is explained in detail when SSH exits after the first line and does not succeed, and how to avoid automatically reading a line and jumping out of the while loop.

The contents of the file are as follows:
[Email protected]_hai~] #cat 2.txt
SCJZ 2144 10.16.100.113 89
SCJZ 2144 10.16.100.114 90

Some of the code is as follows:
#!/bin/bash
Serverdir=/data/scjz_server
Iplist=$1
Port=22
num=3300
Echo-e "\e[32m ' cat $IpList ' \e[0m"
Echo-e "\033[31m whether to operate the above Server game service, please confirm (yes or no): \033[0m"
Read confirm
If ["$confirm" = = "Yes"];then
While Read line
Do
Platform= ' echo $line |awk ' {print $} '
Ip= ' echo $line |awk ' {print $} '
Id= ' echo $line |awk ' {print $4} '
Server= ' ssh-p $Port $Ip "CD $ServerDir;/bin/ls-d *_${platform}_s${id}" '
Index= ' echo $Server |awk-f_ ' {print $} '
mysqlport=$ (expr $index + $Num)
Echo $Ip $Server
Done < $
Fi

After some experimentation, it was found that the redirect mechanism was used in the while and the information in the 2.txt file was 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 back to read everything in the input, which results in the completion of the SSH statement, the input cache has been read out, when read the reading statement of course also can not read the record, the loop is exited.

The improved method is to modify the SSH line, and then add the redirect input after SSH:

The correct code is as follows:
#!/bin/bash
Serverdir=/data/scjz_server
Iplist=$1
Port=22
num=3300
Echo-e "\e[32m ' cat $IpList ' \e[0m"
Echo-e "\033[31m whether to operate the above Server game service, please confirm (yes or no): \033[0m"
Read confirm
If ["$confirm" = = "Yes"];then
While Read line
Do
Platform= ' echo $line |awk ' {print $} '
Ip= ' echo $line |awk ' {print $} '
Id= ' echo $line |awk ' {print $4} '
Server= ' ssh-p $Port $Ip "CD $ServerDir;/bin/ls-d *_${platform}_s${id}" </dev/null "
Index= ' echo $Server |awk-f_ ' {print $} '
mysqlport=$ (expr $index + $Num)
Echo $Ip $Server
Done < $
Fi

In this way, the SSH statement can be executed smoothly in the while statement.

This article is from the "Rookie Fighter" blog, please be sure to keep this source http://linushai.blog.51cto.com/4976486/1678317

Problem solving in shell that only loops the first row when using while loop ssh

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.