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.
Sometimes we want to get some inputs from the user through the console. We can use input() function to achieve this. In this case, the program will wait indefinitely for the user input. Once the user provides the input data and presses the enter key, the program will start executing the next statements.
$! is an internal Bash variable that stores the PID of the last job run in the background. In this example, that is the PID of the sleep command. We're storing the PID in a variable ( process_id ). Prints the PID number. The PID is passed to the wait command that waits until the sleep command completes.
wait is a built-in command of Linux that waits for completing any running process. wait command is used with a particular process id or job id. When multiple processes are running in the shell then only the process id of the last command will be known by the current shell.
The bash wait command is a Shell command that waits for background running processes to complete and returns the exit status. Unlike the sleep command, which waits for a specified time, the wait command waits for all or specific background tasks to finish.
You can do the following
until ifconfig -l | grep ppp0 >/dev/null 2>&1; do :; done
Works on MacOS
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With