There are several things wrong in the script.
The first one is that $1 is used and that is a special variable in a function and contains the first value transferred by the call function. So using it here invalids the :local 1. Use sound variable names.
Actually was curious the ":local 1 ..." usage – I would have thought be the error here. But instead it overrides any argument that would have been in $1. Guess that make sense but variables starting with a number is something I avoid. But for reassignment of the positional arguments, unintentionally cleaver.
I have seen this sequence in this forum to convert values of the variables "tx-total" and "rx-total" so that they can be displayed correctly by a person regardless of the value obtained. (Bytes or KiB or MiB or GiB).
I think the script problem is it's not really a function any more. So if you change "human" from :local to :global, then use the $human function whatever you want. I added some protection around invalid types being provided, but assuming the rest of the B,K,M logic is right...
:global human do={
:if ([:typeof $1]="nothing") do={
:error "must provide a byte value to humanize";
};
:local input [:tonum $1];
:if ([:typeof $input]!="num") do={
:error "cannot convert $1 to number";
};
:local q;
:local r;
:local output;
:if ($input<1024) do={
:set $output "$input B";
} else={
:if ($input<1048576) do={
:set q ($input/1024);
:set r ($input-$q*1024);
:set r ($r/102);
:set output "$q.$r KiB";
} else={
:if ($input<1073741824) do={
:set q ($input/1048576);
:set r ($input-$q*1048576);
:set r ($r/104858);
:set output "$q.$r MiB"
} else={
:set q ($input/1073741824);
:set r ($input-$q*1073741824);
:set r ($r/107374182);
:set output "$q.$r GiB"
}
}
}
:return $output
};
So to use this you "call" $human with the results of a "/interface/get" command that returns with bytes. If you want to print it to the console (instead of using $human in a script), you can add a put like below to see the returned value from $human:
:put [$human [/interface get lte2 tx-byte]]
# 1.4 GiB
or assign it to variable to use and/or use string interpolation:
{
# in a script, block, you can assign it to variable to then use in email script
:local ltetx [$human [/interface get lte2 tx-byte]]
:put $ltetx
# alternatively in a string using interpolation
:local strtxrx "TX is $([$human [/interface get lte2 tx-byte]] and RX is $([$human [/interface get lte2 rx-byte]]))
}