You can make it:
has_ports() {
ls -A "/sys/devices/virtual/net/$1/brif/" 2> /dev/null | grep -q .
}
has_ports br0 || brctl delbr br0
Or:
if ! has_ports br0; then
brctl delbr br0
fi
(note that you do need the `-A` as interface names are allowed to start with `.`).
To count the number of ports:
With `zsh`:
ports=(/sys/devices/virtual/net/$bridge/brif/*(DN:t))
printf '%s\
' "$#ports ports in $bridge"
`(:t)` to only have the file names instead of full paths.
With `bash`:
shopt -s nullglob dotglob
ports=("/sys/devices/virtual/net/$brige/brif/"*)
printf '%s\
' "${#ports[@]} ports in $bridge"
(note that ports contains the full paths as `bash` has no equivalent for `zsh`'s `:t`).
Both would return 0 for a bridge that doesn't exist.