2

I am writing a simple bash script on Linux to ping some hosts, e.g.:

ping -c 1 google.com
ping -c 1 amazon.com
...

In my approach, I am loading the hosts from a separate file into an array and then loop over the elements. The problem occurs when calling the ping command with elements from the array. Executing the following script gives me an error message.

#!/bin/bash

IFS=$'\n' read -d '' -r hosts < hostnames.txt

for host in "${hosts[@]}"
do
    ping -c 1 ${host}
done

I guess there is something wrong with the syntax, but I couldn't figure it out yet.

8
  • You could also do this: while read -r hosts; do ping -c "$host"; done < hostnames provided the command in your while loop (ping in this example) does not consume/slurp stdin. Commented May 21, 2022 at 18:39
  • 1
    IFS=$'\n' read -d '' -r hosts < hostnames.txt: Use readarray/mapfile instead. mapfile -t hosts < hostnames.txt. Read more at help mapfile Commented May 21, 2022 at 18:44
  • Thank you for your answer anishane. Reading the file content into an array works fine, also with the mapfile command or readarray. I tried your suggestion but it gives me the same error message: name or service not known. Commented May 21, 2022 at 18:51
  • The error occurs when the ping command is invoked Commented May 21, 2022 at 18:53
  • 1
    The read command you have will not create an array, just a plain variable containing newline characters. Depending on how you check its contents, it's easy to be misled about what it contains. Use declare -p hosts to get a better idea what's actually being stored in the variable/array. Commented May 21, 2022 at 19:09

1 Answer 1

2

Your hostnames.txt was generated on a Windows machine?

Your hostnames have trailing \r characters, so the lookups fail.

Try this:

cp hostnames.txt hostnames.txt.bkp
dos2unix hostnames.txt.bkp hostnames.txt

And then run your script again.

If you don't have dos2unix installed and don't want to install it ... maybe you have tr already available. In that case this should do the trick, too:

tr -d '\r' < hostnames.txt.bkp > hostnames.txt
Sign up to request clarification or add additional context in comments.

6 Comments

Whoa! Finally. Thank your for your help sir, that solved my problem. Btw. I created the hostname.txt file under linux with Xed editor but it seems like it appends an \r for each line.
Sweet .. pleased to hear that. And I never heard of Xed, seems like the wrong tool to use on Linux ;)
Out of curiosity: which method of conversion did you end up using? @Debo
dos2unix did the job :)
If the Xed editor doesn't have a config option to only use Linux line endings of '\n', then time for a new editor. vim is available on most Linux systems and if you can remember "type 'i'" for insert mode and "hit [esc]" to return to command mode -- you can use vim. (in command mode :wq writes (saves) and quits)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.