Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15537042
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类:

2008-12-26 21:35:48

《我的第一个shell脚本模板开发完成,嘿嘿》
# Copyright 2008 luther.gliethttp
#============================================================================
lu_commands=()
lu_functions=()
lu_functions_names=()
lu_functions_args=()
LU_FUNC_ARGS_INDEX_CONST=2
LU_FUNC_NAME_INDEX_CONST=0
LU_FUNC_INDEX_CONST=1

lu_add_functions() {
    lu_functions_names[${#lu_functions_names[@]}]=$1
    shift
    lu_functions[${#lu_functions[@]}]=$1
    shift
    lu_functions_args[${#lu_functions_args[@]}]=$#
    lu_functions_args=(${lu_functions_args[@]} $*)
    # until [ $# -eq 0 ]; do
    #     echo "$#,$*"
    #     shift
    # done
}

LU_FUNC_INDEX=0
LU_FUNC=0
LU_FUNC_ARGS=0
lu_find_func() {
    local func_nams=$1
    LU_FUNC_INDEX=0
    for name in ${lu_functions_names[@]}; do
        if [ $name = $func_nams ]; then
            LU_FUNC=${lu_functions[$LU_FUNC_INDEX]}
            return 0
        fi
        LU_FUNC_INDEX=`expr $LU_FUNC_INDEX + 1`
    done
    return 1
}

lu_find_args() {
    local largs=$1
    local index=0
    local offset=0
    local counts=${lu_functions_args[$offset]}
    while [ $index -lt $largs ]; do
        let "offset+=counts+1"
        counts=${lu_functions_args[$offset]}
        # index=`expr $index + 1`
        let "index+=1"
    done
    let "++offset"
    LU_FUNC_ARGS=${lu_functions_args[@]:$offset:counts}
}

lu_do_func() {
    local index=$1
    lu_find_args $index
    ${lu_functions[$index]} $index $LU_FUNC_ARGS
}

lu_list_commands() {
    local index=0
    for name in ${lu_functions_names[@]}; do
        lu_find_args $index
#    ${lu_functions[$index]} $index $LU_FUNC_ARGS
        # printf ' %s\n' $name
        echo " $name($LU_FUNC_ARGS)"
        let "index++"
    done
}

lu_list_added_commands() {
    local index=$LU_DEFAUT_FUNCS
    if [[ ${#lu_functions[@]} != $index ]]; then
        echo '==========================================='
        for name in ${lu_functions_names[@]:$index}; do
            lu_find_args $index
            # ${lu_functions[$index]} $index $LU_FUNC_ARGS
            # printf ' %s\n' $name
            echo " $name($LU_FUNC_ARGS)"
            let "index++"
        done
        echo '-------------------------------------------'
    fi
}

lu_print_commands() {
    local cur=$1
    local nums=$2
    local index=$3
    local name=
    echo '+++++++++++++++++++++++++++++++++++++++++++'
    lu_find_args $index
    name=${lu_functions_names[$index]}
    echo "[$cur:$nums]$name($LU_FUNC_ARGS)"
    echo
}

lu_add_commands() {
#    lu_find_func $1 || return 1
#    return 0
    lu_find_func $1
    if [ $? -gt 0 ]; then
        # echo
        # echo command: '"'$cmd'"' is not supported.
        return
    else
        lu_commands[${#lu_commands[@]}]=$LU_FUNC_INDEX
    fi
}

lu_drain_commands() {
    local index=1
    for cmd in $*; do
        lu_print_commands $index $# $cmd
        lu_do_func $cmd
        let "++index"
    done
}

no_action() {
    return
}

let "LU_SECONDS=0xffffffff"
do_check_timeout() {
    if [ $(($SECONDS + $1)) -gt $LU_SECONDS ]; then
        echo 'time reached now'
        exit 1
    fi
}

do_action() {
    local func_init=$1
    local func=$2
    local func_exit=$3
    local max_counts=$4
    local sleeps=$5
    local do_quit='0'
    local counts=0
    # echo $LU_SECONDS
    [ $func_init != no_action ] && $func_init
    until [ $do_quit = '1' ]; do
        # 执行func
        [ $func != no_action ] && $func $counts
        if [ $? -gt 0 ]; then
            return 1
        fi
        # 计数统计
        do_check_timeout $sleeps
        counts=$(($counts + 1))
        if [ $counts -ge $max_counts ]; then
            do_quit='1'
            continue
        fi
        sleep $sleeps
    done
    [ $func_exit != no_action ] && $func_exit $counts
}
#----------------------------------------------------------------------------
lu_test_init() {
    echo 'init'  
}

lu_test_exit() {
    echo 'exit'  
}

lu_test_action() {
    printf "%d: test[random= %-5d]\n" $SECONDS $RANDOM
    return 0
}

lu_test() {
    echo $*
    # func          init                do_action           exit         maxcounts        sleep_seconds
    do_action    lu_test_init         lu_test_action     lu_test_exit       10                  1    
}


lu_ping_init() {
    echo 'init'  
    LU_PING_COUNTS=0
}

lu_ping_exit() {
    echo 'exit'  
}


lu_ping_action() {
    local maxcounts=10
    local pingcounts=1
    local interval=1
    local ip_addr='127.0.0.1'
    echo "ping -c $pingcounts $ip_addr -i $interval"
    ping -c $pingcounts $ip_addr -i $interval
    let "++LU_PING_COUNTS"
    if [ $LU_PING_COUNTS -ge $maxcounts ]; then
        return 1
    fi
    return 0
}

lu_ping() {
    # func          init                do_action           exit         maxcounts        sleep_seconds
    do_action   lu_ping_init         lu_ping_action      lu_ping_exit    4294967295             1
}

lu_laping() {
    shift
    local maxcounts=$1
    local sleep_seconds=$2
    local pingcounts=$3
    local interval=$4
    local ip_addr=$5
    until ((maxcounts-- <= 0)); do
        ping -c $pingcounts $ip_addr -i $interval >/dev/null 2>&1
        if [[ $? > 0 ]]; then
            echo 'wrong'
        else
            echo 'pass'
        fi
        sleep $sleep_seconds
    done
}

lu_add_functions 'test' lu_test  'hello'  'test' 1 2 3 4 5 6 7 8 9 10 11 12 13
lu_add_functions 'ping' lu_ping  5 0 2 1 '127.0.0.1'
lu_add_functions 'laping' lu_laping  5 1 2 1 '127.0.0.1'

LU_DEFAUT_FUNCS=${#lu_functions[@]}

Usage() {
    # cat <<\_EOF
    cat <<_EOF

Usage:
`basename $0` -t[loop seconds] cmd0 cmd1 ...
              -a[maxcounts],[sleep_seconds],[pingcounts],[interval],[ip_addr]
commands list:
_EOF
    lu_list_commands
    echo
    return 0
}

while getopts hva: op; do
    case $op in
        'h')
            Usage
            exit 0
        ;;
            
        't')
            LU_SECONDS=$OPTARG
            echo $LU_SECONDS | grep -q '[^0-9]' && Usage && exit 1
            [ $LU_SECONDS -eq 0 ] && LU_SECONDS=$((0xffffffff))
        ;;
            
        'a')
                [ -z $lapingindex ] && lapingindex=0
                substring=$OPTARG
                laping_defaut_values=(${substring//','/' '})
                laping_defaut_values_CONST=(5 1 2 1 "202.108.22.5")
                i=${#laping_defaut_values[@]}
                while (( i <= ${#laping_defaut_values_CONST[@]} )); do
                    laping_defaut_values[$i]=${laping_defaut_values_CONST[$i]}
                    let "i++"
                done
                lu_add_functions "laping$lapingindex" lu_laping ${laping_defaut_values[@]}
                lu_add_commands "laping$lapingindex"
                let "lapingindex++"
        ;;

        *)
        ;;
    esac    
done    
    
shift `expr $OPTIND - 1`

for cmd in $*; do
    lu_add_commands  $cmd
done

lu_list_added_commands
lu_drain_commands ${lu_commands[@]}
luther@gliethttp:~$ ./lp.sh  -a 5,1,1,1,127.0.0.1 -a 10,1,1,1,gliethttp.cublog.cn
===========================================
 laping0(5 1 1 1 127.0.0.1)
 laping1(10 1 1 1 gliethttp.cublog.cn)
-------------------------------------------
+++++++++++++++++++++++++++++++++++++++++++
[1:2]laping0(5 1 1 1 127.0.0.1)

pass
pass
pass
pass
pass
+++++++++++++++++++++++++++++++++++++++++++
[2:2]laping1(10 1 1 1 gliethttp.cublog.cn)

pass
pass
pass
pass
pass
pass
pass
pass
pass
pass
luther@gliethttp:~$
阅读(1245) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~