Community discussions

MikroTik App
 
Zetera
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 52
Joined: Sat Apr 21, 2018 6:57 pm

If, then, else Scripting difficulties. Please Help!

Tue May 08, 2018 10:59 pm

:if ( [/file find flash] = yes) do={ certificate import file-name=flash/placeholder passphrase="placeholder"
certificate import file-name=flash/placeholder passphrase="placeholder"
certificate import file-name=flash/placeholder passphrase="placeholder"
certificate import file-name=flash/placeholder passphrase="placeholder"
certificate import file-name=flash/placeholder passphrase="placeholder" } else={ certificate import file-name=placeholder passphrase="placeholder"
certificate import file-name=placeholder passphrase="placeholder"
certificate import file-name=placeholder passphrase="placeholder"
certificate import file-name=placeholder passphrase="placeholder"
certificate import file-name=placeholder passphrase="placeholder" }

Basically trying to see if the flash folder is there. (This is because older r1 MikroTiks don't have the flash folder.) and then if it isn't there go about performing the next set of commands instead. Doing it hilariously wrong atm. Need help getting this right. Any help is much appreciated!
Last edited by Zetera on Tue May 08, 2018 11:35 pm, edited 1 time in total.
 
Zetera
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 52
Joined: Sat Apr 21, 2018 6:57 pm

Re: Scripting difficulties. Please Help!

Tue May 08, 2018 11:10 pm

Basically "If flash folder exists; do 'this set of commands'; else 'this set of commands'.

any way to get this to work will be much appreciated. (An explanation would be awesome as well!
 
looka
Frequent Visitor
Frequent Visitor
Posts: 77
Joined: Sat Apr 14, 2018 3:33 pm

Re: If, then, else Scripting difficulties. Please Help!

Wed May 09, 2018 10:39 am

find gets you an array of results. check if this array's length is > 0
:if([:len [/file find flash]]>0) do={#stuff}
wiki scripting page is also useful.
 
Zetera
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 52
Joined: Sat Apr 21, 2018 6:57 pm

Re: If, then, else Scripting difficulties. Please Help!

Wed May 09, 2018 3:16 pm

It seems to work just fine on R1s (my old script worked on R1s. But on R2s and newer it can't seem to find the file at all.
You do not have the required permissions to view the files attached to this post.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10240
Joined: Mon Jun 08, 2015 12:09 pm

Re: If, then, else Scripting difficulties. Please Help!  [SOLVED]

Wed May 09, 2018 4:01 pm

Normally the only safe way to handle things like this in scripting is to use the do { statements } on-error={ statements } construct.
Otherwise when an error occurs during expression evaluation the script simply stops, so you cannot test for conditions like nonexisting items safely.
 
Zetera
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 52
Joined: Sat Apr 21, 2018 6:57 pm

Re: If, then, else Scripting difficulties. Please Help!

Wed May 09, 2018 4:03 pm

I got it to work!
:if ( [ :len [ /file find name~"flash" ] ] >0 ) do={ /certificate import file-name=flash/example.crt passphrase="" } else={ /certificate import file-name=example.crt passphrase="" }
 
Zetera
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 52
Joined: Sat Apr 21, 2018 6:57 pm

Re: If, then, else Scripting difficulties. Please Help!

Wed May 09, 2018 4:15 pm

Normally the only safe way to handle things like this in scripting is to use the do { statements } on-error={ statements } construct.
Otherwise when an error occurs during expression evaluation the script simply stops, so you cannot test for conditions like nonexisting items safely.
Wish I knew this existed before I put an entire day into the previous script. Yes this makes sense. I'll use this one instead seeing as it works as well and is less likely to do nothing.

Who is online

Users browsing this forum: No registered users and 73 guests