CShell 1.4.0

dotnet add package CShell --version 1.4.0                
NuGet\Install-Package CShell -Version 1.4.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CShell" Version="1.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CShell --version 1.4.0                
#r "nuget: CShell, 1.4.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CShell as a Cake Addin
#addin nuget:?package=CShell&version=1.4.0

// Install CShell as a Cake Tool
#tool nuget:?package=CShell&version=1.4.0                

<img src="https://github.com/tomlm/CShell/raw/master/turtle.png" width="100"/>

CShell

CShell creates a runtime environment to make it easy to create C# based shell style scripts.

Description

CShell is built using MedallionShell and runs great using dotnet-script (.csx) giving you a great cross platform C# alternative to powershell and bash scripts.

CShell provides:

  • The concept of a current folder with relative commands for navigating and manipulating files and folders
  • The ability to smoothly invoke processes and pipe
  • Helpers to make it easy to work with the output of processes

By maintaining the concept of a current folder all file and folder commands can be take absolute or relative paths just like a normal shell.

Properties

CShell exposes 3 properties which are the working environment of your script. The CurrentFolder is used to resolve relative paths for most methods, so if you call MoveFile(@"..\foo.txt", @"....\bar") it will resolve the paths and execute just like a normal shell.

Property Description
CurrentFolder The current folder as a DirectoryInfo object
FolderHistory List of folder paths you have navigated to
FolderStack current stack from Push/Pop operations
Echo Controls whether commands are echoed to output
ThrowOnError Controls whether to throw exception when commands have non-sucess error code

Folder Methods

CShell defines a number of methods which work relative to the current folder to make it easy to manipulate folders.

Method Description
cd() Change the current folder with relative or absolute path
md() Create a folder relative to current folder
rd() Delete a folder relative to current folder
pushd() Push the current folder onto the stack and change folder to the new one
popd() Pop the current folder off the stack and change the folder the popped folder

File Methods

CShell defines a number of methods which work relative to the current folder to make it easy to manipulate files.

Method Description
copy() Copy a file relative to current folder
move() Move a file relative to current folder
rename() Move a file relative to current folder
delete() Delete a file relative to current folder
erase() does a file relative to current folder exist
type() type a file to standardout
cat() cat a file to standardout

Process Methods

CShell is built using MedallionShell, which provides a great set of functionality for easily invoking processes and piping data between them. CShell adds on location awareness and helper methods to make it even easier to work with the output of processes.

Method Description
ReadFile()/cat()/type() read a file and create a stream
echo(text/lines/stream) echo text,lines from memory to a stream
Run(program, arg1, ..., argN) run a program directly with the given args (aka Process.Start(program, args)
Start(program, arg1, ..., argN) run a DETACHED program directly with the given args (aka Process.Start(program, args)
Cmd(cmd) run the cmd inside a cmd.exe, allow you to execute shell commands (like dir /b .
Bash(bash) run the program in bash environment, allow you to execute bash shell commands (like ls -al *
// Invoke multiple commands using fluent style
var cmd1= await Run("cmd1", "args1")
    .PipeTo("cmd2", "args2", "args3")
    .PipeTo("cmd3", "args4");
var result1 = await cmd1.AsResult();

// we can even chain commands together with the pipe operator
var cmd2 = await Run("cmd1", "args1") 
    | Run("cmd2", "args2", "args3") 
    | Run("cmd3", "args4");
var result2 = await cmd2.AsResult();

// we can even chain commands together with the > operator
var = await Run("cmd1", "args1") 
    > Run("cmd2", "args2", "args3")
    > Run("cmd3", "args4");
var result3 = await cmd3.AsResult();

The CommandResult object has StandardOutput, StandardError information for further processing.

Working with results

CShell adds on helper methods to make it even easier to work with the result of a command chain.

Method Description
Execute(log) get the CommandResult (with stdout/stderr) of the last command
AsString(log) get the standard out of the last command a string
AsJson(log) JSON Deserialize the standard out of the last command into a JObject/dynamic
AsJson<T>(log) JSON Deserialize the standard out of the last command into a typed T
AsXml<T>(log) XML Deserialize the standard out of the last command intoa typed T
AsFile() Write the stdout/stderr of the last command to a file

To call a program you await on:

  1. call ReadFile()/Run()/Cmd()/Bash()/echo()
  2. call any chaining commands
  3. end with a result call like Execute()/AsJson()/AsString()/AsXml()etc.

The result methods all take a log argument is passed set to true then the commands output will be written to standard out.

global using static CShellNet.Globals;
using CShellNet;

Console.WriteLine("Hello world!");

// run a command and interpret the json as an AccountInfo object
var account = await Cmd("az account show").AsJson<AccountInfo>();
    
// run a command and interpret the XML as an AccountInfo object
var account2 = await Cmd("az account show -o xml").AsXml<AccountInfo>();
    
// run a command interpret the result as a string.
var accountString = await Cmd("az account show").AsString();
    
// run a command and get back the CommandResult which has Succes, StatusCode, StandardInput and StandardError.
var result = await (Run("x", "foo") | Cmd("az account show")).AsResult();
if(result.Sucess)
{
    var output = result.StandardOutput;
    ...
}

CShell + dotnet-script == awesome

CShell is a dotnet library which can be used in any .net program, but it is super useful to use from a dotnet-script (.csx) file. There is a dotnet template to make it super easy to create a .csx file with CShell all set up to use.

To install dotnet-script support

dotnet tool install -g dotnet-script

To install the csx template

dotnet new --install CShell.Template

To invoke the template

dotnet new cshell

NOTE: If you want debug support from visual studio code simply run dotnet script init in the same folder.

#r "nuget: CShell, 1.4.0"
global using static CShellNet.Globals;
using CShellNet;

Console.WriteLine("Hello world!");
foreach (var arg in Args)
{
    md(arg);
    ...
}

Registering .csx files to be executable on windows

You can register dotnet-script as the default handler for .csx files by running these commands:

dotnet script register

After registering you can simple type your.csx to execute your cshell program.

CHANGELOG

V1.4.0

  • Added Start() method for detached processess (you can monitor process but not access input/output)
  • Added Run(Action<Option>, process, arg1, arg2) signature to control options for starting processes
  • Added echo() method for piping strings/streams into .Run() commands.
  • Added CShellNet.Globals which enables top-level mainless .cs/.csx projects

V1.2.3

  • Added log parameters to AsJson()/AsXml()/AsResult() output standardOut/StandardError
  • added Execute() as alias for AsResult() as that seems cleaner then AsResult()

v1.2.1

  • Added ThrowOnError property to turn on/off throwing on command failing.

v1.2.0

  • Added echo(true) echo(false) to turn on off echo of the commands
  • added Cmd(".....") to allow you to execute cmd.exe functions (Example: Cmd("dir /b foo") )
  • added Bash("....") to allow you to execute bash commands (Example: Bash("ls -al") )
  • Upgraded MedalianShell to 1.6.1
  • upgraded JSon.Net to 12.x
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.0 335 8/17/2024
1.3.0 554 3/24/2023
1.2.4 7,778 9/4/2020
1.2.3 405 9/4/2020
1.2.2 422 9/4/2020
1.1.0 1,190 9/28/2018
1.0.11 738 9/28/2018
1.0.10 728 9/27/2018
1.0.9 737 9/27/2018
1.0.8 717 9/27/2018
1.0.7 727 9/27/2018