Multiple IF Statement match and action accordingly

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.

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

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"
}
}
}

thx, it worked ok.

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.

thx a lot, awesome
I am using it for my own watchdog, thx bro

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 = ||

Hi
it’s run in terminal perfectly but not work in script!!!
why???

what you try to run?
paste here

: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.

It’s only a fragment, it’s not related with previous post, and without publish anyting you ask why not work???

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

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.

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
}

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"
    }
}

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 :confused:

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

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"
    }
}

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