LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to get bash to wait for something to happen? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-get-bash-to-wait-for-something-to-happen-416453/)

drakebasher 02-17-2006 07:07 AM

How to get bash to wait for something to happen?
 
I have dial-up Internet access. I want to have a script do the following:

>Dialout to get ppp0 up
>Once ppp0 is up, run another script

This is what I've done: it works, but I don't like much:
Code:

#!/bin/bash
# User initiation of ppp0 Internet connection and setup of iptables
# Clear variable PTP
PTP=

while [ "$PTP" != "255.255.255.255" ]
do
  PTP=$( /sbin/ifconfig ppp0 | grep 'inet addr:' | gawk -F: '{ print $4 }' )
done

# Get present IP address:
THISIP=$( /sbin/ifconfig ppp0 | grep 'inet addr:' | gawk -F: '{ print $2 }' | gawk '{ print $1 }' )

# Setup iptables with new IP address
iptables -t mangle -F
iptables -t nat -F
iptables -t filter -F

/sbin/route del default
/sbin/route add default gw $THISIP

#*mangle
/sbin/iptables -t mangle -P PREROUTING ACCEPT
/sbin/iptables -t mangle -P INPUT ACCEPT
/sbin/iptables -t mangle -P FORWARD ACCEPT
...followed by all the other rules, some of which need the $THISIP variable...

This works, but the while command gets exectued as fast as it can until ppp0 comes up, maybe 100 times! I'd rather have it execute every 3 seconds or so and then I could do even better by exiting if I don't get ppp0 with 10 tries. I looked at 'watch' but I don't know how I could use that: maybe redirect the output to an if loop? But how is that done?

Thanks-

nx5000 02-17-2006 07:51 AM

Have a look at /etc/ppp/ip-up, pppd launches this script when ppp0 is active

Otherwise as a general question how to make bash wait for an event, I don't know. I use the same solution for different events but you should add sleep 1 in the while loop.

alienDog 02-17-2006 11:04 AM

Put sleep inside your loop. Man sleep.

drakebasher 02-17-2006 08:22 PM

Great work, guys! Sleep is what I asked for, but thanks, nx5000, I think /etc/ppp/ip-up.d is what I should look at. Another brick in the wall.

Cheers~

alienDog 02-18-2006 08:00 AM

Quote:

and then I could do even better by exiting if I don't get ppp0 with 10 tries.
For this you need a counter inside the loop, one way to do it is with expr:

Code:

COUNTER=`expr COUNTER + 1`
if [ $COUNTER -gt 10 ]; then
  echo "No connection."
  exit 1
fi

Man expr gives you a detailed explanation on how expr works.


All times are GMT -5. The time now is 07:55 PM.