Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15318783
  • 博文数量: 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 12:17:31

# 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]} $LU_FUNC_ARGS
}

lu_list_commands() {
    for name in ${lu_functions_names[@]}; do
        printf ' %s\n' $name
    done
}

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

lu_drain_commands() {
    for cmd in $*; do
        lu_do_func $cmd
    done
}
#----------------------------------------------------------------------------
lu_func1() {
    echo 'this is func1'
    printf "random=%d\n" $RANDOM
    echo "args=[$*]"
    echo
}

lu_func2() {
    echo 'this is func2'
    echo "args=[$*]"
    echo
}

lu_func3() {
    echo 'this is func3'
    echo "args=[$*]"
    echo
}

lu_func4() {
    echo 'this is func4'
    echo "args=[$*]"
    echo
}

lu_func5() {
    echo 'this is func5'
    echo "args=[$*]"
    echo
}

lu_add_functions 'tx' lu_func1 'hello1' 'luther.gliethttp' 1 2 3 4 5 6 7 8 9 10 11 12 13
lu_add_functions 'tx' lu_func1 'hello1' 'luther.gliethttp'
lu_add_functions 'rx' lu_func2 'hello2' 'luther.gliethttp' 1
lu_add_functions 'f3' lu_func3 'hello3' 'luther.gliethttp' 1 2
lu_add_functions 'f4' lu_func4 'hello4' 'luther.gliethttp' 1 2 3
lu_add_functions 'f5' lu_func5 'hello5' 'luther.gliethttp' 1 2 3 4

Usage() {
    # cat <<\_EOF
    cat <<_EOF
Usage: `basename $0` cmd0 cmd1 ...
commands list:
_EOF
    lu_list_commands
    return 0
}

while getopts hva: op; do
    case $op in
        'h')
            Usage
            exit 0
        ;;

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

for cmd in $*; do
    lu_add_commands  $cmd
done

lu_drain_commands ${lu_commands[@]}

luther@gliethttp:/vobs/tmp$ ./gliethttp.sh tx rx f3 f4 f5  f6 f7
this is func1
random=4649
args=[hello1 luther.gliethttp 1 2 3 4 5 6 7 8 9 10 11 12 13]

this is func2
args=[hello2 luther.gliethttp 1]

this is func3
args=[hello3 luther.gliethttp 1 2]

this is func4
args=[hello4 luther.gliethttp 1 2 3]

this is func5
args=[hello5 luther.gliethttp 1 2 3 4]

luther@gliethttp:/vobs/tmp$ ./gliethttp.sh -h
Usage: gliethttp.sh cmd0 cmd1 ...
commands list:
 tx
 tx
 rx
 f3
 f4
 f5
luther@gliethttp:/vobs/tmp$ ./gliethttp.sh tx
this is func1
random=11860
args=[hello1 luther.gliethttp 1 2 3 4 5 6 7 8 9 10 11 12 13]

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