You can use MiniScript on the command line, and even use it to write clean, readable shell scripts!
Download and unzip the appropriate binary for your system:
Then install it (or a symbolic link to it) somewhere in your path. On Mac and Linux, /usr/local/bin/ is a standard choice.
The distribution includes a lib folder which you'll want to keep next to the executable, and also a demo folder of short programs you can keep wherever. See the included README.md file for more details.
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 Command-Line (Unix) v1.3; language v1.6.2 (Feb 18 2024) > 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.
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 |
shellArgs | returns command-line arguments given with script invocation, as a list; shellArgs[0] is the script itself |
env | returns the shell environment variables, as a map; assignment to this map adds/updates environment variables |
exec(cmd, timeout=30) | executes a shell command (timeout is in seconds) |
file | module of file-related methods (see below) |
import | imports another script (see the wiki for details) |
file
returns a map that simply groups together a bunch of methods for manipulating files.
file.curdir | returns the current working directory |
---|---|
file.setdir(path) | changes the current working directory |
file.makedir(path) | creates a new 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.exists(path) | returns true if a file exists at the given path |
file.copy(oldPath, newPath) | copies a file to a new location |
file.move(oldPath, newPath) | moves/renames a file |
file.delete(path) | deletes a file |
file.info(path) | gets a map of details about a file including size, isDirectory, date, and (full) path |
file.readLines(path) | returns full contents of a text file as a list of strings |
file.writeLines(path, data) | writes out the given list as a text file, one line per list element |
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.
.close | closes the open file |
---|---|
.isOpen | returns 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 |
.read | reads the rest of the file, returning it as a string |
.readLine | reads the next line of the file |
.position | returns current read/write position in the file |
.atEnd | returns true at the end of the file, false otherwise |
Source code for the MiniScript core, and this command-line wrapper is available in our GitHub repo. Contributions and suggestions are very welcome!
1.3 | Feb 18 2024 |
1.2.1 | Jun 30 2023 | 1.2 | Feb 06 2023 | 1.1 | Dec 18 2021 | Updated core language to 1.5.1 (no changes to host environment). | 1.1 | Jan 16 2021 | Added | env to provide access to environment variables.1.0 | Sep 13 2019 | First public release for Mac, Windows, and Linux. | |
---|