OS 3.28 Help with varialbe substitution

Hi

Trying to get the following working

[timh@MikroTik] > :local i 0;:put $i;/ip firewall mangle remove $i;               
0                                                                                 
no such item

Though the following works

[timh@MikroTik] > :local i 0;:put $i;/ip firewall mangle remove 0;                 
0

Not sure what I am doing wrong. Any help would be greatly appreciated.

what exactly do you want to do?..

Indices of items aren’t really numeric as you use the on the command line. See below:

[admin@MikroTik] > :foreach rule in=[/ip firewall mangle find] do={ :put $rule }
*3
*4

When passing an index on the CLI (or in a script) directly as a numerical value it gets translated into an index, but when you’re using a variable, it expects it to already be in the right format. When manipulating items in a script, first fetch their IDs (usually via find). Shouldn’t make much of a difference for your scripting needs, really - if you want to manipulate a rule with a particular offset to 0, you might as well hard code that right into the statement rather that use a variable.

HTH,
Felix

try

:local i [:tostr 0]

just interesting…

I was trying to run the following script

/ip firewall mangle print without-paging
:for i from=0 to={[/ip firewall mangle print count-only] - 1} do { \ 
    /ip firewall mangle remove $i}

I have found another way of doing it. Though was making sure that I had not missed something as there were other scripts on the Wiki that did something similar that also did not work.

Nope

:local i [:tostr 0];:put $i;/ip firewall mangle remove $i;

Didn’t work either :slight_smile:

/ip firewall mangle remove [/ip firewall mangle find comment=“something”]

You have to find the records you want to remove first. Find them by anything you wish, comments, in-interface, out-interface, ip addresses, etc - any combination basically to select what you want removed.

print doesnt work* in scripts, there is no buffer to hold the printed lists indexes to use in the next command.