CS-Script 3.27.0


CS-Script on Mono

Mono is an Open Source implementation of CLR. It is a cross-platform runtime, which is available for Android, BSD, iOS, Linux, Mac OS X, Windows, Solaris, and Unix operating systems. While CS-Script was designed to run on .NET (on Windows) it can run on on Mono on Linux as well.


CS-Script on Windows is all about integration with the operating system and the development tools. However on Linux it is all about simplicity, light weight  and ease of use. Thus if you want to run C# scripts on Linux all you need to do is just a single CS-Script engine file. The following is a very brief demonstration of how you can set and run CS-Script on Linux:


Deploying CS-Script

First of all you need to copy the script engine file (cscs.exe) to the location of your choice on your Linux file system. Starting from version 3.0 CS-Script engine has full built-in support for Mono and the file (cscs.exe) can be taken from the root directory of the CS-Script distributables. However if you are trying to run CS-Script on earlier version of Mono you may need to use corresponding cscs.exe file form <cs-script>/Lib/Bin.


The script engine v3.0 was tested under Mono v2.6.7 on Linux (Ubuntu 11.04). This version of Mono is an equivalent of MS.NET v3.5 thus the script engine file was taken from <cs-script>/Lib/Linux in the distributables (the same executable as in <cs-script>/Lib/Bin/NET 3.5).


Running CS-Script

The script engine (cscs.exe) is a managed application and as such it can be run on Linux providing it is loaded on the Mono environment.
First check if Mono is installed and it is equal or higher than version 2.6.7.

In the terminal execute the following command to check the version:
mono -V

Note: Ubuntu 11.04 comes with Mono 2.6.7 preinstalled.


Now check if you can run CS-Script.
In the terminal execute the following command to check CS-Script version:
mono ./cscs.exe -v

or

./cscs.exe -v

Note: you can omit command "mono" as the shell is smart enough to understand that cscs.exe is a n executable targeting Mono and  it will load the executable into Mono environment even if you did not requested it in the command line.

Also note that all command-line switches on Linux should be specified with prefix '-' in contrary to the Windows environment where the prefix is character is '/'.

This is the expected output:

Note: if CSCSRIPT_DIR environment variable is not set the "Home dir" value is reported as "<not integrated>".


Running C# script

The easiest way of preparing the C# script is to execute script engine with "-s" parameter (print sample script) and redirect output in the file.

In the terminal execute the following command:

./cscs.exe -s > hello.cs

This will yield hello.cs file with the following content:

//css_reference System.Windows.Forms;
using System;
using System.Windows.Forms;

class Script
{
     static public void Main(string[] args)
     {
        for (int i = 0; i < args.Length; i++)
            Console.WriteLine(args[i]);
     
         MessageBox.Show("Just a test!");
     }
}

Now you can execute the script with the following command:

./cscs.exe hello.cs
If you want to suppress printing CS-Script logo ("-nl" - nologo switch) and pass some test arguments to the script the command should look like this:

./cscs.exe -nl hello.cs arg1 arg2

This is the expected output:



Note: Depending on the Linux distro Mono libraries may or may not be installed by default. Thus for the hello.cs sample apart from the Mono minimal installation you will need to have at least two following libraries:
libmono-system-runtime2.0-cil
libmono-winforms2.0-cil


Installing the proper prerequisites solves the problem. It's just that Ubuntu and various other distributions have broken mono up into minimal pieces whereas the original distribution is one big bundle.

It is also possible to load the script in MonoDevelop for debugging:

./cscs.exe debug hello.cs

This will create a MonoDevelop solution on-fly and laod it into IDE. Just make sure that when you copied the script engine on Linux from <cs-script>/Lib/Linux you also copied the debug file.


Changing the way C# scripts run

You probably noticed that the sample script produced in the previous step contains a template for the hashbang.  If you update the template with the proper path to the script engine you will be able to execute the script without specifying the path to the script engine.

Another useful execution option is a possibility to execute scripts containing no class definition (classless scripts). You can enable this functionality by specifying "-ac" (autoclass) parameter.

You can combine these two useful execution options:

#!/home/oleg/Desktop/cs-script/cscs.exe -nl -ac 
using System;

void Main()
{
Console.WriteLine("Hello World!");
}

This would produce the following output:



The autoclass feature is implemented as an embedded (in cscs.exe) precompiler. Precompilers are extremely flexible very simple to implement and they offer almost endless opportunities for the customization. By slightly modifying the default autoclass precompiler you can achieve not only classless script but even a free-style script without any structure at all:

#!/home/oleg/Desktop/cs-script/cscs.exe -nl -precompiler:custom_autoclass.cs 

using
 System;

Console.WriteLine("Hello World! - from free-style script");


And of course virtually all features available on Windows are also available on Linux (e.g. referencing another scripts or assembly, converting to executable). Thus for example conversion of the script into executable is as simple as a trivial execution command one extra parameter:

./cscs.exe -e hello.cs

The parameter "-e" instructs the script engine to produce the hello.exe executable instead of executing the script. Note that hello.exe is just a normal managed executable but not a script launcher containing embedded script engine and the script itself.




Limitations

Yes, CS-Script can be run on Mono, however some functionality may not be available (depending on the Mono version):

 


Acknowledgments

The initial work on testing/adopting CS-Script on Linux was done by Alexander Prokopyev (http://www.aulix.com/mono-vb-net-scripting-host). Because of his enthusiasm and the initiative I decided to rework the script engine and embed Linux support by default.

Oleg Shilo


See Also

Command-line interface | Autoclass | Precompilers