MiniScript on the Command Line

You can use MiniScript on the command line, and even use it to write clean, readable shell scripts!

Download & Installation

Download and unzip the appropriate binary for your system:

Then install it somewhere in your path. On Mac and Linux, /usr/local/bin/ is a standard choice.

Usage

The miniscript command by itself invokes an interactive (read/eval/print) loop. You can enter any MiniScript expression, press Return, and immediately see the answer. This is handy for quick calculations or experimentation.

$ miniscript
MiniScript v1.3
Apr  6 2019
> 2+2
4

You can also enter functions, loops, if statements, etc.; when you have one of these open, the prompt changes from > to >>> to indicate that more input is needed.

> for i in range(1,5)
>>> print("Spam " * i)
>>> 

The code will be executed when the block closer (end for in this case) has been entered.

To exit interactive MiniScript, press Control-D (i.e. terminate input) or enter exit.

You can also invoke MiniScript with a single command (which could be multiple statements separated by semicolons), using the -c option. MiniScript will execute that command and immediately exit.

$ miniscript -c 'print(2^12)'
4096

Or, you can execute miniscript with the path to a MiniScript script file. The script file will be executed, and MiniScript will exit when done. Note that in this mode, MiniScript skips the first line if it starts with "#!". This allows you to write shell scripts that can be executed directly.

#!/usr/local/bin/miniscript
print("The current directory is: " + File.curdir)
print("And my arguments are: " + shellArgs)

Command-line MiniScript has a few other command-line options; use miniscript -? to see what they are.

Extra Intrinsics

In addition to the standard MiniScript intrinsic methods (as seen in the Quick Reference and User's Manual), the command-line version of MiniScript provides the following:

exit(resultCode=0)quits the MiniScript session
input(prompt="")prompts the user for input, and returns the entered string
shellArgsreturns command-line arguments given with script invocation, as a list; shellArgs[0] is the script itself
Filemodule of file-related methods (see below)

File returns a map that simply groups together a bunch of methods for manipulating files.

File.curdirreturns the current working directory
File.setdir(path)changes the current working directory
File.children(path)returns list of files within the given directory
File.name(path)returns the file name from the given path
File.parent(path)returns the path to the directory containing the given path
File.child(parentPath, childName)combines the given directory path and file name, returning path to the child
File.move(oldPath, newPath)moves/renames a file
File.delete(path)deletes a file
File.open(path, mode="rw+")opens or creates a file; mode can be "rw+" (open for reading/updating, creating if it doesn't exist), or any standard fopen mode; returns a file handle object (see below)

Finally, when you use File.open to open a file, what you get back is a little object that allows for reading and writing from that file. This object has the following methods.

.closecloses the open file
.isOpenreturns true if the file is open, false if closed
.write(s)writes the given string to the file
.writeLine(s)writes the given string, followed by a carriage return
.readreads the rest of the file, returning it as a string
.readLinewrites the next line of the file
.atEndreturns true at the end of the file, false otherwise

Source Code

We will be putting the source code for MiniScript core, as well as the command-line wrapper, up in a public GitHub repo this summer (2019). In the meantime, please be patient while we get our act together.