kingcoin
kingcoin

Reputation: 69

Bash for loop only does one iteration

I cannot figure out what is going on here:

#! /bin/bash
set -e

for i in {0..24}
do
  echo "ping -c 1 10.11.1.$i"
  ping -c 1 10.11.1.$i
done

echo "done"

If I run this, it pings the first host and quits. Strangely, I don't even see the final "done" outside of the loop. If I remove the ping command, it prints everything as expected.

Can anyone tell me why this won't run? Bash version 5.0.16(1)-release from $BASH_VERSION

Upvotes: 0

Views: 739

Answers (2)

kingcoin
kingcoin

Reputation: 69

Remove set -e from script to continue even if ping reports packet loss.

Upvotes: 2

Rattaj
Rattaj

Reputation: 11

change to:

for i in $(seq 0 24);

example:

#!/bin/bash
for ip in $(seq 200 210); do
ping -c 1 192.168.31.$ip | grep "bytes from" | cut -d ' " -f4 | cut -d ":" -f1 &
done

Upvotes: 0

Related Questions