0

For some reason, this script (run on RHEL v6.9) processes only the first line in the host_file

#!/bin/bash

process()
{
 ssh $host ls
}

while IFS= read -r host
do
  echo "Running $host"
  process
  echo "DONE $host"
done < host_file
#-------End of Script-----------------

$ cat host_file
server1
server2
humility
  • 143
  • Found the answer - https://unix.stackexchange.com/questions/24260/reading-lines-from-a-file-with-bash-for-vs-while?rq=1 – humility Aug 31 '18 at 21:37

1 Answers1

1

ssh will slurp up the rest of stdin. Use a different file descriptor for the while-read loop:

while IFS= read -u4 -r host
do
  echo "Running $host"
  process
  echo "DONE $host"
done 4< host_file
glenn jackman
  • 85,964
  • -u is not POSIX http://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html - better would be <&4 – Zombo Aug 31 '18 at 21:44