| |
 |
|
 |
 |
|
 |
shell基础学习系列(4):CU Shell版精华练习三则
|
|
|
本学习笔记都是取自CU Shell版,有很多无法在脚本的版权声明中注明原作者,在此致歉。仅供学习之用,希望后来者亦能有所得。
1、net_state【测试网络状态的脚本】
可以利用ping探测TARGET的存在。这在嵌入式系统中可以取代心跳监测,不仅可以网络保活,而且可以实时测试网络在线状态。
原理就是通过获取返回信息中的损失率来检验网络状态是否良好。缺点就是这并不能作为网络状态是否良好的绝对标准。因为如果对方禁止ICMP回显,则此方法失效。采用心跳监测算是一种比较通用的方法了。
整理脚本如下:
|
#!/bin/bash # Copyright 2007 (c), Shandong University # All rights reserved. # # Filename : net_state # Description: Test network state, and return 0 if good; else return 1 # Author : Liu Qingmin # Version : 1.0 # Date : 2007-05-17 #
# Default Variable NET_GOOD=0 # the value of return if network state is good E_WRONG_ARGS=1 # error number of return if arguments are wrong E_NET_NOT_GOOD=2 # error number of return if network state is not good INTERVAL=3 # the time interval of ping TIMES=1 # the times every one LOSSRATE=0 # the loss rate if network is good
# Handle Arguments case $1 in "") echo "Usage: `basename $0` [TARGET]" exit $E_WRONG_ARGS ;; *) TARGET=$1 ;; esac
# Getch the loss rate lossrate=`ping -i $INTERVAL -c $TIMES $TARGET | \ grep -o "...%" | \ sed 's/,//g' | \ awk -F% '{print $1}'`
# Handle the result if [ $lossrate != $LOSSRATE ]; then echo "Network is not good" exit $E_NET_NOT_GOOD else echo "Network is good!" exit $NET_GOOD fi
########################## END OF SCRIPT ###########################################
|
2、cleanup【日志清理脚本】
这个算是初级版本,毕竟不是做系统管理。只是学习一下而已。
|
#!/bin/bash
# Copyright 2007 (c), Shandong University # All rights reserved. # # Filename : cleanup # Description: clean up /var/log---delete all files before ${SAVE_DAYS}.
# ---restore the $LINES of messages # Author : Liu Qingmin # Version : 1.0 # Date : 2007-05-17 #
# Default Variable TRUE=0 # the value of return if true FALSE=1 # the value of return if false NOT_ROOT=2 # the value of return if not root WRONGARGS=3 # the value of return if args is wrong E_XCD=4 # cannot excute documents LOGDIR=/var/log # the log directory ROOT_UID=0 # the UID of root
# Configuration LINES=50 # default lines to restore SAVE_DAYS=7 # default days to save
# test whether the user is the root is_root() { if [ `id -u` != 0 ]; then return $TRUE else return $FALSE fi }
###### main func ######
# must be the root if is_root; then echo "Must be root to run this script." exit $NOT_ROOT fi
# handle parameters case $1 in "") lines=$LINES ;; *[!0-9]*) echo "Usage: `basename $0` [lines]" exit $WRONGARGS ;; *) lines=$1 ;; esac
# step into LOGDIR cd $LOGDIR || { echo "cannot change to necessary directory." exit $E_XCD }
# cleanup tail -$lines messages > msg.tmp mv msg.tmp messages cat /dev/null > wtmp find $LOGDIR -mtime +$SAVE_DAYS | xargs rm -f
# quit echo "Logs cleaned up" exit 0
|
3、tr_case.sh【shift用法】
|
#!/bin/bash # Copyright 2007 (c), Shandong University # All rights reserved. # # Filename : tr_case.sh # Description: convert files to either upper or lower case # Author : Liu Qingmin # Version : 1.0 # Date : 2007-05-18 #
# Default Configuration # Cannot be modified FILES="" # file list to be dealed with TRCASE="" # action to be taken EXT="" # extended name OPT=no # assure that there is one option at lease
# Default Error Variable SUCCESS=0 # convert successed or quit normally FAIL=1 # convert failed ILLEGAL_OPTION=2 # use illegal option NO_OPTION=3 # no one option NO_ARGS=4 # no one argument
# Print Syntax usage() { echo "Usage: `basename $0` -[l | u] file [...]" }
# Print error information if convertion failed error_msg() { _FILE=$1 echo "`basename $0`: Error! The conversion failed on $_FILE." }
# Print help information help() { echo echo "`basename $0` [option] [file]" echo echo "Description: " echo " To convert files to upper or lower case" echo echo "option: " echo " -l convert to lowercase" echo " -u convert to uppercase" echo echo "Usage: " echo " `basename $0` -[l | u] file [...]" echo }
############################## Main Func ###################################
# Make sure that there is one option at least. if [ $# = 0 ]; then echo "For more info, please try `basename $0` --help" exit $NO_ARGS fi
# Handle options while [ $# -gt 0 ]; do case $1 in # set the variables based on what option was used -u) # uppercase setup TRCASE=upper EXT=".UC" OPT=yes shift ;; -l) # lowercase setup TRCASE=lower EXT=".LC" OPT=yes shift ;; --help) # display help information help exit $SUCCESS ;; -*) # deal with illegal option usage exit $ILLEGAL_OPTION ;; *) # deal with file if [ -f $1 ]; then FILES=$FILES""$1 else echo "`basename $0`: cannot find the file $1" fi shift ;; esac done
# if no options, give help information if [ "$OPT" = "no" ]; then echo "`basename $0`: Error! You need to specify an option. No action taken" echo "try `basename $0` --help" exit $NO_OPTION fi
# now read in all the file(s) for LOOP in $FILES; do case $TRCASE in lower) # if lower cat $LOOP | tr "[A-Z]" "[a-z]" > $LOOP$EXT if [ $? != 0 ]; then error_msg $LOOP exit $FAIL else echo "converted file called $LOOP$EXT" fi ;; upper) # if upper cat $LOOP | tr "[a-z]" "[A-Z]" > $LOOP$EXT if [ $? != 0 ]; then error_msg $LOOP exit $FAIL else echo "converted file called $LOOP$EXT" fi ;; esac done
# quit successfully exit $SUCCESS ############################## End of Script ###################################
|
测试:
|
[armlinux@lqm scripts]$ ./tr_case.sh For more info, please try tr_case.sh --help
[armlinux@lqm scripts]$ echo $? 4 [armlinux@lqm scripts]$ ./tr_case.sh -k Usage: tr_case.sh -[l | u] file [...] [armlinux@lqm scripts]$ echo $? 2 [armlinux@lqm scripts]$ ./tr_case.sh opt.sh tr_case.sh: You need to specify an option. No action taken try tr_case.sh --help [armlinux@lqm scripts]$ echo $? 3 [armlinux@lqm scripts]$ ./tr_case.sh -u opt.sh converted file called opt.sh.UC [armlinux@lqm scripts]$ echo $? 0 [armlinux@lqm scripts]$ ./tr_case.sh --help
tr_case.sh [option] [file]
Description: To convert files to upper or lower case
option: -l convert to lowercase -u convert to uppercase
Usage: tr_case.sh -[l | u] file [...]
[armlinux@lqm scripts]$
|
|
|
|
发表于: 2007-05-17,修改于: 2007-11-19 17:01,已浏览1037次,有评论0条
推荐
投诉
|
|
 |
|
 |
|  |
|
 |
|