Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1014489
  • 博文数量: 327
  • 博客积分: 9995
  • 博客等级: 中将
  • 技术积分: 4319
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-25 11:21
文章存档

2011年(31)

2010年(139)

2009年(157)

我的朋友

分类: LINUX

2009-12-20 15:30:28

个人认为学习shell脚本最好的方法就是看一个优秀的shell脚本,把pppoe的几个脚本文件看了一下。把其中的一个脚本修改了一下,作为学习shell脚本以及以后编写时的模板,如下:

#!/bin/sh
#***********************************************************************
# useful tips for shell scripts
#***********************************************************************
# From AUTOCONF
prefix=/usr
exec_prefix=${prefix}
# Paths to programs
IFCONFIG=/sbin/ifconfig
PPPD=/usr/sbin/pppd
PPPOE=${exec_prefix}/sbin/pppoe
ECHO=/bin/echo
LOGGER="/usr/bin/logger -t `basename $0`"
CONFIG=/etc/ppp/pppoe.conf
copy() {
    cp $1 $2
#$?是指前一条指令的结果是否正常,如果正常退出为0
    if [ "$?" != 0 ] ; then
 $ECHO "*** Error copying $1 to $2"
 $ECHO "*** Quitting."
 exit 1
    fi
}
# 判断是否是root的代码
if [ "`/usr/bin/id -u`" != 0 ] ; then
    $ECHO "$0: Sorry, you must be root to run this script"
    exit 1
fi
# 判断config文件是否存在
if [ ! -r "$CONFIG" ] ; then
    $ECHO "Oh, dear, I don't see the file '$CONFIG' anywhere.  Please"
    $ECHO "re-install the PPPoE client."
    exit 1
fi
# 判断需要内嵌的可执行文件是否可以执行
if [ ! -x $PPPD ] ; then
    $ECHO "Oops, I can't execute the program '$PPPD'.  You"
    $ECHO "must install the PPP software suite, version 2.3.10 or later."
    exit 1
fi
export CONFIG
#定义,展开变量;注意是“点 空格“,下面的USER ETH等都是在这里定义的
. $CONFIG
#printf例子,读取外面变量
    $ECHO ""
    $ECHO "USER NAME"
    $ECHO ""
    printf "%s" ">>> Enter your PPPoE user name (default $USER): "
    read U
    if [ "$U" = "" ] ; then
 U="$USER"
    fi
# while[]; do done 这是格式
    while [ true ] ; do
 $ECHO ""
 $ECHO "PASSWORD"
 $ECHO ""
 stty -echo
 printf "%s" ">>> Please enter your PPPoE password:    "
 read PWD1
 $ECHO ""
 printf "%s" ">>> Please re-enter your PPPoE password: "
 read PWD2
 $ECHO ""
 stty echo
 if [ "$PWD1" = "$PWD2" ] ; then
     break
 fi
 printf "%s" ">>> Sorry, the passwords do not match.  Try again? (y/n)"
 read ANS
 case "$ANS" in
     N|No|NO|Non|n|no|non)
  $ECHO "OK, quitting.  Bye."
  exit 1
 esac
    done
#case例子,和while do done一起使用的。
 while [ true ] ; do
     printf "%s" ">>> Choose a type of firewall (0-2): "
     read a
     if [ "$a" = 0 -o "$a" = 1 -o "$a" = 2 ] ; then
  break
     fi
     $ECHO "Please enter a number from 0 to 2"
 done
 case "$a" in
     0)
  FIREWALL=NONE
  ;;
     1)
  FIREWALL=STANDALONE
  ;;
     2)
  FIREWALL=MASQUERADE
  ;;
 esac
    fi
 
    while [ true ] ; do
        printf "%s" '>>> Accept these settings and adjust configuration files (y/n)? '
        read ANS
 case "ANS" in
     Y|y|yes|Yes|oui|Oui)
  ANS=y
  ;;
            N|n|no|No|non|Non)
  ANS=n
  ;;
 esac
 if [ "$ANS" = "y" -o "$ANS" = "n" ] ; then
     break
        fi
    done
    if [ "$ANS" = "y" ] ; then
 break
    fi
done
 
# 将变动的变量更新到conf文件中
sed -e "s&^USER=.*&USER='$U'&" \
    -e "s&^ETH=.*Ð='$E'&" \
    -e "s&^PIDFILE=.*&PIDFILE=\"$VARRUN/\$CF_BASE-pppoe.pid\"&" \
    -e "s/^FIREWALL=.*/FIREWALL=$FIREWALL/" \
    -e "s/^DEMAND=.*/DEMAND=$D/" \
    -e "s/^DNSTYPE=.*/DNSTYPE=$DNSTYPE/" \
    -e "s/^DNS1=.*/DNS1=$DNS1/" \
    -e "s/^DNS2=.*/DNS2=$DNS2/" \
    -e "s/^PEERDNS=.*/PEERDNS=$PEERDNS/" \
    < $CONFIG-bak > $CONFIG
#又是$? 的用法
if [ $? != 0 ] ; then
    $ECHO "** Error modifying $CONFIG"
    $ECHO "** Quitting"
    exit 1
fi
#备份文件的操作
$ECHO "Adjusting /etc/ppp/pap-secrets and /etc/ppp/chap-secrets"
if [ -r /etc/ppp/pap-secrets ] ; then
    $ECHO "  (But first backing it up to /etc/ppp/pap-secrets-bak)"
    copy /etc/ppp/pap-secrets /etc/ppp/pap-secrets-bak
else
    cp /dev/null /etc/ppp/pap-secrets-bak
fi
if [ -r /etc/ppp/chap-secrets ] ; then
    $ECHO "  (But first backing it up to /etc/ppp/chap-secrets-bak)"
    copy /etc/ppp/chap-secrets /etc/ppp/chap-secrets-bak
else
    cp /dev/null /etc/ppp/chap-secrets-bak
fi
#以exit结束
exit 0
阅读(1973) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~