gladius
gladius

Reputation: 91

Ping with a variable in linux script

I want that my script pings the ip-addresses 192.168.0.45 192.168.0.17 192.168.0.108 by doing this:

bash Script.sh 45 17 108

I want to give the last numbers with bash to ping this ip-addresses.

I don't know how I have to do this. Do I have to work with a 'case' in a do while or something else??

Upvotes: 1

Views: 2573

Answers (2)

Jahid
Jahid

Reputation: 22428

I want to give the last numbers with bash to ping this ip-addresses.

I presume, you want to ping the addresses simultaneously. In that case you can do this:

Script.sh:

#!/bin/bash
ping 192.168.0.$1 & ping 192.168.0.$2 & ping 192.168.0.$3 &

This will send all of the three ping commands to background where they will be executed simultaneously and print continuous output on terminal.

You can do this with a for loop too:

#!/bin/bash
for i in $*;do
ping 192.168.0.$i &
done

The for loop method can take any number of arguments

Upvotes: 1

pasaba por aqui
pasaba por aqui

Reputation: 3519

#!/bin/bash

for i in $*; do
  ping 192.168.0.$i
done

Upvotes: 3

Related Questions