收集的脚本

#======================================================
#!/bin/sh
VIF="eth1"
VIF_S=0
VIF_E=15
for ((VIF_S=$VIF_S;$VIF_S - $VIF_E;VIF_S++))
do
vconfig add $VIF $VIF_S
done
#=========================拨号用的============================
#!/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin
. /etc/rc.d/rc.status

modprobe ppp_generic
modprobe pppoe

case "$1" in
start)
        echo -n "* Starting pppoe: "
        pppd plugin rp-pppoe.so eth0 mtu 1412 noipdefault noauth default-asyncma
p \
defaultroute hide-password nodetach usepeerdns mtu 1492 mru 1492 noaccomp \
nodeflate nopcomp novj novjccomp user CD87059820 lcp-echo-interval 20 \
lcp-echo-failure 3 >/dev/null 2>&1 &
#       logger -t WAN "PPPoE Connection is offline, Attempting re-connect!">/dev
/null
        check_status
        ;;
stop)
        echo -n "* Stopping pppoe: "
        killall pppd >/dev/null 2>&1 &
        check_status
        ;;
restart)
        sleep 5
        $0 stop
        $0 start
        ;;
check)
        PPPID=`pidof pppd`
        if [ -z $PPPID ];then
        echo "pppoe is disconnected at `date|awk '{print $3"/"$2 " - " $4}'`">>/
var/log/pppoe.log
        $0 start
        else
        false
        fi
        ;;
*)

#======================================================
check_status()
{
        PREV_ERR=$?
        echo -en "\\033[75G"
        if [ $PREV_ERR = 0 ]; then
#               echo -en "\\033[1;34m"
                echo -en "[ ok ]"
        else
#               echo -en "\\033[1;31m"
                echo -en "[ !! ]"
        fi
        echo -e "\\033[0;39m"
        unset PREV_ERR
}
#======================================================

一道shell编程题拿来做练习,初学awk,写的乱七八糟,但还算是做出来了:)
原题连接
http://bbs.chinaunix.net/thread-1020087-1-6.html

下面是代码

#!/bin/bash

export TITLE=title
export AUTHOR=author
export CATEGORY=category
export STATUS=in
export BNAME=none
export DATE=`date +%Y/%m/%d`

#add a book
add_mode(){
clear
read -p "UNIX library ----ADD MODE

Title: " TITLE
read -p "Author: " AUTHOR
read -p "Category: " CATEGORY
echo "$TITLE:$AUTHOR:$CATEGORY:$STATUS:$BNAME:$DATE" >> library
read -p "Any more to add? Yes or No >" YORN
if [ $YORN = "y" -o $YORN = "Y" ]; then
add_mode
else
lib_mng
fi
}

#delete a book
del_mode(){
clear
read -p "delete: " DELNAME
awk -v DEL="$DELNAME" 'BEGIN{FS=":"}{if(DEL==$1) print "UNIX library ----DELETE MODE\n" \
"Title:",$1"\n" "Author:",$2"\n" "Category:",$3"\n" "Status:",$4"\n" "Checked out by:",$5"\n" "Date:",$6}' library
read -p "Delete this book? Yes or No >" YON1
if [ $YON1 = "Y" -o $YON1 = "y" ];then
sed -i '/^'$DELNAME'/d' library
read -p "Any more to del? Yes or No >" YON2
if [ $YON2 = "Y" -o $YON2 = "y" ];then
del_mode
else
lib_mng
fi
else
lib_mng
fi
}

#display a book
display_mode(){
clear
DISINFO="can not find this book"
read -p "display: " DISNAME
for i in $(awk 'BEGIN{FS=":"} {print $1}' library)
do
if [ $i = $DISNAME ];then
DISINFO=`awk -v DIS="$DISNAME" 'BEGIN{FS=":"}{if(DIS==$1) print "UNIX library ----Display MODE\n" \
"Title:",$1"\n" "Author:",$2"\n" "Category:",$3"\n" "Status:",$4"\n" "Checked out by:",$5"\n" "Date:",$6}' library`
#DISINFO="haha"
#break
fi
done
echo $DISINFO
read -p "Any more to look for?Yes or No>" YON3
if [ $YON3 = "Y" -o $YON3 = "y" ];then
display_mode
else
lib_mng
fi
}

#update a book's status
update_mode(){
clear
read -p "title: " UPNAME
awk -v UP="$UPNAME" 'BEGIN{FS=":"}{if(UP==$1) print "UNIX library ----UPDATE MODE\n" \
"Title:",$1"\n" "Author:",$2"\n" "Category:",$3"\n" "Status:",$4"\n" "Checked out by:",$5"\n" "Date:",$6}' library
STATUS=`awk -v UP="$UPNAME" 'BEGIN{FS=":"}{if(UP==$1) print $4}' library`
if [ $STATUS = "in" ];then
read -p "studentname is: " BNAME
DATE=`date +%Y/%m/%d`
awk -v UP="$UPNAME" 'BEGIN{FS=OFS=":"}{if(UP==$1) {$4="out";$5="'$BNAME'";$6="'$DATE'"};print >"library"}' library
echo "New status: out"
else
DATE=`date +%Y/%m/%d`
awk -v UP="$UPNAME" 'BEGIN{FS=OFS=":"}{if(UP==$1) {$4="in";$5="none";$6="'$DATE'"};print >"library"}' library
echo "New status: in"
fi
read -p "Any more to update?Yes or No>" YON4
if [ $YON4 = "Y" -o $YON4 = "y" ];then
update_mode
else
lib_mng
fi
}

#report library's status
report_mode(){
clear
awk 'BEGIN{FS=":"}{print "UNIX library ----DELETE MODE\n" "Title:",$1"\n" "Author:",$2"\n" \
"Category:",$3"\n" "Status:",$4"\n" "Checked out by:",$5"\n" "Date:",$6}' library | more
lib_mng
}

#master mode
lib_mng(){
clear
read -p "UNIX library manager
0.exit this program
1.add a book
2.delete a book
3.display a book
4.update a book's status
5.report library's status

Input your choice: " NUM
case $NUM in
     "0")
     exit 1
     ;;
     "1")
     add_mode
     ;;
     "2")
     del_mode     
     ;;
     "3")
     display_mode
     ;;
     "4")
     update_mode
     ;;
     "5")
     report_mode
     ;;
     *)
     echo "Invalid chioce,press any key to try again"
     ;;
esac
}
lib_mng

你可能感兴趣的:(#shell脚本相关)