openwrt/lib/function.sh

Source: Internet
Author: User

/lib/function.sh

#!/bin/sh # Copyright (c) 2006-2014 openwrt.org # Copyright (c) 2006 Fokus Fraunhofer. 
De> # Copyright (C) Vertical Communications Debug () {${debug:-:} "$@"} # NewLine n= "" _c=0 no_export=1 Load_state=1 list_sep= "" "append () {local var= ' $ local value= ' $ ' local sep= ' ${3:-} ' eval ' export $ {no_export:+-n}--\ "$var =\${$var: +\${$var}\${value:+\ $sep}}\ $value \"} list_contains () {local var= "$" local s Tr= "$" local val eval "val=\" \${$var} \ "[${val%% $str *}"!= "$val"]} config_load () {[n] $IPK
    G_instroot "] && return 0 uci_load" $@ "} RESET_CB () {CONFIG_CB () {return 0;}
    OPTION_CB () {return 0;}
LIST_CB () {return 0;}} RESET_CB package () {return 0} config () {local cfgtype= "$" local 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_${conf Ig_section}_type= $cfgtype "} option () {local varname=" $ "; shift local value=" $* "Export ${no_export:+-n} "Config_${config_section}_${varname}= $value" [-N "$NO _callback] | |  OPTION_CB "$varname" "$*"} list () {local varname= ' $ '; 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" "$ * "} config_unset () {config_set" $ ""} # Config_get <variable> <section> <option> [<de Fault>] # Config_get<section> <option> Config_get () {case "$" "") eval echo "\${config_${1}_${2}:-\${4}";;
    *) eval Export ${no_export:+-n}-"${1}=\${config_${2}_${3}:-\${4}";; ESAC} # Config_get_bool <variable> <section> <option> [<default>] Config_get_bool () {Local
        _tmp config_get _tmp "$" "$" "$" case "$_tmp" in 1|on|true|yes|enabled) _tmp=1;;
        0|off|false|no|disabled) _tmp=0;;
    * _tmp= "$";; ESAC export ${no_export:+-n} "$1=$_tmp"} config_set () {local section= "$" local option= "$" local value = "$" local old_section= $CONFIG _section "config_section=" $section "option" $option "$value" Config_sect ion= "$old _section"} config_foreach () {local ___function= "$" [$#-ge 1] && shift local ___type=
    "$" ["$#"-ge 1] && shift local section Cfgtype [-Z "$CONFIG _sections"] && return 0 For section in ${coNfig_sections};
        Do config_get cfgtype "$section" TYPE [-N "$___type"-A "X$cfgtype"!= "X$___type"] && continue Eval "$___function \" \ $section \ "\ \$@\" "Done} Config_list_foreach () {[" $# "-ge 3] | |-return 0 L Ocal section= "$"; Shift local option= "$"; Shift local function= "$"; Shift local Val Local len local c=1 config_get len "${section}" "${option}_length" [-Z "$len"] ;& return 0 While [$c-le "$len"]; Do Config_get val "${section}" "${option}_item$c" eval "$function \" \ $val \ "\" \$@\ "" c= "$ ($c + 1) "Done} insert_modules () {to M in $*, do if [-f/etc/modules.d/$m]; then sed ' s/^[^#]/i Nsmod &/'/etc/modules.d/$m | Ash 2>&-| | : Else modprobe $m fi do} default_prerm () {local name name=$ (basename $) | cut-d.-f1) [-f/usr/lib/opkg/info/${name}.prerm-pkg] &&amP;. /usr/lib/opkg/info/${name}.prerm-pkg for i in ' cat/usr/lib/opkg/info/${name}.list | grep "^/etc/init.d/"; Do $i disable $i stop doing} default_postinst () {local pkgname rusers pkgname=$ (echo $ basen Ame $) |  cut-d.-f1) rusers=$ (grep require-user: ${ipkg_instroot}/usr/lib/opkg/info/${pkgname}.control) [-N ' $rusers]
            && {Local user group UID GID for a in $ (echo $rusers | sed "s/require-user://g"); User= "" group= "for B in $ (echo $a | sed" s/://g ");  Do local ugname ugid ugname=$ (echo $b | cut-d=-f1) ugid=$ (echo $b |
                    -d=-f2) [-Z "$user"] && {user= $ugname uid= $ugid
                    Continue} gid= $ugid [-N ' $gid '] && { Group_exists $ugname | |
    Group_add $ugname $gid            } [-Z ' $gid] && {group_add_next $ugname g
                Id=$?
                    [-Z ' $group] && {user_exists $user | | user_add $user "$UID" $gid
            group= $ugname Continue} group_add_user $ugname $user Done} [F ${ipkg_instroot}/usr/lib/opkg/info/${pkgname}.postinst-pkg] && (. $ {ipkg_instroot}/usr/lib/opkg/info/${pkgname}.postinst-pkg) [-N "${ipkg_instroot}"] | | Rm-f/tmp/luci-indexcache 2>/dev/null ["$PKG _upgrade" = "1"] | | For i in ' Cat ${ipkg_instroot}/usr/lib/opkg/info/${pkgname}.list | grep "^/etc/init.d/"; Do [-N "${ipkg_instroot}"] && $ (which bash) ${ipkg_instroot}/etc/rc.common ${ipkg_instroot} $i enable; \ [-N "${ipkg_instroot}"] | | {$i enable $i start} done returN 0} include () {Local file with file in $ (ls $1/*.sh 2>/dev/null); does. $file Done} find_mtd_i Ndex () {local part= $ (grep "\ $1\" "/PROC/MTD | awk-f: ' {print} ')" "Local index=" ${part# #mtd} "Echo ${ind EX}} find_mtd_part () {Local index=$ (find_mtd_index "$") local Prefix=/dev/mtdblock [-d/dev/mtdblock] & 
    amp;& Prefix=/dev/mtdblock/echo "${index:+ $PREFIX $index}"} group_add () {local name= "$" local gid= "$" Local RC [-F "${ipkg_instroot}/etc/group"] | | return 1 [-n ' $IPKG _instroot] | |
    Lock/var/lock/group echo "${name}:x:${gid}:" >> ${ipkg_instroot}/etc/group rc=$? [-N ' $IPKG _instroot] | | Lock-u/var/lock/group return $RC} group_exists () {GREP-QS "^${1}:" ${ipkg_instroot}/etc/group} group_add_n Ext () {Local GID Gids gid=$ (grep-s "^${1}:" ${ipkg_instroot}/etc/group | cut-d:-F3) [-N ' $gid]] &&am P return $gid gids=$ (cat ${ipkg_instrooT}/etc/group | cut-d:-F3) gid=100 while [-N "$ (echo $gids | grep $gid) "]; Do gid=$ (GID + 1) is done Group_add the $gid return $gid} group_add_user () {Local GRP delim= , "grp=$ (grep-s" ^${1}: "${ipkg_instroot}/etc/group) [-Z" $ (echo $grp | Cut-d:-f4 | grep $) "] | | return [-N] $ (echo $grp | grep ": $") "] && delim=" "[-N" $IPKG _instroot] | | LOCK/VAR/LOCK/PASSWD sed-i "s/$grp/$grp $delim$2/g" ${ipkg_instroot}/etc/group [-N "$IPKG _instroot"] | | Lock-u/var/lock/passwd} user_add () {local name= ' ${1} ' local uid= ' ${2} ' local gid= ' ${3} ' local desc= ' $ 
        {4:-$1} ' local home= ' ${5:-/var/run/$1} ' local shell= ' ${6:-/bin/false} ' local RC [-Z ' $uid] && { uids=$ (Cat ${ipkg_instroot}/etc/passwd | cut-d:-F3) uid=100 while [-N "$ (echo $uids | grep $uid) "];
  Do uid=$ ((UID + 1) is done} [-Z "$gid"] && gid= $uid  [f "${ipkg_instroot}/etc/passwd"] | | return 1 [-n ' $IPKG _instroot] | |
    LOCK/VAR/LOCK/PASSWD echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${ipkg_instroot}/etc/passwd
    echo "${name}:x:0:0:99999:7:::" >> ${ipkg_instroot}/etc/shadow rc=$? [-N ' $IPKG _instroot] | | Lock-u/var/lock/passwd return $RC} user_exists () {GREP-QS "^${1}:" ${IPKG_INSTROOT}/ETC/PASSWD} [-Z "$IPK G_instroot "-a-f/lib/config/uci.sh] &&.
 /lib/config/uci.sh

The script contains commonly used config, config_get, etc. functions,
It is necessary to configure the OPENWRT system frequently.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.