-1

I have written this code to ping multiple IP addresses but it does not work. Can anyone please tell me what's wrong here?

#!/bin/bash
for i in 'seq 1 20' do  
    ping -c 1 "10.88.209.$i";
done

ps: the error it is showing is => ./ping.sh: line 3: syntax error near unexpected token ping' ./ping.sh: line 3: ping -c 1 "10.88.209.$i";'

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
Rishabh Gour
  • 170
  • 1
  • 13

2 Answers2

3

Use:

for i in $(seq 1 20); do

or

for i in {1..20}; do

or

for ((i=0;i<=20;i++)); do

obsolete:

for i in `seq 1 20`; do
Cyrus
  • 84,225
  • 14
  • 89
  • 153
2

Your syntax is broken. Single quotes and back-ticks are not interchangeable, and for-loops require a semicolon or newline before the do keyword. For example:

#!/usr/bin/env bash

for i in `seq 1 20`; do
    ping -c 1 "10.88.209.${i}"
done

Rather than spawning seq, you can also use the more efficient (and more idiomatic) Bash brace-expansion sequence. For example:

#!/usr/bin/env bash

for i in {1..20}; do
    ping -c 1 "10.88.209.${i}"
done
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199