Page 1 of 1
Variables - why does it not work?
Posted: Sat Jan 15, 2022 12:06 am
by quotengrote
Hi,
i have that simple Script for netwatch:
:log error "wireguard-tunnel down: wireguard_s2s_ag";
/interface/wireguard/disable wireguard_s2s_ag;
:delay 4000ms;
/interface/wireguard/enable wireguard_s2s_ag;
:log info "Restart wireguard-tunnel: wireguard_s2s_ag";
The first one works, the second not.
# set variables
:local wg_interface wireguard_s2s_ag;
:log error "wireguard-tunnel down: $wg_interface";
/interface/wireguard/disable $wg_interface;
:delay 4000ms;
/interface/wireguard/enable $wg_interface;
:log info "Restart wireguard-tunnel: $wg_interface";
Does anyone has an idea?
Wishes
mg
Re: Variables - why does it not work? [SOLVED]
Posted: Sat Jan 15, 2022 12:12 am
by Jotne
Remove the underscore in the variable name.
Use
:local wginterface wireguard_s2s_ag
And
1. You can remove ; at end of all line. Only needed while there are multiple command on same line.
2.
:delay 4000ms is the same as
:delay 4s
Re: Variables - why does it not work?
Posted: Sat Jan 15, 2022 12:25 am
by Sob
It's also documented:
Valid characters in variable names are letters and digits. If variable name contains any other character, then variable name should be put in double quotes.
But it would be so much better, it RouterOS scripting had some useful error reporting. Just imagine how it could be, even simple "syntax error at line 1, column 9" would be so beautiful. Maybe one day...
Re: Variables - why does it not work?
Posted: Sat Jan 15, 2022 12:32 am
by quotengrote
It worked, thank you very much.
But it would be so much better, it RouterOS scripting had some useful error reporting. Just imagine how it could be, even simple "syntax error at line 1, column 9" would be so beautiful. Maybe one day...
That would be great...
Wishes
mg
PS:
For the docs:
# set variables
:local wginterface wireguard_s2s_ag
# Valid characters in variable names are letters and digits. If variable name contains any other character, then variable name should be put in double quotes.
:log error "wireguard-tunnel down: $wginterface"
/interface/wireguard/disable $wginterface
:delay 4s
/interface/wireguard/enable $wginterface
:log info "Restart wireguard-tunnel: $wginterface"
Re: Variables - why does it not work?
Posted: Sat Jan 15, 2022 12:58 am
by msatter
It does....
If you past, notice the {}, this into Terminal it will show you in red the locations where there are errors in the syntax.
{
# set variables
:local wg_interface wireguard_s2s_ag;
:log error "wireguard-tunnel down: $wg_interface";
/interface/wireguard/disable $wg_interface;
:delay 4000ms;
/interface/wireguard/enable $wg_interface;
:log info "Restart wireguard-tunnel: $wg_interface";
}
Showing:
terminal_errors.JPG
...and notice that the log lines do not produce an error because there the underscore is between " ", as documented.
Re: Variables - why does it not work?
Posted: Sat Jan 15, 2022 12:59 am
by quotengrote
Did not know that, thx.
Re: Variables - why does it not work?
Posted: Sat Jan 15, 2022 1:37 am
by Sob
But that's not even remotely user friendly. If I need to write some longer script, I can't be copying and pasting the whole thing in terminal over and over again, that's terrible. Better than nothing, maybe, but not by much.
Re: Variables - why does it not work?
Posted: Sat Jan 15, 2022 12:29 pm
by eworm
Well, you can edit the scripts in CLI...
/system script edit Script-Name source
That way you have syntax highlighting all the time.
Re: Variables - why does it not work?
Posted: Sat Jan 15, 2022 6:55 pm
by Sob
Thanks, I didn't know about that. Quick test says that it's getting closer to something good. For now I dare to say usable. There are some flaws, most obvious is that it saves with Unix line endings, which messes up display in WinBox, where it becomes just one long line. Another problem is with clipboard shortcuts, where in order to have working copy (Ctrl+Ins), I have to use ssh, it doesn't work in Terminal in WinBox. But on the upside, it looks like it will fix troubles with "silent death".