Reputation: 983
I am new to linux scripting so apologies if this is a trivial question. I have written the following script. The script basically pings the ip addresses in 192.168.11.0 once and prints if the response has been successful or not.
#!/bin/bash
NETWORK=192.168.11
IP_START=101
IP_END=148
IP_COUNTER=$IP_START
while [[ $IP_COUNTER -le $IP_END ]]
do
ip=$NETWORK.$IP_COUNTER
ping -c1 $ip &>/dev/null && echo "$ip is UP" || echo "$ip is DOWN"
IP_COUNTER=$(($IP_COUNTER +1))
done
However I would like to have a 1-second delay between the ping and the "echo "$ip is UP"..." part. I am not sure how to do this. I would really appreciate it if someone could guide me in the right direction.
Upvotes: 0
Views: 4037
Reputation: 2111
Use sleep.
if ping -c1 $ip &>/dev/null ; then
sleep 1
echo $ip is up
else
sleep 1
echo $ip is down
fi
As I look at your code, maybe better matching your coding style will be
ping -c1 $ip &>/dev/null && sleep 1 && echo "$ip is up" || sleep 1 && echo "$ip is down"
"True" posix sleep will accept only integral values (sleep 1, sleep 2), but linux version of sleep can also handle non-integral (sleep 0.25 for 1/4 of second).
Upvotes: 2