Skip to content

while read skips the last machine if machines.txt has no empty line at the end #892

@Justin-De-Sio

Description

@Justin-De-Sio

Hi,

When running:

while read IP FQDN HOST SUBNET; do
  ssh -n root@${IP} hostname
done < machines.txt

I expected this output:

server
node-0
node-1

But I only got:

server
node-0

Cause: machines.txt had no blank line at the end. The read command skips the last line if it doesn’t end with a newline (\n).

Fix: Add an empty line after the last entry in machines.txt.

Example:

XXX.XXX.XXX.XXX server.kubernetes.local server
XXX.XXX.XXX.XXX node-0.kubernetes.local node-0 10.200.0.0/24
XXX.XXX.XXX.XXX node-1.kubernetes.local node-1 10.200.1.0/24

I'm not sure what is the best solution to avoid confusion here. Maybe include a note in the tutorial about ending the file with a new line or just rework the bash script.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions