Guys,

I am new to shell script, and want to learn more about it. i have got one script to report on status of ADSL connection. i can understand half of it (or less then half). can you guys please help me out in this.

Here is the script,
-------------------------------------------------------------------------------------

CONFIG="$1"
if [ -z "$CONFIG" ] ; then
get_device
[ -z "$CONFIG" ] && CONFIG=/etc/ppp/pppoe.conf
fi
if [ ! -f "$CONFIG" -o ! -r "$CONFIG" ] ; then
echo "$0: Cannot read configuration file '$CONFIG'" >& 2
exit 1
fi
. $CONFIG
PPPOE_PIDFILE="$PIDFILE.pppoe"
PPPD_PIDFILE="$PIDFILE.pppd"
if [ "$DEMAND" != "no" ] ; then
echo "Note: You have enabled demand-connection; adsl-status may be inaccurate."
fi
# If no PPPOE_PIDFILE, connection is down, unless we're using the Linux plugin
if [ "$LINUX_PLUGIN" = "" ] ; then
if [ ! -r "$PPPOE_PIDFILE" ] ; then
echo "adsl-status: Link is down (can't read pppoe PID file $PPPOE_PIDFILE)"
exit 1
fi
fi
# If no PPPD_PIDFILE, something fishy!
if [ ! -r "$PPPD_PIDFILE" ] ; then
echo "adsl-status: Link is down (can't read pppd PID file $PPPD_PIDFILE)"
exit 1
fi
PPPD_PID=`cat "$PPPD_PIDFILE"`
# Sigh. Some versions of pppd put PID files in /var/run; others put them
# in /etc/ppp. Since it's too messy to figure out what pppd does, we
# try both locations.
for i in /etc/ppp/ppp*.pid /var/run/ppp*.pid ; do
if [ -r $i ] ; then
PID=`cat $i`
if [ "$PID" = "$PPPD_PID" ] ; then
IF=`basename $i .pid`
netstat -rn | grep " ${IF}\$" > /dev/null
# /sbin/ifconfig $IF | grep "UP.*POINTOPOINT" > /dev/null
if [ "$?" != "0" ] ; then
echo "adsl-status: Link is attached to $IF, but $IF is down"
exit 1
fi
echo "adsl-status: Link is up and running on interface $IF"
/sbin/ifconfig $IF
exit 0
fi
fi
done
echo "adsl-status: Link is down -- could not find interface corresponding to"
echo "pppd pid $PPPD_PID"
exit 1

-------------------------------------------------------------------------------------

If possible then please explain each line of the script. that what it does.

Any reply would be highly appreciated.

You missed this

Our Software Development forum category encompasses topics related to application programming and software design. When posting programming code, encase it in code-tags, (code=syntax), where 'syntax' is any language found within our Code Snippets section, or (icode), for inline code, bbcode tags. Also, to keep DaniWeb a student-friendly place to learn, don't expect quick solutions to your homework. We'll help you get started and exchange algorithm ideas, but only if you show that you're willing to put in effort as well.
Our Shell Scripting forum is the place for Q&A-style discussions related to *nix shell scripting languages such as bash. Note that we also have a separate Perl forum within the Software Development category.

And this
Read before posting

Also, to save us explaining what you already know, tell us what you think you know about the bits you think you know about

  • it tells us you've done something
  • it tells us how much you really know
  • it tells us which bits to concentrate on.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

@salem

Thanks for your reply. this is what i have understood so far

#$1 is the command line argument that is passed to shell script
CONFIG="$1"

#checking if string is empty or not
if [ -z "$CONFIG" ] ; then
get_device
#again checking if the string is empy, if yes then setting value of CONFIG to /etc/ppp....
[ -z "$CONFIG" ] && CONFIG=/etc/ppp/pppoe.conf
fi

#checking if either file pppoe.conf doesn't exists or is not readable
# -f checks for file existence, -o is or, -r is checks if file is readabel, ! negates the output
if [ ! -f "$CONFIG" -o ! -r "$CONFIG" ] ; then
echo "$0: Cannot read configuration file '$CONFIG'" >& 2
exit 1
fi


. $CONFIG
PPPOE_PIDFILE="$PIDFILE.pppoe"
PPPD_PIDFILE="$PIDFILE.pppd"
if [ "$DEMAND" != "no" ] ; then
echo "Note: You have enabled demand-connection; adsl-status may be inaccurate."
fi
# If no PPPOE_PIDFILE, connection is down, unless we're using the Linux plugin
if [ "$LINUX_PLUGIN" = "" ] ; then
if [ ! -r "$PPPOE_PIDFILE" ] ; then
echo "adsl-status: Link is down (can't read pppoe PID file $PPPOE_PIDFILE)"
exit 1
fi
fi
# If no PPPD_PIDFILE, something fishy!
if [ ! -r "$PPPD_PIDFILE" ] ; then
echo "adsl-status: Link is down (can't read pppd PID file $PPPD_PIDFILE)"
exit 1
fi
PPPD_PID=`cat "$PPPD_PIDFILE"`
# Sigh. Some versions of pppd put PID files in /var/run; others put them
# in /etc/ppp. Since it's too messy to figure out what pppd does, we
# try both locations.
for i in /etc/ppp/ppp*.pid /var/run/ppp*.pid ; do
if [ -r $i ] ; then
PID=`cat $i`
if [ "$PID" = "$PPPD_PID" ] ; then
IF=`basename $i .pid`
netstat -rn | grep " ${IF}\$" > /dev/null
# /sbin/ifconfig $IF | grep "UP.*POINTOPOINT" > /dev/null
if [ "$?" != "0" ] ; then
echo "adsl-status: Link is attached to $IF, but $IF is down"
exit 1
fi
echo "adsl-status: Link is up and running on interface $IF"
/sbin/ifconfig $IF
exit 0
fi
fi
done
echo "adsl-status: Link is down -- could not find interface corresponding to"
echo "pppd pid $PPPD_PID"
exit 1


I would really appreciate if you explain me rest of the script.

There's just no helping some people.
What part of USE CODE TAGS is confusing you?

Does it look something like this when viewed on your system?
Specifically, note the indentation which helps you figure out the flow of the code.

if [ "$LINUX_PLUGIN" = "" ] ; then
  if [ ! -r "$PPPOE_PIDFILE" ] ; then
    echo "adsl-status: Link is down (can't read pppoe PID file $PPPOE_PIDFILE)"
    exit 1
  fi
fi

> for i in /etc/ppp/ppp*.pid /var/run/ppp*.pid ; do
Read the comment, what do you think it does?

Try this on your command line for i in /etc/ppp/ppp*.pid /var/run/ppp*.pid ; do echo $i ; done > IF=`basename $i .pid`
Back-ticks collect the output of a sub-command, and store the result in a variable.

Try this command basename /foo/bar/hello.txt .txt Then try this command man basename > netstat -rn | grep " ${IF}\$" > /dev/null
/dev/null is the trash can for output you're not interested in.
It's typically used when you're just interested in the success/fail of a command rather than the detail of the output.
Here, we're just interested in whether the netstat command has produced a particular output.

This article has been dead for over six months. Start a new discussion instead.