wifinigel.MikrotikScripting/scripts/v6/ch9-03-mutiple-args-array.rsc

50 lines
1.4 KiB
Text
Raw Permalink Normal View History

2023-01-19 21:27:15 +00:00
# filename: ch9-03-multiple-args-array.rsc
# Let's create function to receive a single array arg
# --- start of function ---
:global PrintInterfaceAttrsFunc do={
2023-03-06 20:56:48 +00:00
# verify arg is correct type
2023-01-19 21:27:15 +00:00
if ([:typeof $1] != "array") do={
:error "Function argument is not an array!";
}
2023-03-06 20:56:48 +00:00
# extract values from array passed as arg
2023-01-19 21:27:15 +00:00
:local Args $1;
:local InterfaceName ($Args->"ifname");
:local InterfaceSpeed ($Args->"ifspeed");
:local InterfaceIp ($Args->"ifip");
# check args are correct data types
if ([:typeof $InterfaceName] != "str") do={
:error "InterfaceName argument not a string!";
}
if ([:typeof $InterfaceSpeed] != "str") do={
:error "InterfaceSpeed argument not a string!";
}
if ([:typeof $InterfaceIp] != "str") do={
:error "InterfaceIp argument not a string!";
}
# data type checks passed, print the interface data
2023-02-19 13:53:15 +00:00
:put "$InterfaceName: IP address = $InterfaceIp, speed = \
$InterfaceSpeed";
2023-01-19 21:27:15 +00:00
}
# --- end of function ---
# Let's call the function with some sample values in an array
2023-03-06 20:56:48 +00:00
:local Args { ifname="WAN1"; ifspeed="100Mbps"; \
2023-02-19 13:53:15 +00:00
ifip="192.168.99.1/24" };
2023-01-19 21:27:15 +00:00
$PrintInterfaceAttrsFunc $Args;
# Let's call the function with some other values in an array
2023-03-06 20:56:48 +00:00
:set Args { ifname="ether2-LAN"; ifspeed="1000Mbps"; \
ifip="172.16.1.254/24" };
2023-01-19 21:27:15 +00:00
$PrintInterfaceAttrsFunc $Args;
# Cleanup global namespace
:set PrintInterfaceAttrsFunc;