全部博文(2005)
分类:
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[@]} |