bash while loop read only one line

霸气de小男生 提交于 2020-01-04 05:36:31

问题


I have issue with this code.

  while IFS='' read -r line
  do

    echo "host: "$line

    IP_addr=$(echo $line |cut -d" " -f1)
    host=$(echo $line | cut -d" " -f2)

    FILE_CHECK=$(ssh -o ConnectTimeout=10 $USER@$IP_addr "find $SRC_FILE -mtime 0")

    echo $FILE_CHECK

    ((host_num+=1))


    echo "" #empty line
  done < "$SERVER_LIST"

  echo $host_num

Where variable server list contain path to file with server

192.168.1.1 app1
192.168.1.2 app2
192.168.1.3 app3 

Script read first line, successfully connect to host and then exit. When I tried loop through file and only echo lines loop read all lines.


回答1:


ssh reads from stdin, so it will eat the rest of the file. Run ssh with the -n option to redirect its input to /dev/null.



来源:https://stackoverflow.com/questions/49508612/bash-while-loop-read-only-one-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!