Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a couple ideas on how I would achieve this. Not sure how I would script it.

Method 1: (probably the better choice)

Create a loop that pings a server until reply is received then execute command if no reply is received in X amount of time/runs continue script.

Method 2:

Check if network interface has valid IP then continue script

How would one go about adding this functionality in a script. Would awk or grep be of use in a situation like this? Thank you in advance for ANY input.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

This command should wait until it can contact google or it has tried 50 times:

for i in {1..50}; do ping -c1 www.google.com &> /dev/null && break; done

The for i in {1..50} loops 50 times or until a break is executed. The ping -c1 www.google.com sends 1 ping packet to google, and &> /dev/null redirects all the output to null, so nothing is outputed. && break executes break only if the previous command finished successfully, so the loop will end when ping is successful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...