adsl-start.sh 分析

来源:互联网 发布:美橙互联域名如何备案 编辑:程序博客网 时间:2024/06/06 10:51
#! /bin/bash
# Generated automatically from adsl-start.in by configure.
#***********************************************************************
#
# adsl-start
#
# Shell script to bring up an ADSL connection
#
# Copyright (C) 2000 Roaring Penguin Software Inc.
#
# $Id: adsl-start.in,v 1.7 2001/06/25 15:00:47 dfs Exp $
#
# This file may be distributed under the terms of the GNU General
# Public License.
#
# Usage: adsl-start [config_file]
#        adsl-start interface user [config_file]
# Second form overrides USER and ETH from config file.
# If config_file is omitted, defaults to /etc/ppp/pppoe.conf
#
#***********************************************************************


# From AUTOCONF
prefix=/usr
exec_prefix=/usr


# Paths to programs
CONNECT=/sbin/adsl-connect
ECHO=/bin/echo
IP=/sbin/ip
LS=/bin/ls
NETWORKDIR=/etc/sysconfig/network-scripts


get_device() {
    if [ ! -d $NETWORKDIR ] ; then #是目录?
        $ECHO "** $NETWORKDIR not found"
        $ECHO "** Quitting"
        exit 1
    fi


    cd $NETWORKDIR
    interfaces=$($LS ifcfg-ppp* 2>/dev/null | egrep -v '(~|\.bak)$' | \
                 egrep -v '(rpmsave|rpmorig|rpmnew)' | sed 's/^ifcfg-//g')
#若错误直接删除,-v 显示不包括匹配文本的所有行,过滤以.bak结尾的等文件,并将ifcfg-去掉,这样,最后提出的是pppN,作为变量赋给interface, 命令替换$(cmd)和符号`cmd`(注意这不是单引号,在美式键盘上,`是ESC下面的那个键)有相同之处.以echo $(ls)来说明整个替换过程:shell扫描一遍命令行,发现了$(cmd)结构,便将$(cmd)中的cmd执行一次,得到其标准输出,再将此输出放 到原来命令echo $(ls)中的$(ls)位置,即替换了$(ls),再执行echo命令。如下:运行$ ls,显示a b c,运行$ echo $(ls),显示 a b c,运行$ echo `ls`,显示a b c
    for i in $interfaces ; do
        test -f ifcfg-$i && . ifcfg-$i 2>/dev/null
测试ifcfg-pppN是否正规文件,命令之间使用 && 连接,实现逻辑与的功,只有在 && 左边的命令返回真(命令返回值 $? == 0),&& 右边的命令才会被执——只要有一个命令返回假(命令返回值 $? == 1),后面的命令就不会被执行。用sorcue 或者.(dot) 。明确告诉shell不要fork执行脚本,而是在当前的shell执行,这样环境变量就可以保存下来了 
        if [ "$TYPE" = "xDSL" ] ; then
            CONFIG=$NETWORKDIR/ifcfg-$i
            确定配置文件
break
        fi
    done
}


# Must be root
if [ "`/usr/bin/id -u`" != 0 ] ; then
    [ "$DEBUG" = "1" ] && $ECHO "$ME: You must be root to run this script" >& 2
    exit 1
fi


# Debugging
if [ "$DEBUG" = "1" ] ; then
    $ECHO "*** Running in debug mode... please be patient..."
    DEBUG=`mktemp -d /tmp/pppoe-debug-XXXXXXXX`
    if [ $? -ne 0 ] ; then
$ECHO "Could not create directory $DEBUG... exiting"
exit 1
    fi
    export DEBUG
    DEBUG=$DEBUG/pppoe-debug.txt


    # Initial debug output
    $ECHO "---------------------------------------------" > $DEBUG
    $ECHO "* The following section contains information about your system" >> $DEBUG
    date >> $DEBUG
    $ECHO "Output of uname -a" >> $DEBUG
    uname -a >> $DEBUG
    $ECHO "---------------------------------------------" >> $DEBUG
    $ECHO "* The following section contains information about your network" >> $DEBUG
    $ECHO "* interfaces.  The one you chose for PPPoE should contain the words:" >> $DEBUG
    $ECHO "* 'UP' and 'RUNNING'.  If it does not, you probably have an Ethernet" >> $DEBUG
    $ECHO "* driver problem." >> $DEBUG
    $ECHO "Output of ip addr show" >> $DEBUG
    $IP addr show >> $DEBUG
    $ECHO "---------------------------------------------" >> $DEBUG
    if [ "`uname -s`" = "Linux" ] ; then
        $ECHO "* The following section contains information about kernel modules" >> $DEBUG
$ECHO "* If the module for your Ethernet card is 'tulip', you might" >> $DEBUG
$ECHO "* want to look for an updated version at http://www.scyld.com" >> $DEBUG
$ECHO "Output of lsmod" >> $DEBUG
lsmod >> $DEBUG
$ECHO "---------------------------------------------" >> $DEBUG
    fi
    $ECHO "* The following section lists your routing table." >> $DEBUG
    $ECHO "* If you have an entry which starts with '0.0.0.0', you probably" >> $DEBUG
    $ECHO "* have defined a default route and gateway, and pppd will" >> $DEBUG
    $ECHO "* not create a default route using your ISP.  Try getting" >> $DEBUG
    $ECHO "* rid of this route." >> $DEBUG
    $ECHO "Output of netstat -n -r" >> $DEBUG
    netstat -n -r >> $DEBUG
    $ECHO "---------------------------------------------" >> $DEBUG
    $ECHO "Contents of /etc/resolv.conf" >> $DEBUG
    $ECHO "* The following section lists DNS setup." >> $DEBUG
    $ECHO "* If you can browse by IP address, but not name, suspect" >> $DEBUG
    $ECHO "* a DNS problem." >> $DEBUG
    cat /etc/resolv.conf >> $DEBUG
    $ECHO "---------------------------------------------" >> $DEBUG
    $ECHO "* The following section lists /etc/ppp/options." >> $DEBUG
    $ECHO "* You should have NOTHING in that file." >> $DEBUG
    $ECHO "Contents of /etc/ppp/options" >> $DEBUG
    cat /etc/ppp/options >> $DEBUG 2>/dev/null
    $ECHO "---------------------------------------------" >> $DEBUG
    DEBUG="1"
fi


# Sort out command-line arguments
case "$#" in 参数数量?
    1)
CONFIG="$1"
;;
    3)
CONFIG="$3"
;;
esac


if [ -z "$CONFIG" ] ; then 判断字符串是否为空
    get_device
    [ -z "$CONFIG" ] && CONFIG=/etc/ppp/pppoe.conf
fi


if [ ! -f "$CONFIG" -o ! -r "$CONFIG" ] ; then
-f判断是否为普通文件,-o测试条件中有一个为真,则测试条件为真,-r判断是否可读
    [ "$DEBUG" = "1" ] && $ECHO "$ME: Cannot read configuration file '$CONFIG'" >& 2
    exit 1
fi


. $CONFIG


# Check for command-line overriding of ETH and USER
case "$#" in
    2|3)
ETH="$1"
USER="$2"
;;
esac



if [ -r "$PIDFILE" ] ; then
 PIDFILE是配置文件pppoe.conf中的一个字段名,PIDFILE="/var/run/$CF_BASE-adsl.pid",/etc/run/pppoe-adsl.pid中记录的是adsl-connect的进程ID
    PID=`cat "$PIDFILE"`
    # Check if still running
    kill -0 $PID > /dev/null 2>&1
-0,不发出任何信号
    if [ $? = 0 ] ; then
判断命令退出的状态,0表示没错误,非0表示有错误
[ "$DEBUG" = "1" ] && $ECHO "$ME: There already seems to be an ADSL connection up (PID $PID)" >& 2
exit 1
    fi
    # Delete bogus PIDFILE
    rm -f "$PIDFILE" "$PIDFILE.pppd" "$PIDFILE.pppoe" "$PIDFILE.start"
fi


echo $$ > $PIDFILE.start


# Start the connection in the background unless we're debugging
if [ "$DEBUG" = "1" ] ; then
    $CONNECT "$@"
"$@"传递所有参数
    exit 0
fi


$CONNECT "$@" > /dev/null 2>&1 &
CONNECT_PID=$!


if [ "$CONNECT_TIMEOUT" = "" -o "$CONNECT_TIMEOUT" = 0 ] ; then
    exit 0
-o是或的意思
fi


# Don't monitor connection if dial-on-demand
if [ "$DEMAND" != "" -a "$DEMAND" != "no" ] ; then
    exit 0
fi


# Monitor connection
TIME=0
while [ true ] ; do
    /sbin/adsl-status $CONFIG > /dev/null 2>&1


    # Looks like the interface came up
    if [ $? = 0 ] ; then
# Print newline if standard input is a TTY
[ "$DEBUG" = "1" ] && tty -s && $ECHO " Connected!"
exit 0
    fi


    if test -n "$FORCEPING" ; then
[ "$DEBUG" = "1" ] && $ECHO -n "$FORCEPING"
    else
[ "$DEBUG" = "1" ] && tty -s && $ECHO -n "$PING"
    fi
    sleep $CONNECT_POLL
    TIME=`expr $TIME + $CONNECT_POLL`
    if [ $TIME -gt $CONNECT_TIMEOUT ] ; then
break
    fi
done


[ "$DEBUG" = "1" ] && $ECHO "TIMED OUT" >& 2
# Timed out!  Kill the adsl-connect process and quit
kill $CONNECT_PID > /dev/null 2>&1


# Clean up PIDFILE(s)
rm -f "$PIDFILE" "$PIDFILE.pppd" "$PIDFILE.pppoe" "$PIDFILE.start"


# add old default gw back
if [ -s /etc/default-route ] ; then
    route add default gw `cat /etc/default-route`
    rm -f /etc/default-route
fi


exit 1
原创粉丝点击