Community discussions

MikroTik App
 
BlastJosh
just joined
Topic Author
Posts: 5
Joined: Thu Jan 12, 2023 11:40 pm

Nested If Statement requiring less brackets?

Thu Jan 19, 2023 6:36 pm

Currently I'm doing a small script just to check the logs for a certain error message and then email out to our admin that said error had occurred. However, this mikrotik script is saying I need one less curly bracket, but it has two open ones. It's throwing a problem with that last bracket and I have no idea why.
:if ( $lastTime = "" ) do={ 
    :set lastTime $currentTime 
    :local tempMessage            
    :set $tempMessage [/log get [ :pick $currentBuf ($currentLineCount-1)]
    :local found
    :set $found [:find $tempMessage "Down to Init"]
    :if ($found > 0) do={
        :log info "Found a thing!";
        :set message [/log get [ :pick $currentBuf ($currentLineCount-1) ] message]
        }
} 
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Nested If Statement requiring less brackets?

Thu Jan 19, 2023 7:46 pm

I'ts another script you found on internet?
viewtopic.php?p=978326#p978472
Last edited by rextended on Thu Jan 19, 2023 7:48 pm, edited 2 times in total.
 
BlastJosh
just joined
Topic Author
Posts: 5
Joined: Thu Jan 12, 2023 11:40 pm

Re: Nested If Statement requiring less brackets?

Thu Jan 19, 2023 7:47 pm

Sorry yes I should have clarified that it was a script it used, I'm trying to figure out how I broke it on accident.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Nested If Statement requiring less brackets?

Thu Jan 19, 2023 7:49 pm

Where are defined lastTime, currentTime, currentBuf, currentLineCount, etc?

Why do you have to waste peoples time, it's not even the complete script, it's just a piece and the error could be before (or after).
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Nested If Statement requiring less brackets?

Thu Jan 19, 2023 7:53 pm

Just some critical errors, without check the others.
:if ( $lastTime = "" ) do={ 
    :set lastTime $currentTime 
    :local tempMessage            
    :set $tempMessage [/log get [ :pick $currentBuf ($currentLineCount-1)]
#        ^ error, $ must not be used on global, local or set               ^ missing ]
    :local found
    :set $found [:find $tempMessage "Down to Init"]
#        ^ error, $ must not be used on global, local or set
    :if ($found > 0) do={
        :log info "Found a thing!";
        :set message [/log get [ :pick $currentBuf ($currentLineCount-1) ] message]
        }
} 
 
BlastJosh
just joined
Topic Author
Posts: 5
Joined: Thu Jan 12, 2023 11:40 pm

Re: Nested If Statement requiring less brackets?

Thu Jan 19, 2023 7:58 pm

I has assumed the error was about the curly brackets. I'll post in my full code bellow. I modified this addresses code to work for what I need https://help.mikrotik.com/docs/display/ ... g+examples.
:global lastTime;

:global currentBuf [ :toarray [ /log find buffer=ospf  ] ] ;
:global currentLineCount [ :len $currentBuf ] ;
:global currentTime [ :totime [/log get [ :pick $currentBuf ($currentLineCount -1) ] time   ] ];

:global message "";
:log info "Starting";
:if ($lastTime = "") do={
    :set lastTime $currentTime;
    :local tempMessage;
    :set $tempMessage [/log get [ :pick $currentBuf ($currentLineCount-1)];
    :local found;
    :set $found [:find $tempMessage "Down to Init"];
    :if ($found > 0) do={
             :set message [/log get [ :pick $currentBuf ($currentLineCount-1) ] message];
    }

}
:if (!($found >0) do={
   :if ($lastTime < $currentTime ) do={
       :set lastTime $currentTime ; 
       :local tempMessage;
       :set $tempMessage [/log get [ :pick $currentBuf ($currentLineCount-1)];
       :local found;
       :set $found [:find $tempMessage "Down to Init"]
       :if ($found > 0) do={
             :set message [/log get [ :pick $currentBuf ($currentLineCount-1) ] message];
                   }
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Nested If Statement requiring less brackets?

Thu Jan 19, 2023 8:01 pm

I has assumed the error […]
Don't assume nothing, you must provide everytime full script, not a piece, or at least write that is only a piece...

The problem, on that fragment, is the missing ] as already noticed on previous post.

also on what you post is missing the ) before "do"
:if (!($found >0) do={
Last edited by rextended on Thu Jan 19, 2023 8:06 pm, edited 2 times in total.
 
BlastJosh
just joined
Topic Author
Posts: 5
Joined: Thu Jan 12, 2023 11:40 pm

Re: Nested If Statement requiring less brackets?

Thu Jan 19, 2023 8:05 pm

Thank you very much for both your time and patience, I will try to do better in the future!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Nested If Statement requiring less brackets?

Thu Jan 19, 2023 8:06 pm

and also is missing one ] on the same instruction on second "if" cycle
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Nested If Statement requiring less brackets?

Thu Jan 19, 2023 8:07 pm

I will try to do better in the future!
no one is born learned, even I can make mistakes,
and if I didn't, I would worry...

Feel free to ask what you want on the forum.
For examples of how to write code read my snippets which have the link in my signature and see scripts from other users on the forum

Who is online

Users browsing this forum: No registered users and 26 guests