美文网首页Openwrt路由联盟程序员
Configration in script(uci 脚本使用b

Configration in script(uci 脚本使用b

作者: albertxie | 来源:发表于2017-03-30 17:16 被阅读169次

    BUG: config_load之后,调用uci delete或者uci set xxx.xxx.xxx='',再度掉用config_load,之后通过config_get读取出来的值将会出错,delete之后不使用commit

    解决方法1:使用API函数config_unset或者config_set

    解决方法2:

    config_clear() {

    local SECTION="$1"

    #遍历每一项option,并将其unset

    for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \

    sed -e 's/\(.*\)=.*$/\1/'` ; do

    unset $oldvar

    done

    }

    config () {

    local cfgtype="$1"

    local name="$2"

    config_clear $name

    export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))

    name="${name:-cfg$CONFIG_NUM_SECTIONS}"

    append CONFIG_SECTIONS "$name"

    [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"

    export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"

    export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"

    }



    在调用api时,首先我们需要调用config_load

    config_load() {

    [ -n "$IPKG_INSTROOT" ] && return 0

    uci_load "$@"

    }

    uci_load() {

    local PACKAGE="$1"

    local DATA

    local RET

    local VAR

    _C=0

    if [ -z "$CONFIG_APPEND" ]; then

    for VAR in $CONFIG_LIST_STATE; do

    export ${NO_EXPORT:+-n} CONFIG_${VAR}=

    export ${NO_EXPORT:+-n} CONFIG_${VAR}_LENGTH=

    done

    export ${NO_EXPORT:+-n} CONFIG_LIST_STATE=

    export ${NO_EXPORT:+-n} CONFIG_SECTIONS=

    export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0

    export ${NO_EXPORT:+-n} CONFIG_SECTION=

    fi

    DATA="$(/sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} ${LOAD_STATE:+-P /var/state} -S -n export "$PACKAGE" 2>/dev/null)"

    RET="$?"

    [ "$RET" != 0 -o -z "$DATA" ] || eval "$DATA"

    unset DATA

    ${CONFIG_SECTION:+config_cb}

    return "$RET"

    }

    1、首先通过uci export $PACKAGE 获取配置文件的内容(其他参数就不一一简绍了,具体查看uci --help)

    package xxx

    config xxx xxx

    option xxx xxx

    list      xxx  xxx

    2、之后eval "$DATA",将package config option替换成函数(这一步是很让我很惊艳的用法,得向大牛学习)

    package() {

    return 0

    }

    config () {

    local cfgtype="$1"

    local name="$2"

    export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))

    name="${name:-cfg$CONFIG_NUM_SECTIONS}"

    append CONFIG_SECTIONS "$name"

    [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"

    export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"

    export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"

    }

    option () {

    local varname="$1"; shift

    local value="$*"

    export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"

    [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"

    }

    list() {

    local varname="$1"; shift

    local value="$*"

    local len

    config_get len "$CONFIG_SECTION" "${varname}_LENGTH" 0

    [ $len = 0 ] && append CONFIG_LIST_STATE "${CONFIG_SECTION}_${varname}"

    len=$(($len + 1))

    config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value"

    config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len"

    append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP"

    list_cb "$varname" "$*"

    }

    3、通过这四个函数将每一项配置项设置到环境变量当中

    (bug出现原因:当设入环境变量后,通过uci delete或uci set 删除只会操作换存,在调用config_load 因为option项不存在了,则不会在度设置到环境变量中,因此还是删除之前的值,而不是空)

    4、之后的config_get之类的函数就是获取环境变量的值


    for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \

    sed -e 's/\(.*\)=.*$/\1/'` ; do

    for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \

    sed -e 's/\(.*\)=.*$/\1/'` ; do

    for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \

    sed -e 's/\(.*\)=.*$/\1/'` ; do

    for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \

    sed -e 's/\(.*\)=.*$/\1/'` ; do

    for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \

    sed -e 's/\(.*\)=.*$/\1/'` ; do

    for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \

    sed -e 's/\(.*\)=.*$/\1/'` ; do

    相关文章

      网友评论

        本文标题:Configration in script(uci 脚本使用b

        本文链接:https://www.haomeiwen.com/subject/zulpottx.html