Skip to content

TCL scripting using mopscript environment

MOP3 allows user-space scripting via a tiny TCL interpreter (originally PICOL: Source). This modified implementation adds custom commands to suit the MOP3 operating system.

Getting started

To start playing around with TCL, you can use the shell's built-in text editor:

$ mkfile /temp/my_script.tcl
$ edit /temp/my_script.tcl

To run your first hello world script you can use the puts command:

puts "Hello world"
# Save and quit: C-x + C-w, C-x + C-q

And then

$ /sys/mopscript -f /temp/my_script.tcl

Or if you'd like to call the script itself directly:

$ cat /temp/my_script.tcl

#!/sys/mopscript -f

puts "Hello world"

$ /temp/my_script.tcl
Hello world

Defining procedures

To define your first procedure you can use the proc keyword:

proc fib {n} {
    if {$n <= 1} {
        return $n
    }
    expr [fib [expr $n-1]] + [fib [expr $n-2]]
}

puts [fib 20]

Running external applications

To run external applications we can use two commands: exec or exec_bg.

exec will run the application and block until it finishes executing. While doing that, it will collect output of the ran application and return it as a string.

# exec example

puts [exec /sys/sdutil -lpd -d /devices/ide0]

exec_bg will not block (and thus won't collect the output). The returned string will be a number representing the PID of started application.

# exec_bg example

puts [exec_bg /sys/spin]