27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

while [ $TRUE ] #Endless loop.<br />

do<br />

tail −$CHECK_LINES $LOGFILE> $TEMPFILE<br />

# Saves last 100 lines of system log file as temp file.<br />

# Necessary, since newer kernels generate many log messages at log on.<br />

search=`grep $KEYWORD $TEMPFILE`<br />

# Checks for presence of the "IP address" phrase,<br />

# indicating a successful logon.<br />

if [ ! −z "$search" ] # Quotes necessary because of possible spaces.<br />

then<br />

echo "On−line"<br />

rm −f $TEMPFILE<br />

exit $ONLINE<br />

else<br />

echo −n "."<br />

fi<br />

sleep 1<br />

done<br />

# Clean up temp file.<br />

# −n option to echo suppresses newline,<br />

# so you get continuous rows of dots.<br />

# Note: if you change the KEYWORD variable to "Exit",<br />

# this script can be used while on−line to check for an unexpected logoff.<br />

# Exercise: Change the script, as per the above note,<br />

# and prettify it.<br />

exit 0<br />

# Nick Drage suggests an alternate method:<br />

while true<br />

do ifconfig ppp0 | grep UP 1> /dev/null && echo "connected" && exit 0<br />

echo −n "." # Prints dots (.....) until connected.<br />

sleep 2<br />

done<br />

# Problem: Hitting Control−C to terminate this process may be insufficient.<br />

# (Dots may keep on echoing.)<br />

# Exercise: Fix this.<br />

# Stephane Chazelas has yet another alternative:<br />

CHECK_INTERVAL=1<br />

while ! tail −1 "$LOGFILE" | grep −q "$KEYWORD"<br />

do echo −n .<br />

sleep $CHECK_INTERVAL<br />

done<br />

echo "On−line"<br />

# Exercise: Discuss the strengths and weaknesses<br />

# of each of these various approaches.<br />

Chapter 30. Debugging 323

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!