The condition is not met, why does the script keep executing?

Hello,

The basic routing settings are shown in the attached picture. I edited a script to modify the IP Route when the gateway IP obtained by the DHCP-Client changes. In actual use, it is found that even if the conditions are not met, the gateway of the IP Route will still be modified repeatedly. Why? ?
script:”Update gateway”

{
:local newip
:local oldip
:local status
:local x “2”
:for i from=1 to=$x do={
:set status [/ip dhcp-client get [/ip dhcp-client find where interface=(“wlan”.$i)] status]
:if ($status=“bound”) do={
:set newip [/ip dhcp-client get [/ip dhcp-client find where interface=(“wlan”.$i)] gateway]
:set oldip [/ip route get [/ip route find dst-address=0.0.0.0/0 static=yes active=yes distance=1 comment=(“tel-”.$i)] gateway]
:set oldip [pick $oldip 0 12]}
:if ($newip != $oldip) do={
/ip route set [/ip route find comment=(“tel-”.$i)] gateway=($newip.“%”.(“wlan”.$i))
/ip route set [/ip route find comment=(“tel-”.“$i$i$i”)] gateway=($newip.“%”.(“wlan”.$i))}}}
}
1.png
2.png
3.png
4.png
5.png

It seem that your picture were added as thumbnail. Not able to see details.

Hello,

Thanks for the reply, I will upload the screenshot again now.
4.png
5.png
3.png
1.png
2.png

Please consider to correctly indent/format the script or is unreadable

Picking with fixed size, for example,
on tel-1 give 192.168.43.1 and on tel-2 give 192.168.43.82 with missing “2” and “192.168.43.82” is different to “192.168.43.8”
or
on tel-2 give 192.168.43.82 and on tel-1 give 192.168.43.1% with added “%” and “192.168.43.1” is different to “192.168.43.1%”

You must split at %, not a generic position

# simply replace
:set oldip [pick $oldip 0 12]}

# with
:set oldip [:pick $oldip 0 [:find $oldip "%" -1]]}

Fixed version

:local newip
:local oldip
:local status
:local x 2
:for i from=1 to=$x do={
    /ip dhcp-client
    :set status [get [find where interface="wlan$i"] status]
    :if ($status = "bound") do={
        /ip dhcp-client
        :set newip [get [find where interface="wlan$i"] gateway]
        /ip route
        :set oldip [:tostr [get [find where dst-address=0.0.0.0/0 static=yes active=yes distance=1 comment="tel-$i"] gateway] ]
        :set oldip [:pick $oldip 0 [:find $oldip "%" -1]]
    }
    :if ($newip != $oldip) do={
        /ip route
        set [find where comment="tel-$i"] gateway="$newip%wlan$i"
        set [find where comment="tel-$i$i$i"] gateway="$newip%wlan$i"
    }
}

Hello,

Thanks for the guidance, but the script keeps getting executed.
scripts.png

I check now, probably is data conversion…

“find” everytime return an array (from 0 to x values) … I fix previous post adding [:tostr … ]

Hello,

Thank you very much, the test passed.