0

I have a piece of a script that is not working right. I make an array from a host list, then use a for loop to ping each host in the array. In the test host list I have localhost, 127.0.0.1, and 10.3.2.1. The point of this is to test that host names will work as well as IP addresses, and that the localhost and loop back will work and 10.3.2.1 will fail because 10.3.2.1 doesn't exist.

When I run the script, the ping command only runs against 10.3.2.1, and does nothing for localhost and 127.0.0.1. If I throw in an echo statement to see if all elements in the array are being read, it returns all 3 hosts.

declare -a Hosts
Hosts=$(grep "^[^#;]" ../Configuration/HostList

for h in "${Hosts[@]}"
do
ping -c1 $h
done

Again, when this runs, it only pings 10.3.2.1. I need it to ping localhost, 127.0.0.1, and 10.3.2.1.

Ken
  • 125
  • 1
  • 12
  • 3
    That's not an array after the assignment. See [this answer](https://stackoverflow.com/questions/9449417/how-do-i-assign-the-output-of-a-command-into-an-array/9449633#9449633). In most cases you want a `while read` loop instead of outputting a command into an array. – jordanm Sep 16 '21 at 19:41
  • 1
    Or, rather, you're putting everything in just one array element, the first one (index 0). – Charles Duffy Sep 16 '21 at 19:44
  • Barmar, thanks for letting me know, that was a typo in the question; it doesn't exist in my script. – Ken Sep 16 '21 at 19:48
  • Thanks, I was able to get it to do what I wanted with jordanm and Duffy's comments! – Ken Sep 16 '21 at 20:14

0 Answers0