Forum     

Go Back   Digit Technology Discussion Forum > Software > Open Source
Register FAQ Calendar Mark Forums Read

Open Source A place where you can talk to like-minded people about the fastest growing software movement today! Discuss anything and everything about Open Source software and Operating Systems.

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 25-08-2008, 09:02 AM   #1 (permalink)
Broken In
 
virus_killer's Avatar
 
Join Date: Jul 2006
Posts: 144
Question Explain this shell script to me.

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.
__________________
Device: T-Mobile G1(black)
ROM: JF ADP1.5 Apps2sd
104 apps with 69 MB remaining
virus_killer is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 25-08-2008, 08:00 PM   #2 (permalink)
Wahahaha~!
 
Faun's Avatar
 
Join Date: Dec 2006
Location: Pune/there
Posts: 7,109
Default Re: Explain this shell script to me.

Quote:
Originally Posted by virus_killer View Post

#$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
-------------------------------------------------------------------------------------

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

Any reply would be highly appreciated.
its easy, what part you didnt understand ? Am feeling a bit lazy typing.

I dont use dialer to connect to internet.
__________________
Steam/Flickr: psygeist
Spoiler:
Asus Z68 V-Pro|i5 2500k|TRUE Black|Ripjaws X+Corsair Vengeance|U2311H|N560GTX|D7000|XONAR STX|RE272|RE0|CC51|XE200PRO Walnut| TD II V2| Ultraphile|N5800

Mono
Faun is online now  
Old 28-08-2008, 04:18 AM   #3 (permalink)
Broken In
 
virus_killer's Avatar
 
Join Date: Jul 2006
Posts: 144
Default Re: Explain this shell script to me.

Thanks for your reply. i really appreciate. i am trying to learn shell scripts.so you can say i am a newbie to this shell script. i am referring books as well. but i have found this way very easy to learn. like comments added to the each lines makes it easy for me to understand.

I would really appreciate your effort if you will explain me rest of the script.
__________________
Device: T-Mobile G1(black)
ROM: JF ADP1.5 Apps2sd
104 apps with 69 MB remaining
virus_killer is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to find whether number is prime or not preshit.net Programming 6 21-02-2008 06:26 PM
can't explain!!pls help!! Chetan1991 Hardware Q&A 8 28-01-2007 11:40 AM
mul. in shell script legolas Open Source 4 24-05-2006 06:46 PM
Alias Does Not Work In Shell Script mannahazarika Open Source 3 24-01-2006 05:48 PM


All times are GMT +5.5. The time now is 07:15 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2