Community discussions

MikroTik App
 
cicserver
Member
Member
Topic Author
Posts: 303
Joined: Sun Jul 24, 2011 12:04 pm

Multiple IF Statement match and action accordingly

Mon Apr 04, 2016 10:33 am

How can i match two statements and if BOTH equals to xxx, act accordingly.
Example:

If variable 1 and Variable 2 values in environment are equal to DOWN, then execute script.
I search forum and google, but found only one statement match examples.
 
User avatar
nz_monkey
Forum Guru
Forum Guru
Posts: 2182
Joined: Mon Jan 14, 2008 1:53 pm
Location: Over the Rainbow
Contact:

Re: Multiple IF Statement match and action accordingly

Mon Apr 04, 2016 11:07 am

if (condition) and (condition) do=
 
cicserver
Member
Member
Topic Author
Posts: 303
Joined: Sun Jul 24, 2011 12:04 pm

Re: Multiple IF Statement match and action accordingly

Mon Apr 04, 2016 11:13 am

if (condition) and (condition) do=
Example:
variable stored in script environment are
var1 = up
var2 = down

I want if both variables value is equal to DOWN, then print "net is down"

:local var1 [/sys scr environment print value where name=status1];
:local var2 [/sys scr environment print value where name=status2];

:if $var1 and $var2 = DOWN do={
    :put "down";
else
:put "still working"
}
something like abobe
 
User avatar
aacable
Member
Member
Posts: 435
Joined: Wed Sep 17, 2008 11:58 am
Location: ISLAMIC Republic of PAKISTAN
Contact:

Re: Multiple IF Statement match and action accordingly

Mon Apr 04, 2016 11:53 am

How can i match two statements and if BOTH equals to xxx, act accordingly.
Example: If variable 1 and Variable 2 values in environment are equal to DOWN, then execute script. I search forum and google, but found only one statement match examples.
Try this.
:local var1 [/system script environment get [/system script environment find name="enviornment-1"] value];
:local var2 [/system script environment get [/system script environment find name="enviornment-2"] value];
:if (($var1 = "DOWN") && ($var2 = "DOWN")) do={ 
:log info "DOWN"
} else={
:log info "UP"
}
}
}
 
cicserver
Member
Member
Topic Author
Posts: 303
Joined: Sun Jul 24, 2011 12:04 pm

Re: Multiple IF Statement match and action accordingly

Tue Apr 05, 2016 11:55 am

thx, it worked ok.
 
profound
just joined
Posts: 2
Joined: Wed Apr 04, 2018 1:11 pm

Re: Multiple IF Statement match and action accordingly

Wed Sep 23, 2020 10:52 am

Thanks for the post.

I've extended it slightly, to log events, and also only change the fan if the temperature warrants the change, and if the fan is not already being used. This way you can have the main fan faster, and a slower, quieter auxilary fan in place, for when the device is adequately cooled. I have this script scheduled (/system script run FanControl) to run every 15 seconds.
:if (([/system health get cpu-temperature]> 60) && ([/system health get use-fan]="auxiliary")) do={
  [/system health set use-fan="main"];
  log info "CPU is hot, switching to main fan";
}
:if (([/system health get cpu-temperature]< 55) && ([/system health get use-fan]="main")) do={
  [/system health set use-fan="auxiliary"];
  log info "CPU is cool, switching to auxiliary fan";
}
 
User avatar
aacable
Member
Member
Posts: 435
Joined: Wed Sep 17, 2008 11:58 am
Location: ISLAMIC Republic of PAKISTAN
Contact:

Re: Multiple IF Statement match and action accordingly

Wed Sep 23, 2020 12:35 pm

Thanks for the post.

I've extended it slightly, to log events, and also only change the fan if the temperature warrants the change, and if the fan is not already being used. This way you can have the main fan faster, and a slower, quieter auxilary fan in place, for when the device is adequately cooled. I have this script scheduled (/system script run FanControl) to run every 15 seconds.
:if (([/system health get cpu-temperature]> 60) && ([/system health get use-fan]="auxiliary")) do={
  [/system health set use-fan="main"];
  log info "CPU is hot, switching to main fan";
}
:if (([/system health get cpu-temperature]< 55) && ([/system health get use-fan]="main")) do={
  [/system health set use-fan="auxiliary"];
  log info "CPU is cool, switching to auxiliary fan";
}
Thx for the contribution.
 
User avatar
rushlife
Member Candidate
Member Candidate
Posts: 254
Joined: Thu Nov 05, 2015 12:30 pm

Re: Multiple IF Statement match and action accordingly

Fri Apr 09, 2021 8:21 am

How can i match two statements and if BOTH equals to xxx, act accordingly.
Example: If variable 1 and Variable 2 values in environment are equal to DOWN, then execute script. I search forum and google, but found only one statement match examples.
Try this.
thx a lot, awesome
I am using it for my own watchdog, thx bro
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3343
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Multiple IF Statement match and action accordingly

Fri Apr 09, 2021 12:00 pm

A cleaned up version.
No need for ; at the end of line, and removed extra parentheses.

{
	:local var1 "DOWN"
	:local var2 "DOWN"
	:if ($var1 = "DOWN" && $var2 = "DOWN") do={ 
		:put "Both are DOWN"
	} else={
		:put "UP"
	}
}
You can any of these: and = &&.
And this is the same:or = ||
 
mehdi2000
just joined
Posts: 8
Joined: Fri Aug 19, 2016 8:54 am

Re: Multiple IF Statement match and action accordingly

Mon May 10, 2021 10:28 pm

Hi
it's run in terminal perfectly but not work in script!!!
why???
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12557
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Multiple IF Statement match and action accordingly

Tue May 11, 2021 12:29 am

what you try to run?
paste here
 
mehdi2000
just joined
Posts: 8
Joined: Fri Aug 19, 2016 8:54 am

Re: Multiple IF Statement match and action accordingly

Tue May 11, 2021 10:52 pm

:if (([$percentage] >= 99) or ($EXDate = 1)) do={
/queue simple set $i max-limit=128k/128k;
}
:if (([$percentage] < 99) and ($EXDate = 0)) do={ /queue simple set $i max-limit="$maxUpload/$maxDownload"}

RB : 951Ui-2HnD
v : 6.48.2
in terminal is ok but in script no.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12557
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Multiple IF Statement match and action accordingly

Wed May 12, 2021 4:52 am

It's only a fragment, it's not related with previous post, and without publish anyting you ask why not work???
 
mehdi2000
just joined
Posts: 8
Joined: Fri Aug 19, 2016 8:54 am

Re: Multiple IF Statement match and action accordingly

Wed May 12, 2021 7:59 am

It's not fragmented .
I wrote this code from aacable code and I don't know why is not working in script ,but work perfectly in terminal
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7186
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Multiple IF Statement match and action accordingly

Wed May 12, 2021 9:14 am

If it is not a fragment, then it is not supposed to work. Script doesn't know anything about all those undefined variables you are trying to use.
 
icepicknz
just joined
Posts: 18
Joined: Thu Jul 29, 2010 1:39 am

Re: Multiple IF Statement match and action accordingly

Fri Sep 10, 2021 2:51 am

I'm trying something similar, is a nested if statement within a do possible?
:global sentfound
:global sentmissing

:local macfound [/ip dhcp-server lease get [find comment="XBOX - 30c Snowmass" status=bound]]
:if ($macfound != "") do={
# :if ($sentfound == 0) do {
:log info "XBOX found"
# }
:set sentfound 1
:set sentmissing 0
} else= {
# :if ($sentmissing = 1) do {
:log info "XBOX missing"
# }
:set sentfound 0
:set sentmissing 1
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12557
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Multiple IF Statement match and action accordingly

Fri Sep 10, 2021 3:00 am

I fix the script, notice the differencies...
:global xstatus
/ip dhcp-server lease
:local macsearch [get [find where comment="XBOX - 30c Snowmass" and status=bound]]
:if ([:len $macsearch] > 0) do={
    :if ($xstatus = "missing") do={
        :log info "XBOX found"
        :set xstatus "found"
    }
} else={
    :if ($xstatus = "found") do={
        :log info "XBOX missing"
        :set xstatus "missing"
    }
}
 
icepicknz
just joined
Posts: 18
Joined: Thu Jul 29, 2010 1:39 am

Re: Multiple IF Statement match and action accordingly

Fri Sep 10, 2021 3:52 am

That script doesn't appear to be working, no "Xbox found" or "Xbox missing"
If I comment your nested if statement it works; so same result as mine.

I know with my script the else wasn't working either, it wouldn't give me the "Xbox Missing".
Second issue with my script, was if I uncommented the nested if statement it wouldn't work either :/

So in summary from your script, this doesn't work...
:global xstatus 1
/ip dhcp-server lease
:local macsearch [get [find where comment="XBOX - 30c Snowmass" and status=bound]]
:if ([:len $macsearch] > 0) do={
    :if ($xstatus = "missing") do={
        :log info "XBOX found"
        :set xstatus "found"
    }
}
But if I remove the nested portion, it works. so has the same problem I have
:global xstatus 1
/ip dhcp-server lease
:local macsearch [get [find where comment="XBOX - 30c Snowmass" and status=bound]]
:if ([:len $macsearch] > 0) do={
#    :if ($xstatus = "missing") do={
        :log info "XBOX found"
#        :set xstatus "found"
#    }
}
Could it be because an item not found shows as "no such item" ?
[admin@OhakuneRTR] /ip dhcp-server lease> get [find where comment="XBOX - 30c Snowmass" and status=bound]
[admin@OhakuneRTR] /ip dhcp-server lease> get [find where comment="NOTHING" and status=bound]
no such item
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12557
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Multiple IF Statement match and action accordingly

Fri Sep 10, 2021 10:05 am

i fix the structure for demonstrate the multiple if statement is doable,
but I do not make the entire script working for your needs

for fix also the rest (do not put a value on the var xstatus on first line, or everytime the value is "1"...)
must used only find, if no result, do not do any error,
and a check must be added if previously xstatus is never defined
:global xstatus
:if ([:typeof $xstatus] = "nothing") do={ :set xstatus "found" }
/ip dhcp-server lease
:local macsearch [find where comment="XBOX - 30c Snowmass" and status=bound]
:if ([:len $macsearch] > 0) do={
    :if ($xstatus = "missing") do={
        :log info "XBOX found"
        :set xstatus "found"
    }
} else={
    :if ($xstatus = "found") do={
        :log info "XBOX missing"
        :set xstatus "missing"
    }
}
 
icepicknz
just joined
Posts: 18
Joined: Thu Jul 29, 2010 1:39 am

Re: Multiple IF Statement match and action accordingly

Fri Sep 10, 2021 1:08 pm

Thanks so much, a bit of playing and it is indeed working

Who is online

Users browsing this forum: No registered users and 8 guests