Cheatsheet
Cheatsheet
com
Hello World Loops Key Topic: Code Comments Functions
# file: hello.rsc # "for" loop # single line comment (ends at end-of-line) # define a function
# Hello World Script :for LoopCount from=1 to=10 do={ :global TimeNowFunc do={
:local Message "Hello World!"; :put "Current loop count = $LoopCount"; # comments cannot span multiple lines :return [/system clock get time];
:put $Message; :delay 1; # therefore multiple comments are required for }
} # a comment block
> /import hello.rsc # print current time
Hello World :put "hello"; # inline comments are useful too :local CurrentTime [$TimeNowFunc];
# "foreach" loop :put ("The time now is: $CurrentTime");
:local MyFruit {"apples"; "plums"; "pears"};
Local Variables # function to calc time
# print out each fruit Strings :global TimeInXMins do={
:foreach Fruit in=$MyFruit do={ :local TimeInterval [:totime "00:$1:00"];
# Create local variable with :put "I like $Fruit as a snack"; :local TimeNow [/system clock get time];
# assigned value } # Concatenation
:local BookTitle "Jaws"; :local Food "Chips"; return ($TimeNow + $TimeInterval);
:local Meal ("Fish and " . $Food); }
# Print variable value # "while" loop :put $Meal;
:put $BookTitle; :local TimeNow [/system clock get time]; :local Interval 10;
:local EndTime ($TimeNow + 00:00:10); # Substitution :put ("The time in $Interval mins is " .
# update variable value :local NiceFood "Pizza"; [$TimeInXMins $Interval]);
:set BookTitle "War and Peace"; :put "Starting 10 sec delay timer…"; :put "I love $NiceFood";
# Clear value :while ([/system clock get time] < $EndTime) do={ Operators
:set BookTitle; :delay 1; # find & extract subs-string
} :local InterfaceName "ether1-WAN"; # command operator []
# Note: local vars only exist for :local DashLoc [:find $InterfaceName "-"]; :local Ver [/system resource get version];
# lifetime of a script. :put "Timer expired!"; :put [:pick $InterfaceName 0 $DashLoc];
# substitution $
# "do-while" loop :put "99 + 100 is $(99+100)";
Global Variables :local Temperature 15; :put "the time is $[/system clock get time]";
:do { Arrays
:put "The temperature is: $Temperature "; # grouping operator ()
# Create global variable with } while= ( $Temperature < 12); :put ((10 + 3) * 10);
# assigned value # simple list
:global NumPages 278; # Note: loop executed once even though initial :local Fruit { "apple"; "orange"; "pear" }; # regex ~
# condition is false :put [/interface find where name ~ "^eth"];
# Print variable value # key/pair array
:put $NumPages; :local Interface { name="ether"; speed="1gbps" }; # Addition (std math)
:len <expression>
-> Retrieves an array element
Arithmetic Operators
:local <var> [<value>] Regex Metacharacters
+ Addition
:log <topic> <message> . Match single character
- Subtraction
:parse <expression> [] Match chars in braces
* Multiplication
:pick <str> <start>[<end>] [^] Match chars NOT in braces
/ Division
:pick <array> <start>[<end>]
% Modulo (division remainder) | Add OR logic to pattern
:return <value>
:rndnum from=[num] to=[num] < Less than * Match zero or more instances of char
:rndstr from=[str] to=[num] > Greater than * Match zero or one instances of char