问题
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