So you are working on it? I am waiting with high expectationelse if and switch case are not implemented at the moment
local myFunc do={put $1}
if ($n=1) do={$MyFunc "A"}
if ($n=2) do={$MyFunc "B"}
if ($n=3) do={$MyFunc "C"}
if ($n!=1 & $n!=2 & $n!=3) do={$MyFunc "Other"}
:global counter 0
:global ifElseWorkAround do={
#if
if ($counter > 180) do={
#your code
:set counter ($counter+1)
:return null;
}
#elseif
if ($counter > 90) do={
#your code
:set counter ($counter+2)
:return null;
}
#elseif
if ($counter > 45) do={
#your code
:set counter ($counter+3)
:return null;
}
# Repeat the elseif code as many as you want
# Finally, put the code that you want to execute if none of the above conditions are met
# else code here
:set counter ($counter+4)
};
$ifElseWorkAround;
&WorkARound, mutch better then if else if else....
...
I am sorry you two, but how and why would either of those two code samples work as an ELSE IF?Late, but I will leave the answer to others because I did not find any convincing answer
...
if ($b1) do={
# if $b1 then this
} else={
if ($b2) do={
# else if $b2 then this
} else={
# else then this
}
}
:global n 2
{
:local MyFunc do={ :put $1; :set n 0 }
:local MyFuncU do={ :put "Out of range (1-3) values for N ($1)"; :set n 0 }
:local switch true
:if ($n=1 and $switch) do={ [$MyFunc "A"]; :set switch false }
:if ($n=2 and $switch) do={ [$MyFunc "B"]; :set switch false }
:if ($n=3 and $switch) do={ [$MyFunc "C"]; :set switch false }
:if ($switch) do={ [$MyFuncU $n] }
}
I guess your change there fixes the issue I had about if $n is equal to multiple cases at the same time. But it still feels like you are making a basic IF ELSEIF ELSE more complicated than it has to be.The better is a @SiB variant, because you can change, or is changed, the tested value.
Like "elseif" or switch are executed only one instance.
...
Both Yes, good deduction, and also No:I guess your change there fixes the issue I had about if $n is equal to multiple cases at the same time.
The better is a @SiB variant, because you can change, or is changed, the tested value.
Like "elseif" or switch are executed only one instance.
(SiB errors fixed, is valid code)Code: Select all:global n 2 { :local MyFunc do={ :put $1; :set $n 0 } :local MyFuncU do={ :put "Out of range (1-3) values for N ($1)"; :set $n 0 } :local switch true :if ($n=1 && switch) do={ $MyFunc "A"; :set $switch false } :if ($n=2 && switch) do={ $MyFunc "B"; :set $switch false } :if ($n=3 && switch) do={ $MyFunc "C"; :set $switch false } :if (switch) do={ $MyFuncU $n } }
/system scheduler add interval=3s name=wan1-autoqos on-event="/system/script/run wan1-autoqos" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-time=startup
:local spdmin 512
:local spdmax 51200
:local spdsafe 2500
:local pingcount 3
:local movingaverage 2
:global wan1qos
:global avgpacketloss
:local curpacketloss
:local adjamt 0
:local spdadj
:local lastqos $wan1qos
#:log info ("")
:if ([/interface/ethernet/get ether1_wan running]=true) do={
:local count [/ping address="8.8.4.4" interface="ether1_wan" src-address="192.168.8.100" size=28 interval=1 count=$pingcount]
#:local count 3
:set curpacketloss (($pingcount - $count) * 100 / $pingcount)
#:log info "Percent of pings lost: $curpacketloss% (received $count / $pingcount)"
:set avgpacketloss (($avgpacketloss * ($movingaverage - 1) + $curpacketloss) / $movingaverage)
#:log info "Avgpacketloss: $avgpacketloss"
:local switch true
:if (($avgpacketloss > 10) && ($curpacketloss > $avgpacketloss)) do={
# Packet loss - Decrease speeds
:if ($wan1qos > 5000 && switch) do= { :set wan1qos $spdsafe; set $switch false }
:if ($wan1qos > 4000 && switch) do= { :set adjamt 512; :set $switch false }
:if ($wan1qos > 3000 && switch) do= { :set adjamt 128; :set $switch false }
:if ($wan1qos > 0 && switch) do= { :set adjamt 64; :set $switch false }
:set spdadj (-1 * $adjamt)
} else {
# No packet loss or packet loss decreasing - Increase speed
:if ($wan1qos > 5000 && $wan1qos != $spdmax && switch) do= { :set wan1qos $spdmax; set $switch false }
:if ($wan1qos > 4000 && switch) do= { :set adjamt 512; :set $switch false }
:if ($wan1qos > 3000 && switch) do= { :set adjamt 128; :set $switch false }
:if ($wan1qos > 0 && switch) do= { :set adjamt 64; :set $switch false }
:set spdadj (1 * $adjamt)
}
:set wan1qos ($wan1qos + $spdadj)
:if ($wan1qos < $spdmin) do={ :set wan1qos $spdmin }
:if ($wan1qos > $spdmax) do={ :set wan1qos $spdmax }
:if ($wan1qos != $lastqos) do= {
/system/script/run qos-env
:log info "QoS: $wan1qos (Adjust: $spdadj)"
}
}
:global wan1qos
:local qosrate $wan1qos
/queue/tree/set "WAN_COMBINED" max-limit=($qosrate . "k")
/queue/tree/set "1. VOIP" max-limit=($qosrate . "k") limit-at=(((32 * $qosrate) / 1000) . "k")
/queue/tree/set "2. DNS" max-limit=($qosrate . "k") limit-at=(((16 * $qosrate) / 1000) . "k")
/queue/tree/set "3. ICMP" max-limit=($qosrate . "k") limit-at=(((16 * $qosrate) / 1000) . "k")
/queue/tree/set "4. ACK" max-limit=($qosrate . "k") limit-at=(((256 * $qosrate) / 1000) . "k")
/queue/tree/set "5. UDP" max-limit=($qosrate . "k") limit-at=(((256 * $qosrate) / 1000) . "k")
/queue/tree/set "6. QUIC" max-limit=($qosrate . "k") limit-at=(((256 * $qosrate) / 1000) . "k")
/queue/tree/set "7. HTTP" max-limit=($qosrate . "k") limit-at=(((512 * $qosrate) / 1000) . "k")
/queue/tree/set "8. OTHER" max-limit=($qosrate . "k") limit-at=(((512 * $qosrate) / 1000) . "k")
/queue/tree/set "9. HTTP_BIG" max-limit=($qosrate . "k") limit-at=(((128 * $qosrate) / 1000) . "k")
/queue/tree/set "10. TORRENTS" max-limit=($qosrate . "k") limit-at=(((64 * $qosrate) / 1000) . "k")
Fixed the script: I add some parts but do not fix or check syntax errors.@rextended: forgive me for any ignorance on my part, but shouldn't it be "&& $switch" rather than "&& switch" ? what am I missing?
:local spdmin 512 :local spdsafe 2500 :local spdmax 51200 :local iface "ether1_wan" :local srcadd 192.168.8.100 :local pingcount 3 :local pingadd 8.8.4.4 :global avgpacketloss :global wan1qos :local lastqos $wan1qos :local movingaverage 2 :local curpacketloss "" :local adjamt 0 :local spdadj "" :if ([/interface ethernet get $iface running]) do={ :local count [/ping address=$pingadd interface=$iface src-address=$srcadd size=28 interval=1 count=$pingcount] # :local count 3 :set curpacketloss ((($pingcount - $count) * 100) / $pingcount) # :log info "Percent of pings lost: $curpacketloss% (received $count / $pingcount)" :set avgpacketloss ((($avgpacketloss * ($movingaverage - 1)) + $curpacketloss) / $movingaverage) # :log info "Avgpacketloss: $avgpacketloss" :local switch true :if (($avgpacketloss > 10) and ($curpacketloss > $avgpacketloss)) do={ # Packet loss - Decrease speeds :if (($wan1qos > 5000) and $switch) do={ :set wan1qos $spdsafe; \ :set switch false } :if (($wan1qos > 4000) and $switch) do={ :set adjamt 512; :set switch false } :if (($wan1qos > 3000) and $switch) do={ :set adjamt 128; :set switch false } :if (($wan1qos > 0) and $switch) do={ :set adjamt 64; :set switch false } :set spdadj ($adjamt * -1) } else={ # No packet loss or packet loss decreasing - Increase speed :if (($wan1qos > 5000) and $switch and ($wan1qos != $spdmax)) do={:set wan1qos $spdmax; \ :set switch false } :if (($wan1qos > 4000) and $switch) do={ :set adjamt 512; :set switch false } :if (($wan1qos > 3000) and $switch) do={ :set adjamt 128; :set switch false } :if (($wan1qos > 0) and $switch) do={ :set adjamt 64; :set switch false } :set spdadj ($adjamt * 1) } :set wan1qos ($wan1qos + $spdadj) :if ($wan1qos < $spdmin) do={ :set wan1qos $spdmin } :if ($wan1qos > $spdmax) do={ :set wan1qos $spdmax } :if ($wan1qos != $lastqos) do= { /system script run "qos-env" :log info "QoS: $wan1qos (Adjust: $spdadj)" } }
:global wan1qos :local qosrate $wan1qos :local qosratek ($qosrate * 1024) /queue tree set "WAN_COMBINED" max-limit=$qosratek set "1. VOIP" max-limit=$qosratek limit-at=($qosrate * 32) set "2. DNS" max-limit=$qosratek limit-at=($qosrate * 16) set "3. ICMP" max-limit=$qosratek limit-at=($qosrate * 16) set "4. ACK" max-limit=$qosratek limit-at=($qosrate * 256) set "5. UDP" max-limit=$qosratek limit-at=($qosrate * 256) set "6. QUIC" max-limit=$qosratek limit-at=($qosrate * 256) set "7. HTTP" max-limit=$qosratek limit-at=($qosrate * 512) set "8. OTHER" max-limit=$qosratek limit-at=($qosrate * 512) set "9. HTTP_BIG" max-limit=$qosratek limit-at=($qosrate * 128) set "10. TORRENTS" max-limit=$qosratek limit-at=($qosrate * 64)
Oh wow, that's looking brilliant!! Simply amazing work, I'll try it and advise any feedback THANK YOU for taking the time to go over this script and help jazz it up like this Sorry for hijacking this thread with the script (I was also just trying to find the best way to do "elseif") we can move this to a new thread if necessary.Fixed the script: I add some parts but do not fix or check syntax errors.@rextended: forgive me for any ignorance on my part, but shouldn't it be "&& $switch" rather than "&& switch" ? what am I missing?
The code is only for a concept, but now is really working code.
*************************
Fixed all syntax errors and reformatted, check the differencies.