Modify

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#19578 closed defect (not_a_bug)

uci show / regression / output contains single quotes

Reported by: bittorf@… Owned by: developers
Priority: normal Milestone:
Component: packages Version: Trunk
Keywords: Cc:

Description

older version of uci:

root@bueronas:~ uci show | grep ^system | head -n2
system.@system[0]=system
system.@system[0].hostname=bueronas

now (e.g. r45455)

root@box:~ uci show | grep ^system | head -n2
system.@system[0]=system
system.@system[0].hostname='box'
                           ^^^^^ single quotes

this breaks scripts, e.g.:

uci show network.@switch_vlan[] | grep =switch0

and there is no need for the single quotes

Attachments (0)

Change History (5)

comment:1 Changed 3 years ago by jow

  • Resolution set to not_a_bug
  • Status changed from new to closed

Your scripts are broken, the "uci show" output was *never* meant to be parsed by scripts since it is ambiguous.

  • Instead of parsing system.@system[0].hostname='box' use uci -q get system.@system[0].hostname.
  • Instead of uci show network.@switch_vlan[] | grep =switch0 use [ "$(uci -q get network.@switch_vlan[0].device)" = "switch0" ]

Or, even better, properly use the shell api:
http://wiki.openwrt.org/doc/devel/config-scripting

Last edited 3 years ago by jow (previous) (diff)

comment:2 Changed 3 years ago by bittorf@…

maybe it was not *meant* to be parsed, but the users do that.
if so, a warning message should be displayed:

examples, where users do 'uci show | grep xy'
http://luci.subsignal.org/trac/browser/luci/trunk/contrib/package/freifunk-policyrouting/files/etc/hotplug.d/iface/30-policyrouting?rev=6967
http://stackoverflow.com/questions/28433770/extract-string-from-another-using-grep
https://freifunk-muenster.de/wiki/doku.php?id=nodes:ucibasics
https://forum.freifunk.net/t/tl-wdr4300-lan-ports-als-switch-nutzen/3525
[...]

beside that:
your examples above with "network.@switch_vlan[]" is invalid: i dont know the interface-index 0 - thats the reason for grep'ing.

also:
i have a dynamic added list of 25 alias-ip's in my network-config:
how can i check, if $IP is already added?

uci show network.@alias[] | fgrep ipaddr=10.10.43.33 || add_alias

how should one write this with '/lib/functions.sh' and 'config_get'?

comment:3 Changed 3 years ago by jow

is_switch=0

check_switch() {
  local switchdev
  config_get switchdev "$1" device
  if [ "$switchdev" = switch0 ]; then
    is_switch=1
    return 1
  fi
}

config_load network
config_foreach check_switch switch_vlan

if [ $is_switch -gt 0 ]; then
  echo "One of the vlans refers to switch0"
fi
used_ip=0
check_ip="10.10.43.33"

check_alias() {
  local ipaddr
  config_get ipaddr "$1" ipaddr
  if [ "$ipaddr" = "$2" ]; then
    used_ip=1
    return 1
  fi
}

config_load network
config_foreach check_alias alias "$check_ip"

if [ $used_ip -gt 0 ]; then
  echo "$check_ip is already configured as alias"
fi

comment:4 Changed 3 years ago by jow

Alternative examples:

local i=0
local switchdev

while switchdev="$(uci -q get network.@switch_vlan[$i].device)"; do
  if [ "$switchdev" = switch0 ]; then
    # blah
    break
  fi
  : $((i++))
done
local i=0
local ip

while ip="$(uci -q get network.@alias[$i].ipaddr)"; do
  if [ "$ip" = 10.10.43.33 ]; then
    # already used
    break
  fi
  : $((i++))
done

comment:5 Changed 3 years ago by bittorf@…

thanks so much for the examples, i go for the last one.

i also understand now and agree, that 'uci show' is really for the user,
so can have arbitrary formatting (colors, comments) and is not suited for parsing.

Add Comment

Modify Ticket

Action
as closed .
The resolution will be deleted. Next status will be 'reopened'.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.