Uci interation - shell

OpenWRT forum,

Is there a more elegant way to iterate over UCI entries using shell/bash? The code below seems a little excessive, I wouldn't think it would need to take 2+ calls to walk down an array/list..

I tried doing things like uci show dhcp.@host[@] or dhcp.@host[*], but none seem to work.

for entry in ` **uci -N show dhcp | egrep '.ip=' | cut -d. -f1-2 **  `; do
  echo -ne "` uci get ${entry}.mac ` "
  echo ` uci get ${entry}.name `
done

It works and occupies 4 strings, so it is nice solution. In any case you should call uci show, uci get. You can access array elements like uci get aaa.@bbb[0] The main problem is to get length of array.

yourCustomFunction() {
config_get nameVar "$1" 'name'
config_get ipVar "$1" 'ip'
}

. /lib/functions.sh
config_load dhcp
config_foreach yourCustomFunction 'host'
4 Likes

genius, thank you!

#!/bin/sh

. /lib/functions.sh


yourCustomFunction() {
  config_get nameVar "$1" 'name'
  config_get ipVar "$1" 'ip'

  echo "$nameVar $ipVar"
}

config_load dhcp
config_foreach yourCustomFunction 'host'

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.