LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-17-2006, 08:07 AM   #1
drakebasher
Member
 
Registered: Aug 2004
Location: Proprio Qui
Distribution: Debian
Posts: 85

Rep: Reputation: 16
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-
 
Old 02-17-2006, 08:51 AM   #2
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
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.
 
Old 02-17-2006, 12:04 PM   #3
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
Put sleep inside your loop. Man sleep.
 
Old 02-17-2006, 09:22 PM   #4
drakebasher
Member
 
Registered: Aug 2004
Location: Proprio Qui
Distribution: Debian
Posts: 85

Original Poster
Rep: Reputation: 16
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~
 
Old 02-18-2006, 09:00 AM   #5
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
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.

Last edited by alienDog; 02-18-2006 at 09:03 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash-script won't wait for application to finish TLV Linux - Software 24 10-01-2004 12:18 AM
while I wait for bash programming jay2901 Programming 4 08-07-2004 08:23 AM
What would happen if... d1s4st3r General 4 06-27-2004 08:12 AM
rm -rf / What would actually happen? TheOneAndOnlySM General 22 03-29-2004 07:51 PM
wait in bash shells raypen Linux - Software 1 04-18-2003 03:19 PM


All times are GMT -5. The time now is 04:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration