Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows shell scripts in C#

I was pretty sure that some months ago I read about the possibility to launch Windows scripts not only the usual .bat, but also .js, .vbs and .cs !

I was willing to try windows scripts in C# .cs files this morning, but I can't happen to find any references on the web. All CS/C# scripts stuffs seem to be 3rd parties.

Do you have any knowledge reference/link for command-line scripts in C# for Windows, pure microsoft style ?

Yes, that would mean dynamic C# compilation... so maybe I am all wrong thinking it is possible. Just tell me.

like image 385
Stephane Rolland Avatar asked Jun 27 '11 09:06

Stephane Rolland


People also ask

Are shell scripts written in C?

C shell's scripting syntax is modeled after the C language in some aspects. Small programs can be created by writing scripts using the C shell syntax. The Bourne shell is also an option to create Unix scripts but if you are reading this book you probably decided the C shell fits your requirements better.

What is a shell script in C?

The C shell is a command processor which is typically run in a text window, allowing the user to type and execute commands. The C shell can also read commands from a file, called a script.

Can we write shell script in Windows?

With the arrival of Windows 10's Bash shell, you can now create and run Bash shell scripts on Windows 10. You can also incorporate Bash commands into a Windows batch file or PowerShell script.

What is Windows equivalent of shell script?

The Windows NT family of Operating Systems (NT 3.51, NT 4.0, 2000, XP, Vista) have a reasonably powerful Command Shell even if it is a bit esoteric. This shell does not have the power of the typical UNIX shells however it is ubiquitous on Windows machines and it is pretty amazing what can be achieved.


1 Answers

Option A

If you're looking for a true C# development experience, try CS-Script.

You can use comment directives to use third-party libraries and include other C# scripts.

//css_nuget dotnetzip
using Ionic.Zip;
using System;

class Script
{
    static public void Main()
    {
        using(ZipFile zip = new ZipFile())
        {
            zip.AddFile("app.exe");
            zip.AddFile("readme.exe");
            zip.Save("app.zip");
        }
    }
}

Personally, I prefer CS-Script because I've noticed 1) faster script start-up times compared to scriptcs below and 2) no issues using namespaces. C# you write here is real C#. The VS IDE just works.

Option B

Alternatively, take a look at scriptcs

http://scriptcs.net/

You can also use VS to write some simple scripts:

 //MyScript.Main()

 public class MyScript{
     public static void Main(){
          Console.WriteLine("hello world.");
     }
 }

When you're ready to run scripts with scriptcs, remove the // comment.

C:\> scriptcs myscript.cs

Also, you can use nuget to install 3rd party packages like NLog

C:\> scriptcs -install NLog

One problem I found with scriptcs is the inability to use namespaces. scriptcs under the hood uses the Roslyn compiler for compiling C# scripts. Unfortunately, Roslyn has a leaky design implementation when compiling scripts. In particular, in order for Roslyn to handle REPL type of use cases, each script compilation uses nested classes to store variables and state. Since you can't have a namespace in a nested class using namespaces with scriptcs will fail. So if you try to import loose *.cs files that contain namespaces (like domain objects from a VS project), you'll need to strip out the namespaces.

This awful design decision left a bad impression on me, so I've mostly used cs-script Option A until the Roslyn team supports namespaces.

like image 152
Brian Chavez Avatar answered Oct 19 '22 23:10

Brian Chavez